Lecture 2 Flashcards
Variables
Containers for storing data values in computer memory
What are the common python variable types?
Int
Float
Str
Bool
Define int
Positive or negative integers
ex: x = 10
Define float
Positive or negative decimal numbers
ex: y = 3.14
Define str
Strings are a single character or sequence of characters.
ex: letter = ‘x’
ex: name = “Alice”
Define bool
Boolean, true or false
ex: is_active = True
Rules variable names MUST follow
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.
Variable naming conventions according to the python style guide:
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.
Why variables?
Store intermediate results.
Represents values symbolically to avoid having to change their values in multiple places within a program.
How do variables change during the runtime of a program?
Variables can be reassigned values. They change in chronological order.
Can you mix integers and float variables?
Yes
Can you mix types of variables?
No, this can result in an unsupported operand
Best practices for single and double quotes:
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\””
What are triple quotes used for
Multi line strings or multi line comments
What distinguishes a multi line string from a multi line comment
Whether it’s assigned to a variable or not. Multi line strings are, multi line comments are not.
What types of data can be included in strings?
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
To determine in python, what a variable is, you can use what function?
Type (x) -> which returns <class ‘ variable type’>
Or
Isinstance(x,int) -> returns True or false for if its an integer
What is the code for an instance function to check if something is true or false?
Isinstance(x, int) , compares x to the following data type
What is type casting?
Explosively converting one data type to another. It is sometimes necessary to ensure compatibility between operations.
How to convert a float to an int
x = int(3.5)
How to convert a int to a string
y= str(123)
How to convert a string to a float
z=float(“2.5”)
How to convert an int to Unicode
a= chr(64)
How to convert Unicode to int
b = ord(‘@‘)
When it typecasting from a float to an int what happens to the number?
It truncates, it does not round
ex: int(-3.9) -> -3
Rounding function
round(-3.9) -> returns -4
Can all type or variables be converted to one another?
Nope
What are the only two numbers a computer can understand
0 and 1
How does binary work?
Binary is a base two number system.
11010.01_2 means 26.25.
How to find binary representation of a decimal number:
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
Hexadecimal prefix and base
Prefix 0x base 16
What is the format for the print command and what does it do
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.
F strings when printing
Strings with variables.
print(f” {name} is {age} years young.”}
How to get print to not add a new line
print(“statement”, end=“”)
print(“statement”)
How to add tabs and new lines to strings
\t for tab and \n for new line before the phrase you want on a new line or tab
What does division always return
A float
and
true if both conditions are true, false otherwise.
or
True if at least one condition is true, false otherwise
not
Reverses the boolean value
Falsy variables
0, 0.0, ‘ ‘, [],{}, none, false
And
Returns the first falsy value it encounters. If all truethy it returns the last value.
Or
Returns the first truthy value it encounters. If all values are falsy it returns the last value
Identity operators
‘Is’ and ‘is not’ : all identity operators, and check the memory location of two variables. They do not compare values.
Membership operators
‘In and not in’ check to see if a character is in a string