🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

DP0 Basic Python programming.pptx

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Lesson 1: First steps DP0 – Intro to Python programming Starter activity Can you follow these instructions? You have two strips of paper. One is X cm long, the other is Y cm long. a b...

Lesson 1: First steps DP0 – Intro to Python programming Starter activity Can you follow these instructions? You have two strips of paper. One is X cm long, the other is Y cm long. a b c Align the two Cut the longer Discard the part that Repeat a toc strips at one end. strip where the you have cut off that until the strips shorter strip ends. is the same length as are equal in the shorter strip. length. Measure that length and report it — it’s the required result. Starter activity Euclid’s method: finds the LCF of X and Y Following these instructions solves a problem: it calculates the Have you heard of the term largest common factor of X and ‘algorithm’ before? Y. This algorithm was recorded by Euclid in around 300 BC. Unit objectives In the previous units, In this unit, you will... you... Used a block-based programming Use a text-based language (Scratch) to create programming language programs that involved: (Python) to build on these ○ Variables, operators, and concepts and extend them expressions ○ Sequence, selection, and iteration ○ Subroutines ○ Lists print("Hello Python!") Objectives In this lesson, you will... Define algorithms and programs and describe how they are executed Write and execute your first Python programs They will involve displaying messages, assigning values to variables, and receiving input from the keyboard. Battle syntax errors! Activity 1 Algorithms An algorithm is a set of precise instructions, expressed in some sort of language (e.g. textual, visual). Understanding the language is necessary in order to execute the instructions. Executing these instructions is meant to solve a problem. Activity 1 Programs A program is a set of precise instructions, expressed in a programming language. Translating the programming language is necessary for a machine to be able to execute the instructions. Activity 1 Python programs To execute a Python program, you need a Python interpreter. This is a program that translates and executes your Python program. The Python interpreter doesn’t necessarily run on your computer. Activity 1 Syntax All programming languages have All languages have rules for rules for syntax, i.e. how syntax, i.e. how sentences can statements can be assembled. be assembled. Programs written in a Speech or text in a language programming language must must follow its syntax. follow its syntax. Programs with syntax errors Humans can infer meaning even cannot be translated and in cases when syntax rules are executed. violated. For example, “tonigth see you”, instead of “see you tonight”, will probably be understood. Activity 1 Syntax All programming languages have rules for syntax, i.e. how statements can be assembled. Programs written in a programming language must In Scratch, syntax errors are not follow its syntax. possible: rules are enforced by Programs with syntax errors the blocks and the way they fit cannot be translated and together. executed. You can still make logical errors ! That’s when your program doesn’t work the way it should. Activity 1 Syntax All programming languages have if remaining < 10: print("We are getting there") rules for syntax, i.e. how else: statements can be assembled. print("Still some way to go") Programs written in a programming language must In Python, you can (and you will) follow its syntax. make syntax errors. You will need Programs with syntax errors to follow the syntax rules. cannot be translated and executed. Syntax errors can be frustrating when you start learning a text-based programming language. Activity 1 Syntax SyntaxError: invalid syntax SyntaxError: Missing parentheses in call to 'print'. SyntaxError: EOL while scanning string literal Has this happened YO to ? Don’t be overwhelmed by these errors. They are here to discourage the faint-hearted. You can fix them! U Activity 2 Your first steps in Python Use pair programming. Driver Control the keyboard and mouse. Navigator Provide support and instructions. Alternate between roles. Activity 2 Your first steps in Python print("Hello world!") Every time you learn a new programming language, it’s a tradition to get your first program to say “hello”. Activity 2 Your first steps in Python Complete the first task in your worksheet to create your own ‘Hello world’ program. Activity 2 Your first steps in Python: commentary print("Hello world!") Checklist: ✔ Have you spelt print correctly? Python is case-sensitive: using capitals makes a difference. ✔ Have you used (opening and closing) round brackets around the message to be displayed? ✔ Have you used (single or double) quotation marks around the message to be displayed? Activity 2 Your first steps in Python: commentary print("Hello world!") You will need the print function: when your program must display text, numbers, or the values of variables and expressions. Activity 2 Your first steps in Python Complete the second task in your worksheet to greet the user by name. Activity 2 Your first steps in Python: commentary user = "Claude" You will need an assignment statement: print("Hello", user) when your program must use a name (an identifier) to keep track of a value. user is a variable, i.e. a name for a value. The variable user currently refers to the value "Claude". The quotation marks around the value show the type of the value: it is a string (a piece of text). Activity 2 Your first steps in Python Complete the third task in your worksheet. Activity 2 Your first steps in Python: commentary user = "Claude" user is a variable. print("Hello", user) It is assigned a string value. lucky = 13 lucky is another variable. print("My lucky number is", lucky) It is assigned an integer value. It is useful to sketch variables and their corresponding values, as they change during program execution. user "Claude" lucky 13 Activity 2 Your first steps in Python Complete the final task in your worksheet, to make your program receive input from the user. Activity 2 Your first steps in Python: commentary print("What’s your name?") You will need the input function: user = input() when your program must receive keyboard print("Hello", user) input from the user. When input is invoked, the program pauses, waiting for keyboard input. The text typed by the user is assigned to the user variable. We can refer to the value of user in the program without knowing what it will be. Activity 3 Python recap print("Best film ever?") Locate the fragment(s) of code film = input() that: print(film, "is my favourite too!") Display a message for the user on the screen Retrieve what the user types on the keyboard Assign a value to a variable Summary In this lesson, you... Next lesson, you will... Defined what algorithms and Use arithmetic expressions to programs are calculate values Wrote and executed your first Trace the values of variables as a Python programs (and weeded program is executed out syntax errors) Your programs involved displaying messages, Write programs that receive assigning values to variables, and receiving input numerical input from the from the keyboard keyboard Lesson 2: Crunching numbers DP0 – Intro to Python programming Starter activity Make predictions (think, pair, share) lucky = 13 Question. print("My lucky number is", lucky) What will be the output of print when this program is executed? A. 1 My lucky number is lucky ▹ B. My lucky number is 13 2 3 C. It is not possible to know the output without executing the program D. There is an error in the program 4 True or false?.True This program will always produce the same output, whenever it is executed. Starter activity Make predictions (think, pair, share) print("My lucky number is", lucky) Question. lucky = 13 What will be the output of print when this program is executed? A. 1 My lucky number is lucky B. My lucky number is 13 2 3 C. It is not possible to know the output without executing the program ▹ D. There is an error in the program 4 During program execution, a variable must have been assigned a value before that value is referenced. Starter activity Make predictions (think, pair, share) print("What’s your name?") Question. user = input() What will be the output of print when this print("Hello", user) program is executed? A. 1 Hello user ▹ B. Hello and whatever the user has typed 2 on the keyboard C. It is not possible to know the output 3 without executing the program D. There is an error in the program 4 True or false?.Fals This program wille always produce the same output, whenever it is executed. Objectives In this lesson, you will... Use arithmetic expressions to calculate values Use variables to store and reference values Follow walk-throughs of code and keep track of variable values Write programs that receive numerical input from the keyboard Activity 1 Assignments days = 365 Assignments are not equations. print(days, "days in a year") This assignment does not mean that the days variable always equals 365. Assignments are instructions to be executed. This is an instruction to assign the value 365 to the days variable. A subsequent assignment can assign a new value to the days variable, replacing the previous value. Activity 1 Assignments with expressions days = 7 * 31 + 4 * 30 + 28 You can use expressions in assignments. print(days, "days in a year") This is an instruction to evaluate the expression on and thethen rightassign the value to the days variable on the left. Tip: Read assignments from right to left. A subsequent assignment can assign a new value to the days variable, replacing the previous value. Activity 1 Arithmetic operators (in Python) You can use these operators to Examples form arithmetic expressions. + addition a+1 a plus 1 - difference b-c b minus c * multiplication 3*d 3 times d / division 9/4 9 divided by 4 (value: 2.25) // integer division 15 // 2 quotient of 15÷2 % remainder of integer (value: 7) division 15 % 2 remainder of 15÷2 ** exponentiation (value: 1) 2 ** 8 2 to the power of 8 (value: 256) Activity 1 Referring to variables days = 7 * 31 + 4 * 30 + 28 An expression can refer to the values of quad = 4 * days + 1 variables. print(quad, "days in four years") To evaluate this expression, the days variable must have been assigned a value. During program execution, a variable must have been assigned a value before that value is referred to. Activity 1 The machine executes the code days = 7 * 31 + 4 * 30 + 28 365 Current instruction. quad = 4 * days + 1 Evaluate the expression print(quad, "days in four years") and assign the value to days. ? Calculate the days in a year. State. days 365 Output. Activity 1 The machine executes the code days = 7 * 31 + 4 * 30 + 28 Current instruction. quad = 4 * days + 1 1461 Evaluate the expression print(quad, "days in four years") and assign the value to quad. ? Calculate the days in four years. State. days 365 quad 1461 Output. Activity 1 The machine executes the code days = 365 Current instruction. quad = 4 * days + 1 Display the value of quad print(quad, "days in four years") and the literal "days in four years". ? Display the result. State. days 365 quad 1461 Output. 1461 days in four years Activity 1 Order matters You will be given a program that is supposed to convert a length of time from seconds to minutes. Rearrange (change the order of) the statements, so that the program runs to completion without errors. Use your worksheet. Activity 2 Subtle points number = 5 Question. double = 2 * number What will be the value of double, after A number = 15 executing lineA ? A. ▹ 1 10 B. 30 2 C. Line A is not a valid assignment: 3 number already has a value Why. Line A only affects the number variable. The value of double is not ‘updated’. Activity 2 Subtle points number = 5 Question. A number = number + 10 What will be the value of number, after executing lineA ? A. 1 5 and 15 ▹ B. 15 2 C. There are no valid values for number 3 D. Line A is not a valid assignment: 4 number already has a value Why. The expression number + 10 is evaluated and the result is assigned to number. The previous value of number is replaced. Activity 3 Calculate age from year of birth Use pair programming. Driver Control the keyboard and mouse. Navigator Provide support and instructions. Alternate between roles. Activity 3 Calculate age from year of birth print("Year of birth?") Create a program that asks the birth_year = input() user for their birth year and age = 2020 - birth_year print("You are", age, "years old") calculates their age. Live coding age = 2020 - birth_year TypeError: unsupported operand type(s) for -: 'int' and 'str' Activity 3 Calculate age from year of birth: commentary print("Year of birth?") The input function always returns what the birth_year = input() user typed as a string, i.e. a piece of text. age = 2020 - birth_year print("You are", age, "years old") The text returned by input is assigned to the birth_year variable: It is not possible to subtract a piece of text from a number, hence the error. what the call "2008" to input returns birth_year "2008" Activity 3 Calculate age from year of birth: commentary print("Year of birth?") The input function always returns what the birth_year = int(input()) user typed as a string: a piece of text. age = 2020 - birth_year print("You are", age, "years old") This value is passed to the int function, which returns the corresponding integer. The expression is evaluated and the result is assigned to the age variable. what the call "2008" to input returns birth_year 2008 age 12 Activity 4 How to input numbers Work on programs that receive numerical input from the keyboard and process it. Use your worksheets. Activity 4 How to input numbers: solutions print("Weight on Earth?") weight_earth = int(input()) weight_moon = weight_earth / 6 print("Weight on moon:", weight_moon) Activity 4 How to input numbers: solutions print("How old are you?") age = int(input()) dog_years = age * 7 print("You are", dog_years, "years old in dog years") Homework Answer the questions in your homework sheet. Summary In this lesson, you... Next lesson, you will... Used arithmetic expressions to Use selection (if statements) to calculate values control the flow of program execution Used variables to store and reference values Introduce elements of randomness into your programs Followed walk-throughs of code and kept track of variable values Wrote programs that receive numerical input from the keyboard Lesson 3: At a crossroads DP0 – Intro to Python programming Starter activity Something missing print("What’s your name?") What if.... user = input() you wanted the program to recognise one print("Hello", user) particular name and treat it differently? print("Best film ever?") What if.... film = input() you wanted the program to react print(film, "is my favourite too!") enthusiastically only to a particular film? print("Year of birth?") What if.... birth_year = int(input()) you wanted the program to display a age = 2024 - birth_year comment that depends on the user’s age print("You are", age, "years old") range? Starter activity Selection When your programs check conditions and select the path of action that they will follow accordingly Objectives In this lesson, you will... Use selection (if statements) to control the flow of program execution between branches Introduce elements of randomness into your programs Activity 1 Selection conditio if n : block of statements conditio if n : block of statements else: block of statements Activity 1 Selection conditio if n : You will need an if or an if, else: block of when there is more than one possible statements path for your program to follow. conditio if n : block of statements else: block of statements Activity 1 Selection in Python: Greeting Use pair programming. Driver Control the keyboard and mouse. Navigator Provide support and instructions. Alternate between roles. Activity 1 Selection in Python: Greeting print("What’s your name?") Extend this program to recognise user = input() one particular name and treat it print("Hello", user) differently. Activity 1 Selection in Python: commentary print("What’s your name?") The condition will check if the value of user user = input() is equal to the string "Elizabeth". if user == "Elizabeth": The expression user == "Elizabeth" will print("Good morning Your Majesty") evaluate to either True or False. else: print("Hello", user) This is the if block, i.e. the code that will be executed if the condition is True. This is the else block, i.e. the code that will be executed if the condition is False. Only one of these blocks will be executed, depending on the value of the condition. Activity 1 Selection in Python: beware of syntax if user == "Elizabeth": Syntax pitfalls. print("Good morning Your Majesty") ✔ It’s if and else. No capitals. else: print("Hello", user) ✔ A colon : is always required after the if condition and after else. ✔ Use indentation to indicate which statements ‘belong’ to the if block and the else block. ✔ The == operator checks for equality. The single = is only used in assignments. ✔ user is a variable. Don’t use quotes. "Elizabeth" is a string literal. It needs quotes. Activity 1 Relational operators in Python (comparisons) You can use these operators to Examples compare the values of expressions. == equal to a == 1 Does a equal 1? != not equal to b != c Are b and c different? < less than d= greater than or equal d >= 10 Is d at least 10? to Expressions formed using these operators You can also use these operators to evaluate to either True or False. compare alphanumeric values (strings). Activity 2 Practise using selection Extend some of the programs that we’ve built so far to use selection. Use your worksheet. Activity 2 Practise using selection: example solutions print("Best film ever?") film = input() if film != "BFG": print(film, "is not too bad") else: print(film, "is my favourite too!") Activity 2 Practise using selection: example solutions lucky = 13 print("Guess my number:") guess = int(input()) if guess == lucky: print("Amazing, you guessed it") else: print("Sorry, it’s not", guess) print("My lucky number is", lucky) print("Nice playing with you") Activity 3 Lucky number: labels lucky = 13 Pick a lucky number. print("Guess my number:") Prompt the user to guess. guess = int(input()) if guess == lucky: Check answer and provide feedback. print("Amazing, you guessed it") else: print("Sorry, it’s not", guess) print("My lucky number is", lucky) print("Nice playing with you") Say goodbye. Activity 3 Lucky number: executing the program lucky = 13 State. print("Guess my number:") lucky 13 guess = int(input()) guess 13 if guess == lucky: True print("Amazing, you guessed it") else: Input/Output. print("Sorry, it’s not", guess) Guess my number: print("My lucky number is", lucky) User types 13 print("Nice playing with you") Amazing, you guessed it Nice playing with you Activity 3 Lucky number: executing the program lucky = 13 State. print("Guess my number:") lucky 13 guess = int(input()) guess 7 if guess == lucky: False print("Amazing, you guessed it") else: Input/Output. print("Sorry, it’s not", guess) Guess my number: print("My lucky number is", lucky) User types 7 print("Nice playing with you") Sorry, it’s not 7 My lucky number is 13 Nice playing with you Activity 4 Randomness lucky = 13 What if.... print("Guess my number:") you wanted a program that didn’t always guess = int(input()) pick the same lucky number? if guess == lucky: Live coding (ncce.io/py-lucky-31) print("Amazing, you guessed it") else: print("Sorry, it’s not", guess) print("My lucky number is", lucky) print("Nice playing with you") Activity 4 Randomness from random import randint Modules (or libraries). lucky = randint(1,20) They extend what our programs can do by print("Guess my number:") providing additional functions.. guess = int(input()) Importing: “from the random module, the if guess == lucky: program will need the randint function” print("Amazing, you guessed it") else: Function call: Invoke the randint function, print("Sorry, it’s not", guess) to return a random integer. print("My lucky number is", lucky) The (comma-separated) arguments 1 and print("Nice playing with you") 20 specify the range for the random integer. Activity 4 Randomness from random import randint Question. diceroll = You are making a dice rolling game. Fill in print("You rolled a", diceroll) the gap to complete the program. A. 1 int(input()) B. 1 to 6 2 3 C. pick random (1) to (6) ▹ D. randint(1,6) 4 Activity 4 Randomness from random import randint Question. diceroll = randint(1,6) When this program is executed, what will print("You rolled a", diceroll) its output be? A. 1 You rolled a diceroll B. You rolled a randint(1,6) 2 ▹ 3 C. You rolled a and a random integer ranging from 1 to 6 4 D. It is not possible to know the output without executing the program Activity 4 Randomness from random import randint Type this program in your diceroll = randint(1,6) development environment. print("You rolled a", diceroll) Run it a few times to see that a random dice roll is displayed each time. Activity 5 Subtle points diceroll = 3 True or false?.True if diceroll > 3: Whenever this program is executed, print(diceroll, "is a large roll") else: it will produce the same output. print(diceroll, "is a small roll") What will this output be? print("Yet you rolled a", diceroll) True or false?.True The instruction highlighted will always be executed. Activity 5 Subtle points from random import randint True or false?.Fals diceroll = randint(1,6) Whenever this e program is executed, if diceroll > 3: it will produce the same output. print(diceroll, "is a large roll") else: print(diceroll, "is a small roll") print("Yet you rolled a", diceroll) True or false?.Fals The instruction ehighlighted will always be executed. Activity 5 Subtle points from random import randint Question. coin = randint(0,1) When this program is executed, what will if coin == 0: its output be? result = "heads" A. 1 Either 0 or 1 print(result) ▹ B. Either heads or nothing 2 else: C. Either heads or tails 3 result = "tails" D. heads 4 Summary In this lesson, you.. Next lesson, you will... Used selection (if statements) to Explore how selection can control the flow of program handle more than two possible execution branches Introduced elements of Introduce iteration (while randomness into your programs statements) to allow the flow of program execution to include loops Lesson 4: More branches DP0 – Intro to Python programming Starter activity Strange weather print("Where do you live?") How would you extend this location = input() program to give advice to the print("Weather in", location, "now?") weather = input() user on how to dress depending on the weather? Provide a rough outline of what your Python code would look like. Give advice for when the weather is cloudy, rainy, or snowy. In any other case, display a generic message. Discuss in pairs and write an answer together. Objectives In this lesson, you will... Explore how selection can handle more than two possible branches Use iteration (while statements) to allow the flow of program execution to include loops Activity 1 Selection conditio if n : These versions of selection block of check one condition and select statements one out of two branches to follow. conditio if n : What if there are more than two block of statements cases in the problem? else: What if there are multiple block of Think of the weather example: branches ? statements “Give advice for when the weather is cloudy, rainy, or snowy.” Activity 1 Multi-branch selection conditio if Multi-branch selection (if, elif, else) n : block of checks successive conditions and statements selects one out of multiple branches to conditio follow. elif n : You will need an if block with elif blocks: block of statements when there are more than two mutually else: exclusive paths for your program to follow. block of statements You can use multiple elif blocks. The else and its block are optional. You can nest anything inside the blocks of statements, even more if statements. Activity 1 Strange weather print("Where do you live?") Extend this program using elif, to location = input() give advice to the user on how to print("Weather in", location, "now?") weather = input() dress depending on the weather. Give advice for when the weather is: cloudy, rainy, or snowy. In any other case, display a generic message. Activity 1 Strange weather: sample solution if weather == "cloudy": advice = "No sunglasses" elif weather == "rainy": advice = "Get an umbrella" elif weather == "snowy": advice = "Mittens and earmuffs" else: advice = "No particular advice" print(advice) Activity 1 Strange weather: executing the program if weather == "cloudy": False State. advice = "No sunglasses" weather "rainy" elif weather == "rainy": True advice = "Get an umbrella" advice "Get an umbrella" elif weather == "snowy": advice = "Mittens and earmuffs" else: Output. advice = "No particular advice" Get an umbrella print(advice) Activity 1 Strange weather: executing the program if weather == "cloudy": False State. advice = "No sunglasses" weather "clear" elif weather == "rainy": False advice = "Get an umbrella" advice "No particular elif weather == "snowy": False advice" advice = "Mittens and earmuffs" else: Output. advice = "No particular advice" No particular advice print(advice) Activity 1 Retrieving the weather from weather import description This modified program retrieves print("Where do you live?") the weather online, instead of location = input() asking the user. weather = description(location) print("The weather is", weather) Note The weather module and its functions are not standard Python components. Open the modified program in your development environment (ncce.io/py-weather-42). Run it a few times and enter different locations around the Activity 2 People in space You will be given a program that displays how many people are currently in space. Note: The number is retrieved from an online service. It is not always the same. Extend this program so that it asks the user to guess this number. Use your worksheet. Activity 2 People in space: sample solution from space import people Retrieve the number of people in number = people() space. print("How many people...") Prompt the user to guess. guess = int(input()) if guess < number: Check the answer and provide print("It's actually more than that.") feedback. elif guess > number: print("It's actually less than that.") else: print("That's right!") print(number, "people in space now") Display the number of people in space. Activity 3 Something missing print("What’s your name?") This program greets the user by name. name = input() print("Hello", name) What if.... we wanted to repeat the process until a specific name is entered? Many of our programs have sections that could be repeated e.g. checking the weather for multiple locations, and a number guessing game with many guesses. Activity 3 Iteration When your programs repeat actions, checking for a terminating condition at the beginning of each new loop Activity 4 Iteration conditio while n : block of statements while True: block of statements Activity 4 Iteration conditio while n : You will need a while statement: block of when your program needs to repeat actions statements while a condition is satisfied. Activity 4 Example: a (surreal) iterative program print("What’s your name?") Question Can you predict what the name = input() outcome of running this program will be? while name != "Hedy": Tip: while block These statements print("Try again Hedy") are to be repeated. name = input() Tip: while condition This condition is print("Hello", name) checked at the beginning of each loop. Answer This program will keep asking the user for their name, until the name is "Hedy", at which point it will display a greeting (ncce.io/py- you-4). Activity 4 Iteration: step-by-step execution print("What’s your name?") State. name = input() name "Margaret" while name != "Hedy": True print("Try again Hedy") Input/Output. name = input() What’s your name? print("Hello", name) User types Margaret Try again Hedy User types Ada Activity 4 Iteration: step-by-step execution print("What’s your name?") State. name = input() name "Ada" while name != "Hedy": True print("Try again Hedy") Input/Output. name = input() What’s your name? print("Hello", name) User types Margaret Try again Hedy User types Ada Try again Hedy User types Hedy Activity 4 Iteration: step-by-step execution print("What’s your name?") State. name = input() name "Hedy" while name != "Hedy": False print("Try again Hedy") Input/Output. name = input() What’s your name? print("Hello", name) User types Margaret Try again Hedy User types Ada Try again Hedy User types Hedy Hello Hedy Activity 4 Subtle points print("What’s your name?") Question. name = input() ⨯ What difference will it make if the line in while name != "Hedy": red is removed? print("Try again Hedy") A. 1 It will make no difference. name = input() ▹ B. There’s no initial value for name: 2 print("Hello", name) an error will occur when checking the condition in while. C. There’s no initial value for name: 3 an error will occur when executing 4 print. D. There’s no initial value for name: the program will execute normally. Activity 4 Subtle points print("What’s your name?") Question. name = input() What difference will it make if the line in while name != "Hedy": red is removed? print("Try again Hedy") (Assume the first name is not "Hedy".) name = input() ⨯ A. 1 It will make no difference. print("Hello", name) 2 B. The value for name is never modified: an error will occur when checking the condition in while. ▹ 3 C. The value for name is not modified: the program will never terminate. Homework Answer the questions in your homework sheet. Summary In this lesson, you... Next lesson, you will... Explored how selection can Use iteration (while statements) handle more than two possible to allow the flow of program branches execution to include loops Used iteration (while Use Boolean variables, statements) to allow the flow of operators, and expressions program execution to include Use variables as counters and loops flags Lesson 5: Round and round DP0 – Intro to Python programming Starter activity Making predictions count = 3 Question. print(count) What will this program display when it is count = count-1 executed? print(count) A. 1 There is no valid value for count. This will result in an error. B. It will print 2. 2 C. It will print 3. 3 ▹ D. It will print 3 and then 2. 4 Starter activity Making predictions count = 3 Assignments are not equations. print(count) Assignments are instructions to be count = count-1 executed. This is an instruction to: print(count) evaluate the expression count-1 and assign the value back to count (replacing the previous value). ? This is an instruction to: decrease the value of count by 1. Starter activity Making predictions: answer count = 3 Question. print(count) What will this program display when it is count = count-1 2 executed? print(count) State. Output. count 3 3 count 2 2 Objectives In this lesson, you will... Use iteration (while statements) to allow the flow of program execution to include loops Use variables as counters Activity 1 Count count = 3 Question. print(count) What will this extended program display count = count-1 when it is executed? The previous print(count) program count = count-1 ended here. Use your worksheet to answer. Then, move on to the next tasks. print(count) count = count-1 Activity 1 Count: walk-through count = 3 Question. print(count) What will this extended program display count = count-1 when it is executed? print(count) State. Output. count = count-1 print(count) count 3 3 count = count-1 count 2 2 count 1 1 count 0 Activity 1 Count: iterative count = 3 count = 3 print(count) while count >= 1: count = count-1 print(count) print(count) count = count-1 count = count-1 print(count) count = count-1 Activity 1 Count: solutions count = 3 count = 10 count = 1 while count >= 1: while count >= 1: while count 10 and c

Use Quizgecko on...
Browser
Browser