Python Fundamentals Flashcards
(74 cards)
What are Python files called?
Python modules
Python files have a .py extension
How do you write a single line comment in Python?
Pound sign: # comment
Correct syntax is a space between pound and text
How do you write multiline comment in Python?
””” Triple Quotes “””
This is most often used in function or class documentation.
How do you use string interpolation rather than the string format function?
name = “Python”
machine = “HAL”
f”Nice to meet you {name}. I am {machine}”
Note: You must put the f in front to let the interpreter know you want to use string interpolation.
What does Python use in lieu of curly braces for code blocks?
Indentations to indicate blocks of code.
What is the equality operator in Python?
Double equals ==
What has a Truthy value?
Any number other than 0
A string as long as it is not an empty string
What has a Falsy value?
The number 0
The keyword None
What is a list like?
How do you define a list?
A list is very similar to an array in other languages. It is even indexed the same.
You define a list by adding empty square brackets to a variable name:
student_names = []
What is the separator for a list?
The comma: student_names = [“Mark”, “Katarina”, “Jessica”]
How do you access list elements?
Use bracket notation:
student_names[0] == “Mark”
list name, then index.
How do you access the last item in a list?
You would use a -1 index.
student_names[-1] == “Jessica”
Second to last would be -2, and so on…
Note: Doesn’t work on a zero index going backwards. No such thing as neg. zero.
How do you append an item to a list?
Use the .append() function:
student_names.append(“Homer”)
How do you check to see if an item is in a list?
You just write the following:
“Mark” in student_names
This will evaluate to True
How do you see how many items are in a list?
Use the len() function: len(student_names) == 4
Can use len for other items, not just exclusive to lists.
How do you delete an element in a list?
use the del keyword:
del student_names[2]
Note: Elements in list will shift to the left.
If you wanted to slice the first element out of a list, how would you do that?
Use the following syntax:
student_names[1:]
This skips the first element and returns everything else.
Note: This will not modify the list, just temporarily ignore elements. The list is still intact
If you want to skip the first 2, you use [2:] and so on…
How do you ignore the first and last elements in a list?
Use the following syntax:
student_names[1:-1]
Remember, the last element is not zero indexed because you can’t have a negative zero number, so it’s -1
Note: This will not modify the list, just temporarily ignore elements. The list is still intact
What are the 2 main loops in Python?
For Loop
While Loop
What is the general syntax of a For loop?
for name in student_names:
print(“Student name is {0}”.format(name))
Note the colon at the end of the line “:” - very important
“name” can be any variable: x, name, boo, whatever…it just represents each item in the list that is being iterated over.
read as: for every “name” in the student_names list, do this…
Works like a foreach loop in other languages.
How do you use the range function?
Syntax:
x = 0
for index in range(10):
x += 10
print(“The value of x is {0}”.format(x))
range(10) would mean “I want to execute this code ten times”.
The index and (10) numbers are index numbers [0, 1, 2…10]
Range supports more arguments:
range(5, 10): Start at 5 and end at 10 [5, 6, 7, 8, 9]
range(5, 10, 2): Start at 5, end at 10, but increment by 2 [5, 7, 9]
What keyword do you use to stop the program execution at any point in your loop?
You use the break keyword:
ex:
for name in student_names:
if name == “Homer”:
print(“Found him! “ + name)
break
print(“Currently testing “ + name)
Break will exit the loop when the criteria tests true.
When would you use the continue keyword?
When you want to skip an item in your loop.
Ex.
for name in student_names:
if name == “Bort”:
continue
print(“Found him! “ + name)
print(“Currently testing “ + name)
This will skip the name “Bort” in the list and continue through each iteration.
It says “stop executing code and continue onto the next iteration”
How do you increment in a while loop?
x += 1
This will increment by 1
+= 2 will increment by two, and so on…