Python Basics Flashcards
Set a variable to your name
var = “Aman”
Print “Hello, Treehouse”
print(“Hello, Treehouse”)
Which of these Python blocks is correctly formatted? a. def my_function() : { print(“Hello”) } b. def my_function() print(“Hello”)
c. def my_function() : print(“Hello”)
c. def my_function() :
print(“Hello”)
How would you name a variable that has two words like hello there?
Hello_there
What is print()?
Function
TF: Syntax is what we call the rules for writing in a programming language.
True
How many equal signs do you need to create a variable?
One
variable = value
TF: Using help() is cheating
False
To look up the documentation about the str class’s upper method, I’d use help()
help(str.upper)
TF: The Python shell is a great place to write the final version of your code.
False
To leave the Python shell, run the ____ () function.
exit()
I have a script named hello_treehouse.py. How would I run that?
python hello_treehouse.py
TF: Before we can run a Python script, we have to run a separate, distinct compilation step.
False
TF: When we’re writing a Python script, we have to include»_space;> in front of every line.
False
What keyword is used to delete a variable?
Del
What are whole numbers in Python called?
Integers or ints
What are numbers with decimals called?
Floats
What are the rules regarding adding, subtracting, multiplying, and dividing when using ints and floats?
When you add, subtract or multiply, you always get ints, unless you are using a float.
When you divide, you always get a float.
What happens when you divide by 0?
You receive a Zero Division Error.
What do you use when you need to convert a value to an integer or a float?
int()
float()
Can you use += or -= in python?
Yes
What are the 4 ways to make strings?
Single quotes, double quotes, triple quotes, str function
Can you multiply a string and a number?
Yes
How would use .format to add a number to a string? “There are {} people here”
Message = “There are {} people here”.format(6)
What’s unique about python lists?
They are kind of like arrays but can hold different types in them like strings, numbers, lists.
Lists are mutable
What are 2 great methods to add to lists?
append() → only for one item
my_list.append(6)
extend() → for more than one item
my_list.extend([4, 5, 6])
What method do you use for removing something from a list?
remove() → only for one item
my_list.remove(7)
Why can’t you run list(5)?
Because it is one item. A list must be iterable.
What is the purpose of the split method?
When we want to break a string into a list based on something that repeats in the string, we use the .split() method. It separates by whitespace (i.e. spaces, tabs, newlines, etc). Or you have to explicitly say by what.
“Hello there”.split() produces [“Hello”, “there”].
“name;age;address”.split(“;”) produces [“name”, “age”, “address”].
What is the purpose of the join method?
.join() method lets us combine all of the items in a list into a string (and only string items).
my_favorite_colors = [“green”, “black”, “silver”]
my_string = “My favorite colors are “
my_string + “, “.join(my_favorite_colors)
PROBLEM
available = “banana split;hot fudge;cherry;malted;black and white”
Use .split() to break the available string apart on the semi-colons (;). Assign this to a new variable sundaes.
Make a new variable named menu that’s set to “Our available flavors are: {}.”.
Then reassign the menu variable to use the existing variable and .format() to replace the placeholder with the new string in display_menu.
sundaes = available.split(‘;’)
menu = “Our available flavors are: {}.”
menu = “Our available flavors are: {}.”.format(‘, ‘.join(sundaes))
What are the index rules?
0 is the first index and then it goes by 1.
If you want the last index, use -1. If you want second to last, use -2. Etc etc.
If you have a list called alpha_list which has the follow [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] and you want to remove c, what can you do?
2 Ways to remove it
alpha_list.remove(‘c’)
del alpha_list[2]