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(‘@‘)