Python Basics Flashcards
How do you print “Hello World!” in Python?
print(“Hello World!”)
What is the ‘console’ in Python?
The console displays whatever you put inside print(). The console here is like the Command Line or Terminal on your computer.
What is print() in Python? Is it a Function, String, or Variable?
print() is a Function in Python
What is it called when we put text inside quotes in Python?
Any text inside of single or double quotes in Python is called a String. We can use the print Function to display Strings in the Console with Python.
We can also combine strings by adding them together:
print (“Hello” + “world!”)
What will the above display in the console when run in Python?
It will display: Helloworld!
This is because we forgot to put a space at the end of “Hello” or the beginning of “world!”
Python doesn’t add any whitespace when we combine strings. We have to add it ourselves.
We could also add whitespace this way:
print(“Hello” + “ “ + “world!”)
Hello world!
Sometimes it is useful to know the number of characters in a string. What function in Python can we use for this?
We can use the function len() for this:
print(len(“Hello world!”))
When run, will display on the console as:
12
How can we add the lengths (the number of characters) of two strings together in Python?
We can use the function len() in this way:
print(len(“Hello”) + len(“world!”))
When run, will display on the console as:
11
Because we did not put a space between “Hello” and “world!”, the strings are a total of 11 characters in length, instead of 12 with a whitespace.
What are strings surrounded by in Python?
Single or double quotes
How do we display strings to the console in Python?
The print() function displays strings to the console.
What will this program output?
print(len(“Hel” + “lo!”))
When run, this program will display on the console as:
6
Remember, this is because len() counts all the characters in a string, even special characters like “!”.
Can we add whitespace to the beginning or end of a string in Python?
Yes. For instance:
print(“Hello “ + “world!”)
or
print(“Hello” + “ world!”)
Will both display in the console as:
Hello world!
What do variables allows us to do in Python?
Variables let us store values in a name. Then we are able to use that name later to refer to the value.
For instance, the following program:
animal = “Lion”
print(animal)
When run, will display in the console as:
Lion
Can we combine variables in Python?
Can we print variables just like strings in Python?
Yes and yes.
For instance, the following program:
lion = “Lion”
zebra = “Zebra”
print(lion + zebra)
When run, will display in the console as:
LionZebra
What do we need to be careful of when using variables in Python?
We need to be careful to never use a variable before we define it.
For instance, the following program:
print(animal)
animal = “Zebra”
When run, will display in the console as:
NameError: name ‘animal’ is not defined
Can we update variables after we’ve already assigned them a value in Python?
Yes. Once we’ve assigned a value to a variable, we can update the variable by setting it to a new value.