Python Syntax Flashcards
What symbol tells Python to interpret the following as a comment?
#
What function tells a computer to talk?
print()
The message should be surrounded by “ “ or ‘ ‘
Be consistent with whichever chosen
What are the printed words as a result of the print() function referred to as?
Output
What are blocks of text called?
Strings
Does it matter which kind of quotation is used for a string?
No, either “” or ‘’
Just be consistent with whichever chosen.
How do you assign variables?
By using the equal sign (=)
message_string = “Hello there” # Prints “Hello there” print(message_string)
Can variables start with numbers?
No, but they can have numbers after the first letter.
Can variables have spaces or symbols other than an underscore ( _ )?
No
What does Python refer to mistakes as?
Errors.
It will point to the location where an error occurred with a ^ character.
What are two common errors encountered while writing Python?
SyntaxError
NameError
What does SyntaxError mean?
There is something wrong with the way your program is written
- punctuation that does not belong, a command where it is not expected, or a missing parentheses can all trigger SyntaxError.
When does a NameError occur?
When the Python interpreter sees a word it does not recognize.
Code that contains something that looks like a variable but was never defined will throw a NameError.
Or when the quotes are never used but should have been.
What does a SyntaxError: EOL mean?
A string wasn’t closed, or wasn’t closed with the same quote-character that started it.
What is the modulo operator?
A companion to division.
It is indicated by % and returns the remainder after division is performed.
What is a float?
A number with a decimal point.
You can define a float with numbers after the decimal point or by just including a decimal point at the end.
You can also define a float using scientific notation, with e indicating the power of 10.
How do you write a string that spans multiple lines?
Use triple quotes
“””136 Whowho Rd
Apt 7
Whosville”””
This can also be used for long comments if you don’t assign it as a variable.
What is a Boolean?
A variable that is either true or false.
A value of true corresponds to an integer value of 1.
A value of false corresponds to a value of 0.
How would you convert an integer to a string?
str()
Example:
age = 13
print “I am” + str(age) + “years old!”
For a reversal, we use int()
int_addition = int(number1) + int(number 2)
How do you deal with apostrophes in strings?
Put a backlash \ before the apostrophe.
“There\’s a snake in my boots!”