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.