Chapter 4: Data Types in Python Flashcards
What are integers in Python, and how do you declare them?
Integers are numbers with no decimals such as -4,4. The declare an integer write variableNAme= initial value.
userAge=30
What is float and how do you declare it?
It refers to numbers that have decimal parts such as 1.234, -0.089. To declare a float in Python we write variableNAme = initial value
Example userHeight = 157.6
What is a string? How do you declare it?
It refers to text. To declare it you can either use variableName= 'initial value'(single quotes) or variableName="initial value"(double quotes example userName="Amy" userSpouseName = "Eric" userAge = "33"
Here 33 isn’t an integer but a string as it is encased in quotes.
How can you combine multiple substrings? Provide examples
Using the concatenate”+” sign. For example, “Amy” + “Eric” will give us Amy Eric
What are built-in string functions?
Python includes a number of built-in functions to manipulate strings. A function is a block of reusable code that performs a certain task.
Provide examples of built-in string functions
upper() method which can be used to capitalize all the letters in a string. For example, ‘Amy’.upper() will give us the string ‘AMY’.
What is the syntax for the % operator?
“string to be formatted”%(values or variables to be inserted into string, separated by commas)
What are the three parts to the % operator?
First, we write the string to be formatted in quotes.
Next, we write the % symbol.
Finally, we have a pair of parentheses within which we write the values or variables to be inserted into the string. The parentheses are known as tuple.
What will be the output for the below code?
brand = “Apple”
exchangeRate = 1.2345
message= “The price of this %s laptop is %d USD and the exchange rate is %4.2fUSD to 1 EUR”%(brand,1299,exchangeRate)
print(message)
The price of this Apple laptop is 1299 USD and the exchange rare is 1.23 USD to 1 EUR
What are %s, %d and %4.2f?
They are known as formatters and serve as placeholders in the string.
%s represents the string
%d represents an integer. to add space before the integer, add a number between % and d sign i.e. %5d adds 2 spaces in front with total length being 5.
%4.2f represents the float. in 4.2, 4 refers to the total length and 2 refers to the decimal places. To add space before the number increase the total length i.e. 4.2 to 7.2
What is the syntax for format () method?
“string to be formatted”.format()
Provide and explain an example of the format () method.
message=”The price of this {0:s} laptop is {1:d} USD and the exchange rate is {2:4.2f} USD to 1 EUR”.format(“Apple”, 1299, 1.235)
WE use braces in the format method. 0:s is assigning the position to the string i.e. Apple, 1:d is assigning the position 1 to the integer 1299 and {2:4.2f} is assigning the second position with 4 as total length and 2 decimal places to the float 1.235. Now if you write a second message with the positions swiped as in
message1=”The price of this {1} laptop is {0} USD and the exchange rate is {2} USD to 1 EUR”.format(“Apple”, 1299, 1.235)
the result will be:
The price of this 1299 laptop is Apple USD and the exchange rate is 1.23 USD to 1 EUR.
What if you don’t want to format the string?
You can simply write
message=”The price of this {} laptop is {} USD and the exchange rate is {} USD to 1 EUR”.format (“Apple”, 1299, 1.235).
The output will be:
The price of this Apple laptop is 1299 USD and the exchange rate is 1.235 USD to 1 EUR
Python assigns the position in the order the variables are mentioned in the parentheses. Therefore, if you change the order to (1299, “Apple”, 1.235), the output will be:
The price of this 1299 laptop is Apple USD and the exchange rate is 1.235 USD to 1 EUR
For the format method where does the position start from?
ZERO
What will the below code yield?
message3=’ and {:d}.format{1.23, 12}
6spaces1.23 and 12
What will the below code yield?
message4=”{}.format(1.23)
1.23
What is Type Casting?
Changing the format of the data types from integer to float to String is called type casting
What are the three built-in functions that allow us to do type casting?
Int(), Float(), Str()
What does INT() do?
It converts float and an appropriate string to Integer. For example, INT(2.34) will yield 2, INT(“2”) it will yield 2
What does float() do?
It converts integer and appropriate string to float. For example, float(2) or float (“2”), we will get 2.0
What does str() do?
It converts a float or integer to a string. For example, str(2.1) will yield “2.1”
What is a list?
It refers to collection of data that is related. Instead of storing this data separately, we can store them as a list.
How do you declare a list?
listName=[initial values]
For example, userAge=[21,22,23,24,25]
What kind of brackets are used to store a list?
Square brackets
How do you declare an empty list and which function is used to add items to it?
userAge=[]
We use append() function to add items to this list
What are indexes?
The individual values in the list are assigned an index number. The first item on the list is always indexed 0 and the last as -1. For example, in userAge = [21,22,23,24,25],
userAge[0] = 21
userAge[1] = 22
userAge[-1] = 25
How do you assign the complete or a part of the list to a variable? The list being
userAge = [21,22,23,24,25]
userAge2=userAge
This will assign all the values in userAge list to the variable userAge2. For partial assignment, wuserAge3=userAge[2:4] will yield [23,24]. 2 represents the index number in the list, 4 actually means 4-1 i.e. up until the item with index number 3.
What is the notation [2:4] called(taking cue from the previous card), and what is a stepper? Explain.
The notation 2:4 is a slice, and the third number in this notation is called a stepper. If we write userAge4= userAge [1:5:2], we get a sub list for every second item from index 1 to index 5-1. Therefore, if the list is userAge= [21,22,23,24,25] then the result would be userAge4= [22,24]
What are the defaults for the slice notations?
The default for the first number is 0. Therefore, userAge[:4] will yield values from index 0 to index 4-1.
The default for second number is the size of the list. Therefore, userAge[1:] will yield values from index 1 to 5-1 (assuming the size of the list is 5 items)
How do you modify the items in the list?
listName[index of item to be modified]= new value
For example
userAge[1]=5 yields
userAge=[21,5,23,24,25]
Which function do you use to add items to the list and how?
append()
For example
userAge.append(99) will add the number 99 to the end of the list. The list is now userAge=[21,5,23,24,25,99]
Which function do you use to delete the items from the list and how?
del listName[index of the item to be deleted].
For example, del userAge[2 it will yield
userAge=[21,4,24,25,99]
What would be the syntax for printing the third item from the list if the list is myList=[1,2,3,4,5,”Hello”]?
print(myList[2]) will yield 3
print the last item
print(myList[-1])
assign myList (from index 1 to 4) to myList2 and print myList2
myList2=myList[1:5]
print(myList2)
modify the second item in myList and print the updated list
myList[1]=22
print(myList)
append a new item to the list and print the updated list
myList.append(“How are you?”)
print(myList)
remove the sixth item from myList and print the updated list
del myList[-1] or del myList[5]
print(myList)
What is a Tuple?
Tuple is another form of a list where the initial values are fixed and cannot be modified throughout the program.
Give an example of a Tuple.
MonthsOfYear= (“Jan”,”Feb”,”Mar”,”Apr”,”May”,”June”,”July”,”Aug”,”Sept”,”Oct”,”Nov”,”Dec”)
Unlike a list, Tuple uses parentheses to define its values.
How do you modify the values in a Tuple?
MonthsOfYear[0]=”Jan”
MonthsOfYear[-1]=”Dec”
What is a dictionary?
It is a collection of related data PAIRS.
For example, if we want to store the username and their respective age, we can use dictionary for that
How do you declare a dictionary?
dictionaryName= {dictionary key:Data}
For example,
userNameAndAge={“Peter”:38, “John”:51, “Alex”:13, “Alvin”:NA}
The keys have to be unique and can’t be repeated. The name Peter can’t appear twice in the dictionary
What is an alternate method of declaring the dictionary?
Using the dict() method
userNameAndAge=dict(Peter=38,John=51,Alex=13,Alvin=NA)
We use parantheses and quotes aren’t need for defining the items.
How do you access the individual items in a dictionary?
to get John’s age you write
userNameAndAge[“John”] it will yield 51
How do you modify the items in a dictionary? Explain with an example
dictionaryName[dictionary key of item to be modified]= new data.
For example:
to modify “John”:51 we write
userNameAndAge[“John”]=21 , this will yield
userNameAndAge=dict(Peter = 38, John=21,Alex=13, Alvi= NA)
What is the syntax for declaring a dictionary with no items in it?
dictionaryName=dict() or dictionaryName={}
adding items to the empty dictionary at the end
userNameAndAge[“Joe”]=40
removing items from the dictionary
del dictionaryName[dictionary key]
del userNameAndAge[“Alex”]
Running a program with dictionaries. Dictionary can contain different kinds of data types #declaring a dictionary
myDict={“One”:1.35, 2.5:”Two Point Five”, 3:”+”, 7.9:2}
Are items in a dictionary in the same order as the way you declared them?
No, they are not. That’s why perhaps they don’t have index numbers.
print the item with key=”One”
print(myDict[“One”]
print the item with key =7.9
print(myDict[7.9])
modify the item with key=2.5 and print the updated list
myDict[2.5]=”Two and a half”
print(myDict)
add a new item and print the updated dictionary
myDict[“New Item”]=”I’m new”
print(myDict)
remove the item with key= “One”and print the updated dictionary
del myDict[“One”]
print(myDict)