Ch 2. Flashcards
What does ending .py mean?
The file is a python file
What is a variable?
A symbolic representation of a value, can think of as a label for a value
Rules for naming and using variables
- Variable names can only contain letters, numbers and underscores.
- Can start with a letter or underscore but NOT a number.
- Spaces are not allowed
- Do not use Python keywords and function names. (aka reserved words)
- Names should be short but descriptive.
- Be careful using lowercase l and uppercase O because it can be confused with 1 and 0.
What is a string?
a series of characters
change to upper case for first letters only?
variable_name.title()
all upper case?
variable_name.upper()
all lower case?
variable_name.lower()
format strings
Python formats the string by replacing the name of any variable in braces with its value:
f”Hello, {variable_name}!”
\t
adds tab
\n
adds new line
How to remove whitespace temporarily? Permanently?
Temp: all: variable_name.strip() for right: variable_name.rstrip() or for left: variable_name.lstrip() Perm: You have to associate the stripped value with the variable name.
When does a syntax error occur?
When python doesn’t recognize a section of your program as valid code.
How do you assign values to more than one variable using a single line of code?
Using multiple assignment: x, y, z = 0, 0, 0
How do you indicate a constant?
Using all caps. For example: MAX_CONNECTIONS = 5000