Python Flashcards

1
Q

What are the 4 major data types?

A

Integer, Float, String, Boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an integer?
What category does it fall into?
What is it’s command

A

Real Number
Data Types
int( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an float?
What category does it fall into?
What is it’s command?

A

Represents a floatingnumber (1.2, 2.0, 2.1)
Data Type
float( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an boolean?
What category does it fall into?
What is it’s command?

A

Represents True (1) or False (0)
Data Type
bool( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a string?
What category does it fall into?
What is it’s command?

A

Represents a sequence of characters enclosed in single quotes (‘’) or double quotes (“”).
Data Type
str( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a list?
What category does it fall into?

A

Data Structure representing an ordered collection of items enclosed in square brackets ([]). Each element in a list is assigned a unique index, starting from 0 for the first element. The items can be of any data type and are mutable, meaning their values can be changed.

fruits = [“apple”, “banana”, “orange”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a tuple?
What category does it fall into?

A

Data structure representing an ordered collection of items enclosed in parentheses (()). Each element in a tuple is assigned a unique index, starting from 0 for the first element. The items can be of any data type. Tuples are immutable, meaning their values cannot be changed once defined.

coordinates = (10.0, 20.0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a set?
What category does it fall into?

A

Data structure representing an unordered collection of unique items enclosed in curly braces ({}). Sets are mutable, meaning their values can be changed.

numbers = {1, 2, 3, 4, 5}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a dictionary?
What category does it fall into?

A

Data structure representing a collection of key-value pairs enclosed in curly braces ({}). Each key-value pair is separated by a colon (:), and the keys are unique. Dictionaries are mutable, meaning their values can be changed.

person = {“name”: “John”, “age”: 30}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you write a list called numbers including numbers 1 to 5?

A

numbers = [1, 2, 3, 4, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you code a tuple called coordinates with values of 10 and 20?

A

coordinates = (10.0, 20.0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you code a set called fruits with values Apple, banana, and orange?

A

fruits = {“apple”, “banana”, “orange”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you code a dictionary entry called person named John, age 30?

A

person = {“name”: “John”, “age”: 30}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a data type?

A

data types define the type of value that a variable can hold

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is casting?

A

Casting in Python refers to the process of changing the data type of a value. It allows you to convert a value from one type to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a Modulo?
How is it written?
What does it do?

A

Modulo (%): Returns the remainder of the division.

result = 10 % 3
print(result) # Output: 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is floor division?
How is it written?

A

Returns the quotient of the division, rounded down to the nearest integer.

result = 10 // 3
print(result) # Output: 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are variables?
What are they used for?

A

Variables in Python are used to store and manipulate data. A variable is a named location in the computer’s memory where you can store a value.

19
Q

What does mutable mean?

A

Means the values can be changed.

20
Q

What does immutable mean?

A

Means the values cannot be changed once defined.

21
Q

How do you access an item in a list or tuple?
Access Apple in the following list?

fruits = [“apple”, “banana”, “orange”]

A

You can access individual elements in a list by their index. Remember that the index starts from 0. For example:

fruits = [“apple”, “banana”, “orange”]
print(fruits[0]) # Output: “apple”
print(fruits[1]) # Output: “banana”

22
Q

How do you add items to a list or tuple?
What is the code to add grapes to this list?

fruits = [“apple”, “banana”, “orange”]

A

You can add new elements to the end of a list or tuple using the append() method.

fruits = [“apple”, “banana”, “orange”]
fruits.append(“grape”)
print(fruits) # Output: [“apple”, “banana”, “orange”, “grape”]

23
Q

How do you modify the value of a list or tuple element?
What is the code to change banana to kiwi?

fruits = [“apple”, “banana”, “orange”]

A

You can change the value of an element in a list or tuple by assigning a new value to its index.

fruits = [“apple”, “banana”, “orange”]
fruits[1] = “kiwi”
print(fruits) # Output: [“apple”, “kiwi”, “orange”]

24
Q

What is it called when you extract a portion of a list or tuple?
Extract banana, orange, and kiwi from below:

fruits = [“apple”, “banana”, “orange”, “kiwi”, “grape”]

A

You can extract a portion of a list using slicing. Slicing allows you to specify a range of indices to extract elements.

fruits = [“apple”, “banana”, “orange”, “kiwi”, “grape”]
sliced_fruits = fruits[1:4]
print(sliced_fruits) # Output: [“banana”, “orange”, “kiwi”]

25
How to you find out the length of a list?
You can determine the length of a list (i.e., the number of elements) using the len() function. fruits = ["apple", "banana", "orange"] length = len(fruits) print(length) # Output: 3
26
What is unpacking a tuple? What is it useful for? How would you code it?
Unpacking: You can unpack the values of a tuple into separate variables. This is useful when you want to assign each element of a tuple to a different variable. coordinates = (10.0, 20.0) x, y = coordinates print(x) # Output: 10.0 print(y) # Output: 20.0
27
How do you access/print values in a dictionary? How would you code it?
Accessing Values: You can access the value associated with a specific key in a dictionary by using the key as an index. person = {"name": "John", "age": 30, "city": "New York"} print(person["name"]) # Output: "John" print(person["age"]) # Output: 30
28
How do you modify values of a dictionary? How would you code it?
Modifying Values: You can change the value associated with a specific key in a dictionary by assigning a new value to it. person = {"name": "John", "age": 30, "city": "New York"} person["age"] = 35 print(person["age"]) # Output: 35
29
How would you add a new key value pair of a dictionary? Code city / New York into list person?
Adding New Key-Value Pairs: You can add new key-value pairs to a dictionary by using a new key as an index and assigning a value to it. person = {"name": "John", "age": 30} person["city"] = "New York" print(person) # Output: {"name": "John", "age": 30, "city": "New York"}
30
How do you delete a key-value pair from a dictionary? How do you code deleting
Removing Key-Value Pairs: You can remove a specific key-value pair from a dictionary using the del keyword. person = {"name": "John", "age": 30, "city": "New York"} del person["age"] print(person) # Output: {"name": "John", "city": "New York"}
31
How do you check if a key exists in a dictionary? What would the output be presented?
Checking Key Existence: You can check if a specific key exists in a dictionary using the in keyword. person = {"name": "John", "age": 30, "city": "New York"} print("name" in person) # Output: True print("country" in person) # Output: False
32
How do you get keys and/or values from a dictionary?
Getting Keys and Values: You can retrieve all the keys or values from a dictionary using the keys() and values() methods, respectively. person = {"name": "John", "age": 30, "city": "New York"} keys = person.keys() values = person.values() print(keys) # Output: dict_keys(["name", "age", "city"]) print(values) # Output: dict_values(["John", 30, "New York"])
33
How do you add elements into a set? How would you code adding grapes to a set called fruits?
Adding Elements: You can add new elements to a set using the add() method. fruits = {"apple", "banana", "orange"} fruits.add("grape") print(fruits) # Output: {"apple", "banana", "orange", "grape"}
34
How do you remove elements from a set?
Removing Elements: You can remove elements from a set using the remove() method. fruits = {"apple", "banana", "orange"} fruits.remove("banana") print(fruits) # Output: {"apple", "orange"}
35
How do you check existence of an element in a set? How would it show in output?
Checking Existence: You can check if an element exists in a set using the in keyword. fruits = {"apple", "banana", "orange"} print("apple" in fruits) # Output: True print("grape" in fruits) # Output: False
36
How do you combine two sets? Set1 and Set2
set1 = {1, 2, 3} set2 = {2, 3, 4} # Union union_set = set1.union(set2) print(union_set) # Output: {1, 2, 3, 4}
37
How do you show the difference in two sets, set1 and set2?
set1 = {1, 2, 3} set2 = {2, 3, 4} difference_set = set1.difference(set2) print(difference_set) # Output: {1}
38
How do you see the common items of a set, set1 and set2?
set1 = {1, 2, 3} set2 = {2, 3, 4} intersection_set = set1.intersection(set2) print(intersection_set) # Output: {2, 3}
39
How do you find out the length of a set?
Length: You can determine the number of elements in a set using the len() function. fruits = {"apple", "banana", "orange"} length = len(fruits) print(length) # Output: 3
40
What are loops? What are the two main types?
Loops in Python are used to repeatedly execute a block of code until a certain condition is met. They allow you to automate repetitive tasks and iterate over collections of data. for loop while loop
41
What is a “for” loop?
The for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects. It executes a block of code for each item in the sequence. for i in range(5): print(i) #output 0 1 2 3 4
42
What is a while loop?
The while loop is used to repeatedly execute a block of code as long as a certain condition is true. It continues executing the loop as long as the condition remains true. count = 0 while count < 5: print(count) count += 1 #output 0 1 2 3 4
43
What is a function? How do you define a function?
Define a function in Python, you can use the def keyword followed by the function name and a pair of parentheses. You can also specify parameters inside the parentheses if the function takes any inputs. def calculate_area(length, width): area = length * width return area result = calculate_area(5, 3) print(result) # Output: 15
44
How do you sort?
The sort() method is used to sort a list in-place, meaning it modifies the original list. numbers = [3, 1, 4, 2, 5] numbers.sort() print(numbers) # Output: [1, 2, 3, 4, 5]