Lecture 2 Flashcards

1
Q

Variables

A

Containers for storing data values in computer memory

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

What are the common python variable types?

A

Int
Float
Str
Bool

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

Define int

A

Positive or negative integers
ex: x = 10

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

Define float

A

Positive or negative decimal numbers
ex: y = 3.14

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

Define str

A

Strings are a single character or sequence of characters.
ex: letter = ‘x’
ex: name = “Alice”

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

Define bool

A

Boolean, true or false
ex: is_active = True

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

Rules variable names MUST follow

A

1) Starts with a letter, (lowercase, a through Z, or uppercase a through Z)
2) can contain letters and numbers and be of any length
3) cannot contain special characters, such as@ or !
4) names are case sensitive. Var1 is not equal to var1.
5) be a reserved word, such as return, class, etc.

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

Variable naming conventions according to the python style guide:

A

1) be informative, and unambiguous, but not too long
2) be followed by a meaningful comment in line where it is first introduced
3) use lowercase letters with words separated by underscore. This is called snake_case. (the exception: a variable that is constant and should not be changed. Should have an all caps NAME.)
4) should not start with a double underscore (__)
5) not to be a built-in python function, such as int.

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

Why variables?

A

Store intermediate results.
Represents values symbolically to avoid having to change their values in multiple places within a program.

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

How do variables change during the runtime of a program?

A

Variables can be reassigned values. They change in chronological order.

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

Can you mix integers and float variables?

A

Yes

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

Can you mix types of variables?

A

No, this can result in an unsupported operand

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

Best practices for single and double quotes:

A

1) use single quotes when the string contains double quotes. ‘He said, “Hello”’
2) use double quotes when the string contains single quotes: “It’s a beautiful day”
3) use escape characters () when the string contains both single and double quotes. “ he said, \” it’s fine\””

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

What are triple quotes used for

A

Multi line strings or multi line comments

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

What distinguishes a multi line string from a multi line comment

A

Whether it’s assigned to a variable or not. Multi line strings are, multi line comments are not.

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

What types of data can be included in strings?

A

Strings are sequences of Unicode characters. Unicode supports over 1.1 million different symbols, including:
-Basic letters, symbols, tabs, lines, Latin, and Latin supplements.
-emojis
-script such as Cyrillic, Arabic, Chinese, etc.
-Math symbols
-ancient script such as Egyptian hieroglyphics
-unassigned code points for symbols

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

To determine in python, what a variable is, you can use what function?

A

Type (x) -> which returns <class ‘ variable type’>
Or
Isinstance(x,int) -> returns True or false for if its an integer

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

What is the code for an instance function to check if something is true or false?

A

Isinstance(x, int) , compares x to the following data type

19
Q

What is type casting?

A

Explosively converting one data type to another. It is sometimes necessary to ensure compatibility between operations.

20
Q

How to convert a float to an int

A

x = int(3.5)

21
Q

How to convert a int to a string

A

y= str(123)

22
Q

How to convert a string to a float

A

z=float(“2.5”)

23
Q

How to convert an int to Unicode

A

a= chr(64)

24
Q

How to convert Unicode to int

A

b = ord(‘@‘)

25
Q

When it typecasting from a float to an int what happens to the number?

A

It truncates, it does not round
ex: int(-3.9) -> -3

26
Q

Rounding function

A

round(-3.9) -> returns -4

27
Q

Can all type or variables be converted to one another?

28
Q

What are the only two numbers a computer can understand

29
Q

How does binary work?

A

Binary is a base two number system.
11010.01_2 means 26.25.

30
Q

How to find binary representation of a decimal number:

A

1) calculate n mod 2 and save this value
2) replace n by n/2, truncating the fractional part
3) repeat steps 1 and 2 until the new n is 0
4) Write the values that you have saves (0 or 1) from left to right. This is the binary representation

31
Q

Hexadecimal prefix and base

A

Prefix 0x base 16

32
Q

What is the format for the print command and what does it do

A

print() output, a string to the command line/terminal. Print statements are used to update the user on the status of a program, display data to the user, debugging.

33
Q

F strings when printing

A

Strings with variables.
print(f” {name} is {age} years young.”}

34
Q

How to get print to not add a new line

A

print(“statement”, end=“”)
print(“statement”)

35
Q

How to add tabs and new lines to strings

A

\t for tab and \n for new line before the phrase you want on a new line or tab

36
Q

What does division always return

37
Q

and

A

true if both conditions are true, false otherwise.

38
Q

or

A

True if at least one condition is true, false otherwise

39
Q

not

A

Reverses the boolean value

40
Q

Falsy variables

A

0, 0.0, ‘ ‘, [],{}, none, false

41
Q

And

A

Returns the first falsy value it encounters. If all truethy it returns the last value.

42
Q

Or

A

Returns the first truthy value it encounters. If all values are falsy it returns the last value

43
Q

Identity operators

A

‘Is’ and ‘is not’ : all identity operators, and check the memory location of two variables. They do not compare values.

44
Q

Membership operators

A

‘In and not in’ check to see if a character is in a string