Python Practice

0.0(0)
studied byStudied by 1 person
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/69

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

70 Terms

1
New cards

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

2
New cards

How do you type in the console?

By using input command.

Structure:

<p>By using <strong>input </strong>command.</p><p></p><p>Structure:</p><p></p>
3
New cards

What is a variable in coding?

a named piece of computer memory
myName is the variable

<p><span>a named piece of computer memory</span><br><span>myName is the variable</span></p>
4
New cards

What is concatenating?

Calling a variable into your print statement.

Concatenates only works with the input command.

5
New cards

How do you add color to text

6
New cards

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.

7
New cards

How do you write an if statement?

8
New cards

How do you concatenate?

9
New cards

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

10
New cards

How do you write an else statement?

The else statement must be indented the same as the if statement.

11
New cards

What is the elif command?

The elif command (which stands for 'elseif') allows you to ask as many questions as you want.

12
New cards

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.

13
New cards

What is Nesting?

Nesting is where we put an if statement within an if statement using the power of indenting.

14
New cards

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.


15
New cards

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.

16
New cards

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 "".


17
New cards

What is int?

int makes the computer recognize whole numbers

18
New cards

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.


19
New cards

What is float?

float makes the computer recognize decimal numbers.

20
New cards

How do you make the computer do math?

21
New cards

How do you make the computer round numbers?

22
New cards

How do you make the computer repeatedly run the code?

By using a while loop

while loop allows your code to repeat itself based on a condition you set.

23
New cards

How do you write a write a while loop?

knowt flashcard image
24
New cards

How do you avoid infinite loops while using a while loop?

25
New cards

What is != in python?

!= means (does not equal)

26
New cards

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

27
New cards

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.

28
New cards

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.

29
New cards

How do you use the break command?

30
New cards

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.

31
New cards

How do you write the continue command?

32
New cards

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.


33
New cards

When is it best to use a while loop?

while loop is perfect to use when we don't know how many times we want the loop to repeat.

34
New cards

What is the structure of a for loop?

35
New cards

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?)

36
New cards

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.


37
New cards

What is a Subroutine?

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.



38
New cards

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.

39
New cards

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 ().

40
New cards

How do you structure a subroutine?

41
New cards

How do you call the subroutine?

42
New cards

How do you add more parameters (arguments) to a subroutine?

43
New cards

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.

44
New cards

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.

45
New cards

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.

46
New cards

Example of os library

47
New cards

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 ().


48
New cards

What is another way you can use a print statement?

49
New cards

What is another another way to use the print statement?

50
New cards

How do you turn the cursor (white box in the console) off in the console?

51
New cards

How do you turn the cursor (white box in the console) back on in the console

52
New cards

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:

53
New cards

How do you structure f-strings?

54
New cards

What can we use local variables for?

To use the same variable multiple times in a print statement.

55
New cards

How do you structure local variables?

56
New cards

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).

57
New cards

How do you align text in a print statement?

58
New cards

How do you move through data in a list?

59
New cards

How do you change an item in a list?

60
New cards

What is a simpler way to create a list?

Without 1:


With 2:

61
New cards

What are Dynamic Lists?

Dynamic lists are ways of using a blank list and adding or removing items to it as we go.

62
New cards

How do you structure a dynamic list?

63
New cards

How do you add things to the dynamic list?

.append will let us add whatever is in () to the list.

64
New cards

What is the structure for adding things to a dynamic list?

65
New cards

How do you remove an item from a dynamic list?

66
New cards

How do you fix trying to remove something that isn't in your dynamic list?

67
New cards

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 

68
New cards

What are the 4 types of string manipulation for if statements to reduce errors? There’s a 5th extra


There is also .strip

69
New cards

Example of the string manipulation for if statements to reduce errors:

70
New cards

How do you prevent duplicates in a list?

by using .strip and then either of the 4 types of string manipulation