Grok Worksheet 2 Flashcards

1
Q

What is the structure of an expression in Python?

A

operand_1 operator operand_2

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

Give an example of a symbol that does not form an expression.

A

Assignment (‘=’) is not an expression, so it can not be used like y = 1 + ( x = 4)

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

List the three array-type operands in Python.

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

What type is used to represent positive or negative whole numbers?

A

Integers: int

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

What type is used to represent positive or negative numbers with a decimal component?

A

Floating point: float

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

Other than using a decimal, how else can we create a floating point number?

A
Using scientific notation:
>>> print(1e4) # 1e4 = 1 x 10 ** 4
10000.0
>>> print(1.23e-2)
0.0123
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Fill in and explain the result:

|&raquo_space;> print(3 / 4, type(3/4))

A

0.75

Python will implicitly convert integers to floats when using (true) division on integers.

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

How can we modify the following line to yield an integer and type = int:
»> print(3 / 4, type(3/4))

A

> > > print(3 // 4, type(3 / /4)) # Floored division
0
print(int(3 / 4), type((int3 /4))) # Floored division
0

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

What is ‘type casting’?

A

Converting one type into another, if possible. Eg:
»> a = str(123)
‘123’
»> type(a)

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

> > > print(int(“32.7”)) # What will this return?

A

> > > print(int(“32.7”)) # ‘.’ is not a number

ValueError: invalid literal for int() with base 10: ‘32.7’

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

> > > print(float(“32.7”)) # What will this return?

A

> > > print(float(“32.7”)) # Unlike int(), float() can handle ‘.’
32.7

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

> > > print(str(123) + str(456)) # What will this return?

A

> > > print(str(123) + str(456))

123456

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

True or false: modulo and floored division only work on type int.

A

False, they also work on type float.

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