Statements Flashcards

1
Q

Write down the seven assignment operators. Know what they do

A
=
\+=
-=
*=
/=
%=
**=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

There is an Acronym describing how operators are ordered. Which one?

A

PEMDAS
1. parentheses
2. exponents
3. multiplication
4. division
5. addition
6. subtraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Assign 1 to 4 variables. Whats the name of the operation?

A

Chained assignment
~~~
a=b=c=d= 1
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What would this output?
a = b = c = d + 10 = 6

A
  • Syntax Error
  • Assignment operator expects only one variable to be on the left-hand side of the equation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give a full example of how to use the input variable

A
print("What is your name?")

his_name = input()
Keyboard: "David"

print("Your Name is: ", his_name) 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define If statements

A
  • conditional statement
  • evaluate an expression
  • If true, following code executed
  • If false, following code skipped
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between a statement and expression?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write down the 6 boolean operators. What do they hold?

A

They hold a bool

  • They hold a Bool
    ~~~

==
!=
>
>=
<
<=
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a if statement asking the user if a player wants jersey number 22 or 24. Print the output

A
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") 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Do an Elif Statement inputing a number and evaluating if if number is negative, zero or positive and finish with a goodbye.

A
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")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define what a loop is

A
  • One of the most important and powerful tools for programming
  • Repeat the same set of instructions repeatedly until a specified condition is met.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a range function and which parameters are used?

A
  • Creates range of numbers based on a set of parameters. Those parameters are as follows:

Start
* optional

Stop
* required

Step
* optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the for loop in a range function? Write down Example

A
  • Lets you loop through all the numbers in set created by range
for x in range(3, 5):
    print(x)
    
Output:

3
4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create a range from 5 - 10 with the step parameter set to 2. Whats the output?

A
for x in range(5, 10, 2):
    print(x)

Output:
 5 
7
9
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Where is the first and last item of index?

A
  • 0
  • n-1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A
my_players = ["Podolski", "Klinsmann", "Schweini"]

for x in range(3):
    print("Player " + str(x+1) + ": " + my_players[x])
    
17
Q

Explain the basic function of a while Loop

A
  • 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.
18
Q

Get the following outpout by creating a while loop: Player 1: Podolski Player 2: Klinsmann Player 3 Schweini

A
                 

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")                                      
19
Q

Print the list of players with range function, but skip Klinsman. How?

A
  • 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"                                      
20
Q

How to loop through all even numbers from 2 to 20

A
for x in range(2, 21, 2)
21
Q

What is an iterator? Explain and Demonstrate

A
  • 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))
22
Q

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

A

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]
23
Q

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

A
my_numbers = [4, 8, 15, 16, 23, 42]
my_new_numbers = [n*n for n in my_numbers if n < 20]
24
Q

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

A
my_list = [1, 2, 3, 4]
new_list = [num*100 for num in my_list if num % 2 == 0]
print(new_list)