Statements Flashcards
Write down the seven assignment operators. Know what they do
= \+= -= *= /= %= **=
There is an Acronym describing how operators are ordered. Which one?
PEMDAS
1. parentheses
2. exponents
3. multiplication
4. division
5. addition
6. subtraction.
Assign 1 to 4 variables. Whats the name of the operation?
Chained assignment
~~~
a=b=c=d= 1
~~~
What would this output? a = b = c = d + 10 = 6
- Syntax Error
- Assignment operator expects only one variable to be on the left-hand side of the equation
Give a full example of how to use the input variable
print("What is your name?") his_name = input() Keyboard: "David" print("Your Name is: ", his_name)
Define If statements
- conditional statement
- evaluate an expression
- If true, following code executed
- If false, following code skipped
What is the difference between a statement and expression?
- Statement is a line of code that the interpreter can execute
- Expression is a section of code that the interpreter evaluates to a certain value.
Write down the 6 boolean operators. What do they hold?
They hold a bool
- They hold a Bool
~~~
==
!=
>
>=
<
<=
~~~
Write a if statement asking the user if a player wants jersey number 22 or 24. Print the output
print("We have two choices of jersey numbers for Podolski") print("You decide:") print("22 or 40") jersey_num = input() if int(jersey_num) == 22: print("The Jersey number is 22") elif int(jersey_num) == 40: print("The Jersey number is 40") else: print("Dann nicht")
Do an Elif Statement inputing a number and evaluating if if number is negative, zero or positive and finish with a goodbye.
print("What is your number?: ") num = int(input()) if num < 0: print("Your number is negative") elif num == 0: print("Your number is zero") else: print("Your number is positive") print("Goodbye")
Define what a loop is
- One of the most important and powerful tools for programming
- Repeat the same set of instructions repeatedly until a specified condition is met.
What is a range function and which parameters are used?
- Creates range of numbers based on a set of parameters. Those parameters are as follows:
Start
* optional
Stop
* required
Step
* optional
What does the for loop in a range function? Write down Example
- Lets you loop through all the numbers in set created by range
for x in range(3, 5): print(x) Output: 3 4
Create a range from 5 - 10 with the step parameter set to 2. Whats the output?
for x in range(5, 10, 2): print(x) Output: 5 7 9
Where is the first and last item of index?
- 0
- n-1
Create a List of players and output that list to the screen as roster. Use the range function. The Output should be like this:
Player 1: Podolski
Player 2: Klinsmann
Player 3: Schweini
my_players = ["Podolski", "Klinsmann", "Schweini"] for x in range(3): print("Player " + str(x+1) + ": " + my_players[x])
Explain the basic function of a while Loop
- Keyword “while” followed by expression.
- If True, the code attached to the while loop will be executed over and over.
- If False, the while loop will exit and execution will resume at the point after the while loop and its associated code block.
Get the following outpout by creating a while loop: Player 1: Podolski Player 2: Klinsmann Player 3 Schweini
my_players = ["Podolski", "Klinsmann", "Schweini"] x = 0 while x < len(my_players): print("Player " + str(x+1) + ": " + my_players[x]) x += 1 print("End of roster")
Print the list of players with range function, but skip Klinsman. How?
- Continue Keyword
my_players = ["Podolski", "Klinsmann", "Schweini"] for x in range(3) if x == 1: continue print("Player " + str(x+1) + ": " + my_players[x]) print("End of roster"
How to loop through all even numbers from 2 to 20
for x in range(2, 21, 2)
What is an iterator? Explain and Demonstrate
- Creates iteration object, captured in variable
- First call of function return first list and so on
- Use next()
my_list = [4, 8, 15, 16, 23, 42] my_iterator = iter(my_list) print(next(my_iterator))
What is a Stolteration error and how to avoid. Demonstrate example multiplying numbers in list and storing result into new list. There are two ways of doing so, use both
First Method
- Occurs when trying to iterate beyond bounds of list, tupil, or dictionary
- Mitigation: Use iterator in conjuntion with for loop
my_numbers = [2, 3, 5, 8, 5] my_new_numbers= [] for x in my_numbers: my_new_numbers.append(x*x) print(my_new_numbers) Second Method my_numbers = [2, 3, 5, 8, 5] my_new_numbers= [n*n for n in my_numbers]
Create a list of squares from an original list, but only if the original value is less then 20. Use the shortest format.and the long format
my_numbers = [4, 8, 15, 16, 23, 42] my_new_numbers = [n*n for n in my_numbers if n < 20]
I have a list called my_list
that contains four values (1, 2, 3, and 4). I want to create a new list called my_new_list
from my existing list my_list
. I want my_new_list
to contain only the even values from my_list
, and I want each of those values to be multiplied by 100. Demonstrate
my_list = [1, 2, 3, 4] new_list = [num*100 for num in my_list if num % 2 == 0] print(new_list)