Basics of Types Flashcards

1
Q

What are the three basic number types and their Python shorthand

A

Integers = int
Real numbers = float
Complex numbers = complex

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

How do you find out the type of an object?

A

Function: type(obj)

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

What is a string and what is the Python shorthand?

A

String is a sequence of characters, normally enclosed in single or double quotation marks.

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

How do you escape characters in strings?

A

Backslash - \
Eg,
» print(“My name is "Levi"”)
My name is “Levi”

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

How do you insert a tab into a string?

A

\t

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

How do you insert a newline into a string?

A

\n

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

How can you avoid having to escape special characters?

A

Triple quotation marks (‘’’ or “””)

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

Where the three main/basic binary operators that can be applied to strings?

A

+ (concatenation)
* (repeat string N times)
in (subset of?)

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

Give an example of the ‘in’ binary operator for strings?

A

> > print(‘z’ in ‘Zebra’)

True

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

How does assignment work?

A
>>> a = 1
>>> print(a)
1
The RHS is evaluated and then assigned to the LHS, allowing self-referential assignment:
>>> a = a + 2
>>> print(a)
3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is ‘stacking’ assignment variables?

A
>>> a = b = c = 2
>>> print(a)
2
>>> print(b)
2
>>> print(c)
2
>>> a = b = c = a + b + c
>>> print(a," ",b," ",c)
6 6 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What characters can a variable name start with?

A

(a-zA-Z) or underscore (_)

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

What characters can a variable name contain?

A

Alphanumeric (0-9a-zA-Z) and underscore (_)

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

Does case matter when naming variables?

A

Yes, apple != Apple, they are different variables

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

What are reserved words?

A

Operators, literals and built-in functions that cannot be used for variable names (eg in, print, not, str)

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

Calculate the ith Fibonacci number using only three variables.

A

a = 1
b = 2
c = a + b
Redefine a and b sequentially

17
Q

How do we ‘cast’ a literal or variable as a different type?

A

Use functions of the same name as the type into which we want to cast: int(), float(), str(), complex()…

18
Q

What does abs() do?

A

Returns the absolute value of the operand

19
Q

What does len() do?

A
Return the length of the ITERABLE operand
Eg,
>>> len('string')
6
>>> len([1,2,3])
3
20
Q

Given num containing an int, calculate the number of digits in it.

A

> > > len(str(num))

21
Q

What are the four basic types in this deck?

A

str, int, float, complex

22
Q

How are string specified?

A

Using single or double quotation marks (‘ or “)

23
Q

What is the difference between a literal and a variable?

A

A literal is fixed, a variable is not. A variable is a stored memory location for an assigned value.