1/69
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
How do you separate the text of one code on multiple lines in a print statement?
By adding 3 quotes do in a print statement print("""text""")
How do you type in the console?
By using input command.
Structure:
What is a variable in coding?
a named piece of computer memory
myName is the variable
What is concatenating?
Calling a variable into your print statement.
Concatenates only works with the input command.
How do you add color to text
What is an if statement?
These statements are a bit like asking a question. You are telling the computer: if something is true, then do this specific block of code. Double equals (==
) is asking the computer to compare if these two things are exactly the same.
How do you write an if statement?
How do you concatenate?
What is the else statement?
If the condition is not met with the if
statement, then we want the computer to do the else
part instead. Likewise, if the condition is met in the if
statement, then the else
bit is ignored by the computer. The else
statement must be the first thing unindented after the if
statement and in line with it
How do you write an else statement?
The else statement must be indented the same as the if statement.
What is the elif command?
The elif
command (which stands for 'elseif') allows you to ask as many questions as you want.
Where do you write an elif statement?
The elif command must go in between if
and else
and have the same indentation.
The print
statements in your elif
command need to line up with the indent of the other print
statements.
What is Nesting?
Nesting is where we put an if
statement within an if
statement using the power of indenting.
How do you use Nesting?
The second if
statement within the first if
statement must be indented and its print
statement needs to be indented one more time.
What is Casting?
If
statements that support more than ==
. They support inequality symbols as well.
Casting is where we explicitly tell the computer that what we are typing is a number and not a letter.
How do you write Casting?
Numbers shouldn't have quotes around it
This is because the way input
works behind the scenes is it always assumes what you are typing is text (or a string) and gets stored as a variable in ""
.
What is int?
int makes the computer recognize whole numbers
How do you use int?
The way the computer will read this code is by starting with what is in brackets first ("your score"). The computer thinks this is text because of the ""
. When we add int
, we are telling the computer, "This is not text. Please convert this to a whole number." Now the variable, myScore
will store the answer as a number.
What is float?
float makes the computer recognize decimal numbers.
How do you make the computer do math?
How do you make the computer round numbers?
How do you make the computer repeatedly run the code?
By using a while loop
A while
loop allows your code to repeat itself based on a condition you set.
How do you write a write a while loop?
How do you avoid infinite loops while using a while loop?
What is != in python?
!= means (does not equal)
What is a while True loop?
A while True
loop is an infinite loop that continues to execute its block of code indefinitely until a break
statement is encountered within the loop's body
When is it best to use a while loop?
A while
loop is best used when you don't know the exact number of times you need to repeat a block of code, but you have a condition that must remain true for the loop to continue. It's ideal for situations where the loop needs to keep going until a certain event occurs or a specific condition is no longer met.
How do you stop a while True loop from infinitely looping?
There is a way to stop the loop with the word break
. This exits the loop and stops all code at that point. Even if there is more code written after break
that is inside the loop.
After break
, the computer jumps out of the loop to the next unindented line of code.
How do you use the break command?
When is it best to use the continue command?
The continue
command stops executing code in the loop and starts at the top of the loop again. Essentially, we want to kick the user back to the original question.
How do you write the continue command?
How do you stop a program after the continue command?
The exit()
command completely stops the program and it will not run any more lines of code.
-
The exit()
command completely stops the program and it will not run any more lines of code.
When is it best to use a while loop?
A while
loop is perfect to use when we don't know how many times we want the loop to repeat.
What is the structure of a for loop?
What can you use the range function for?
starting value: what number do you want to start with?
ending value: the number after the number you want to end with (example: if you type 10 as the ending value, the computer will count until 9)
increment: How much should it increase by every time it loops? (example: Do you want to count by 1s, 5s, 10s?)
What are libraries?
Libraries are collections of code that other people have written. Video games often use massive libraries (for example: game engines) to create the epic water reflections, 3-D graphics, etc.
What is a Subroutine?
A subroutine tells the computer that a piece of code exists and to go run that code again and again...
-
Subroutines work like a recipe in a cookbook. If you want to know how to make a cake, you don't have to start from scratch every time. You can use a recipe to get the same quality each time.
How do you structure a subroutine?
You need to add ()
even if there are no arguments followed by a colon :
. The code needs to be indented.
What are parameters (arguments)?
the pieces of information we pass to the code. These can be variable names that are made up for the first time within the argument ()
.
How do you structure a subroutine?
How do you call the subroutine?
How do you add more parameters (arguments) to a subroutine?
What does the Return Command do?
The return
command sends some information back to the part of the code that called it. This means the function call is replaced with whatever was returned.
What does the pinPicker subroutine do?
This subroutine creates a random pin number for us. This subroutine (called pinPicker
) has the parameter called number
(how many numbers I want to have in this pin). Then, there is a string (called pin
) that is empty and a for
loop that is used to create a defined amount of random numbers. The variable number
controls how many times the loop will add the new number to the pin. This is done through +=
and concatenating new values. We will cast the random number as a string so it can be concatenated together.
Then...the magic...we return
the pin.
What is the os library?
It allows us to "talk" to the console. One of the most powerful things we can do with this library is allow it to clear the console.
Example of os library
What does the time library do?
This library allows us to pause the execution of a program for a specific amount of time.
The time.sleep(1)
function allows us to pause the program for the amount of seconds listed in the ()
.
What is another way you can use a print statement?
What is another another way to use the print statement?
How do you turn the cursor (white box in the console) off in the console?
How do you turn the cursor (white box in the console) back on in the console
What is the best way to combine variables and text together?
f-strings (format strings) are the best way to combine variables and text together.
Without 1:
With 2:
How do you structure f-strings?
What can we use local variables for?
To use the same variable multiple times in a print statement.
How do you structure local variables?
What is a simpler way to use local variables?
Use the letter f
before any string with {}
for variable names (and forget that .format
business).
How do you align text in a print statement?
How do you move through data in a list?
How do you change an item in a list?
What is a simpler way to create a list?
Without 1:
With 2:
What are Dynamic Lists?
Dynamic lists are ways of using a blank list and adding or removing items to it as we go.
How do you structure a dynamic list?
How do you add things to the dynamic list?
.append
will let us add whatever is in ()
to the list.
What is the structure for adding things to a dynamic list?
How do you remove an item from a dynamic list?
How do you fix trying to remove something that isn't in your dynamic list?
What does the len function do?
len
counts how many items are in a list. In this case, it is starting at 0 and then keeps going until it reaches the end of our data inside our list. We can now eliminate the counter variable because we are counting with index (counter+=1.
-
It requires the range function
What are the 4 types of string manipulation for if statements to reduce errors? There’s a 5th extra
There is also .strip
Example of the string manipulation for if statements to reduce errors:
How do you prevent duplicates in a list?
by using .strip and then either of the 4 types of string manipulation