Python Flashcards
What is an integer and float?
What does this mean:
a = 4
b = 3.142
c = 5.
Basic integer (int) types represent whole numbers, and are useful to act as programming counters, indices, and for basic integer arithmetic. They are made with a variable name, an equals sign = and the value of the integer.
Float datatypes in Python represent decimal numbers, and follow a similar construction pattern to integers, the main difference is that the number has a decimal point after the =
a = 4 as an integer
b = 3.142 as a float
c = 5.0 as a float
If:
a = 5
b = 6.
c = 4
d = 2
What is:
1. print(a - b)
2. print(a + c)
3. print(a/c)
4. print(b/c)
5. print(c**d)
- -1.0
- 9
- 1
- 1.5
- 16
What are strings?
Define:
a = ‘You’
b = “Yeah”
print(b, a)
print( a + ‘smell’)
A string (str) represents text as a sequence of characters, and is used for various tasks such as file input/output processes or printing messages to the user. Strings are defined in Python using either single ‘ or double “
marks around the characters
Yeah you
You smell
What are three data containers and are they for order or unordered data structures?
- Two of the most commonly used containers for sequences of data where the order of the items matter are:
- List: a mutable container that allows adding, removing, and editing of
items.
- Tuple: an immutable container that cannot be modified once created.
-For unordered data, we can use a dict (dictionary), which is very efficient for looking up entries in large datasets.
Explain what a the list function is in python
- Python lists are flexible data containers that can hold a sequence of any type of Python objects.
- They can be easily modified after creation, allowing for actions such as adding, editing, removing, and sorting items.
- Lists are created by typing the sequence of items within square brackets and separated by commas, such as [a, b, c, …].
- Lists can store any type of data, including other lists, and do not need to have homogeneous data types for each item.
What does this script mean?
a = [ 3, 5, 6, 8]
b = [ 7, 8, 9 ]
c = [ 5, 2, 8]
a. append(3)
b. extend(c)
print(a)
print(b)
[3, 5, 6, 8, 3]
[7, 8, 9, 5, 2, 8]
What does this script mean?
a = [ ‘one’, ‘up’, ‘one’, ‘down’]
a[3] = ‘Smash’
print(a)
print(a[1])
print(a[-2])
[‘one’, ‘up’, ‘one’, ‘Smash’]
up
one
b = [ ‘I’, ‘dont’, ‘actually’, ‘get’, ‘very’, ‘nervous’ ]
b. remove( ‘actually’)
del b[3]
print(b)
c = [ ‘im’, ‘to’, ‘get’, ‘shaky’ ]
c. insert(1, ‘starting’)
print(c)
[‘I’, ‘dont’, ‘get’, ‘nervous’]
[‘im’, ‘starting’, ‘to’, ‘get’, ‘shaky’]
What are Tuples?
- Tuples are similar to lists, but are read-only.
- They cannot be modified once created, unlike lists.
- They are created by a sequence of datatypes separated by commas, such as a, b, c, but it’s commonly enclosed in parentheses (a, b, c).
- Many of the manipulations that can be done with lists can also be done with tuples, except for those that involve changing the data in the tuple sequence.
What are Dictionaries?
- Dictionaries map unique keys to specific values, and are created using curly brackets {} (e.g. a={} for an empty dictionary).
- Dictionaries are one of the most powerful and important data containers in Python.
- They are very efficient for storing and retrieving data in a organized and fast manner.
- Dict keys are usually an int or a str, while values can be any datatype in Python, including other dict objects.
- Dictionaries can be constructed by using a colon : between the key and value, and a comma between each entry.
What does this script mean?
Food = {
‘burgers’ : 1,
‘pizza’ : 2,
‘cheese’ : 300,
}
Food[‘paella’] = 4
Food[‘burgers’] = 7
print(Food)
print(list(Food.keys()))
print(list(Food.values()))
print(list(Food.items()))
{‘burgers’: 7, ‘pizza’: 2, ‘cheese’: 300, ‘paella’: 4}
[‘burgers’, ‘pizza’, ‘cheese’, ‘paella’]
[7, 2, 300, 4]
[(‘burgers’, 7), (‘pizza’, 2), (‘cheese’, 300), (‘paella’, 4)]
What is slicing?
- Slicing is a way to extract a sub-sequence of an object such as a list, string, or tuple.
- It is done by specifying the start point (inclusive) and endpoint (exclusive) of the slice using a colon :, such as A[start:end].
- The start point is 0-indexed, meaning the first element is index 0.
- Slicing can also be used to change more than one item at a time.
a = [5,6,9,2,6,77,7,8,10,23,1]
print(a[1:3])
print(a[:4])
print(a[5:])
print(max(a), min(a))
b = ‘abcdefghijklmnop’
print(b[2:10:2])
print(b[ : : -2])
print(len(a))
print(len(b))
[6, 9]
[5, 6, 9, 2]
[77, 7, 8, 10, 23, 1]
(77, 1)
cegi
pnljhfdb
11
16
What is Control Flow and what are three types?
- Control flow in programming is about directing code to execute in a non-sequential manner, run multiple times, or not run at all.
- Key concepts for control flow in Python include:
-for and while loops with break and continue
-if-else-elif statements
-try-except
-Python uses indentation of code with 4 spaces to indicate control flow structure, most text editors will automatically add 4 spaces when TAB key is pressed.
What does this script mean?
a = 6
if (a > 0) and ( a % 2 == 0):
print( ‘ Postive and even’)
b = -7
if (b < 0) and ( a % 2 != 0):
print( ‘negative and odd’)
Positive and even
Negative and odd