Chapter 4: Data Types in Python Flashcards

1
Q

What are integers in Python, and how do you declare them?

A

Integers are numbers with no decimals such as -4,4. The declare an integer write variableNAme= initial value.
userAge=30

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

What is float and how do you declare it?

A

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

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

What is a string? How do you declare it?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you combine multiple substrings? Provide examples

A

Using the concatenate”+” sign. For example, “Amy” + “Eric” will give us Amy Eric

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

What are built-in string functions?

A

Python includes a number of built-in functions to manipulate strings. A function is a block of reusable code that performs a certain task.

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

Provide examples of built-in string functions

A

upper() method which can be used to capitalize all the letters in a string. For example, ‘Amy’.upper() will give us the string ‘AMY’.

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

What is the syntax for the % operator?

A

“string to be formatted”%(values or variables to be inserted into string, separated by commas)

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

What are the three parts to the % operator?

A

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.

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

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)

A

The price of this Apple laptop is 1299 USD and the exchange rare is 1.23 USD to 1 EUR

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

What are %s, %d and %4.2f?

A

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

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

What is the syntax for format () method?

A

“string to be formatted”.format()

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

Provide and explain an example of the format () method.

A

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.

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

What if you don’t want to format the string?

A

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

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

For the format method where does the position start from?

A

ZERO

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

What will the below code yield?

message3=’ and {:d}.format{1.23, 12}

A

6spaces1.23 and 12

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

What will the below code yield?

message4=”{}.format(1.23)

A

1.23

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

What is Type Casting?

A

Changing the format of the data types from integer to float to String is called type casting

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

What are the three built-in functions that allow us to do type casting?

A

Int(), Float(), Str()

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

What does INT() do?

A

It converts float and an appropriate string to Integer. For example, INT(2.34) will yield 2, INT(“2”) it will yield 2

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

What does float() do?

A

It converts integer and appropriate string to float. For example, float(2) or float (“2”), we will get 2.0

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

What does str() do?

A

It converts a float or integer to a string. For example, str(2.1) will yield “2.1”

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

What is a list?

A

It refers to collection of data that is related. Instead of storing this data separately, we can store them as a list.

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

How do you declare a list?

A

listName=[initial values]

For example, userAge=[21,22,23,24,25]

24
Q

What kind of brackets are used to store a list?

A

Square brackets

25
Q

How do you declare an empty list and which function is used to add items to it?

A

userAge=[]

We use append() function to add items to this list

26
Q

What are indexes?

A

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

27
Q

How do you assign the complete or a part of the list to a variable? The list being
userAge = [21,22,23,24,25]

A

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.

28
Q

What is the notation [2:4] called(taking cue from the previous card), and what is a stepper? Explain.

A

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]

29
Q

What are the defaults for the slice notations?

A

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)

30
Q

How do you modify the items in the list?

A

listName[index of item to be modified]= new value
For example
userAge[1]=5 yields
userAge=[21,5,23,24,25]

31
Q

Which function do you use to add items to the list and how?

A

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]

32
Q

Which function do you use to delete the items from the list and how?

A

del listName[index of the item to be deleted].
For example, del userAge[2 it will yield
userAge=[21,4,24,25,99]

33
Q

What would be the syntax for printing the third item from the list if the list is myList=[1,2,3,4,5,”Hello”]?

A

print(myList[2]) will yield 3

34
Q

print the last item

A

print(myList[-1])

35
Q

assign myList (from index 1 to 4) to myList2 and print myList2

A

myList2=myList[1:5]

print(myList2)

36
Q

modify the second item in myList and print the updated list

A

myList[1]=22

print(myList)

37
Q

append a new item to the list and print the updated list

A

myList.append(“How are you?”)

print(myList)

38
Q

remove the sixth item from myList and print the updated list

A

del myList[-1] or del myList[5]

print(myList)

39
Q

What is a Tuple?

A

Tuple is another form of a list where the initial values are fixed and cannot be modified throughout the program.

40
Q

Give an example of a Tuple.

A

MonthsOfYear= (“Jan”,”Feb”,”Mar”,”Apr”,”May”,”June”,”July”,”Aug”,”Sept”,”Oct”,”Nov”,”Dec”)

Unlike a list, Tuple uses parentheses to define its values.

41
Q

How do you modify the values in a Tuple?

A

MonthsOfYear[0]=”Jan”

MonthsOfYear[-1]=”Dec”

42
Q

What is a dictionary?

A

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

43
Q

How do you declare a dictionary?

A

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

44
Q

What is an alternate method of declaring the dictionary?

A

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.

45
Q

How do you access the individual items in a dictionary?

A

to get John’s age you write

userNameAndAge[“John”] it will yield 51

46
Q

How do you modify the items in a dictionary? Explain with an example

A

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)

47
Q

What is the syntax for declaring a dictionary with no items in it?

A

dictionaryName=dict() or dictionaryName={}

48
Q

adding items to the empty dictionary at the end

A

userNameAndAge[“Joe”]=40

49
Q

removing items from the dictionary

A

del dictionaryName[dictionary key]

del userNameAndAge[“Alex”]

50
Q
Running a program with dictionaries. Dictionary can contain different kinds of data types
#declaring a dictionary
A

myDict={“One”:1.35, 2.5:”Two Point Five”, 3:”+”, 7.9:2}

51
Q

Are items in a dictionary in the same order as the way you declared them?

A

No, they are not. That’s why perhaps they don’t have index numbers.

52
Q

print the item with key=”One”

A

print(myDict[“One”]

53
Q

print the item with key =7.9

A

print(myDict[7.9])

54
Q

modify the item with key=2.5 and print the updated list

A

myDict[2.5]=”Two and a half”

print(myDict)

55
Q

add a new item and print the updated dictionary

A

myDict[“New Item”]=”I’m new”

print(myDict)

56
Q

remove the item with key= “One”and print the updated dictionary

A

del myDict[“One”]

print(myDict)