Grok Worksheet 2 Flashcards
What is the structure of an expression in Python?
operand_1 operator operand_2
Give an example of a symbol that does not form an expression.
Assignment (‘=’) is not an expression, so it can not be used like y = 1 + ( x = 4)
List the three array-type operands in Python.
Tuples = tuple() : (1, 2, 3) or ('apple', 3.1415) Lists = list() : [1,1,2,3,5] or ['a','b','c','d'] Dictionaries = dict() : {"name" : "Bob", "age" : 45}
What type is used to represent positive or negative whole numbers?
Integers: int
What type is used to represent positive or negative numbers with a decimal component?
Floating point: float
Other than using a decimal, how else can we create a floating point number?
Using scientific notation: >>> print(1e4) # 1e4 = 1 x 10 ** 4 10000.0 >>> print(1.23e-2) 0.0123
Fill in and explain the result:
|»_space;> print(3 / 4, type(3/4))
0.75
Python will implicitly convert integers to floats when using (true) division on integers.
How can we modify the following line to yield an integer and type = int:
»> print(3 / 4, type(3/4))
> > > print(3 // 4, type(3 / /4)) # Floored division
0
print(int(3 / 4), type((int3 /4))) # Floored division
0
What is ‘type casting’?
Converting one type into another, if possible. Eg:
»> a = str(123)
‘123’
»> type(a)
> > > print(int(“32.7”)) # What will this return?
> > > print(int(“32.7”)) # ‘.’ is not a number
ValueError: invalid literal for int() with base 10: ‘32.7’
> > > print(float(“32.7”)) # What will this return?
> > > print(float(“32.7”)) # Unlike int(), float() can handle ‘.’
32.7
> > > print(str(123) + str(456)) # What will this return?
> > > print(str(123) + str(456))
123456
True or false: modulo and floored division only work on type int.
False, they also work on type float.