Python recap: Difference between revisions
Line 21: | Line 21: | ||
== Exercises to be handed in == | == Exercises to be handed in == | ||
A new function to be used in these exercises are the ''input'' function which gets a string as input from the keyboard. | |||
# First a tricky exercise - read carefully and think what this means. Make a program that asks for number, and then continues asking for numbers as long as you input numbers that are greater or equal to all previous numbers (not the sum of previous numbers). Hint: use a list and you fail. | |||
# You have previously calculated the factorial of a number - this is somewhat similar.Ask for an integer and calculate the sum from 0 up/down to the integer. An example: If you input 5 then calculate 5 + 4 + 3 + 2 + 1 (+ 0) = 15. If input is -4 then calculate -4 + -3 + -2 + -1 (+ 0) = -10. | |||
# | |||
# | |||
== Exercises for extra practice == | == Exercises for extra practice == |
Revision as of 15:22, 25 August 2025
Previous: Programme | Next: The path and simple file reading |
Required course material for the lesson
Powerpoint: Python Recap
Resource: Example code - Basics
Subjects covered
This is a superficial recap on Python variables, assignment, some operators (+-*/.), conditional statements with if, looping using for and while. built-in functions like print, len, range, input and type casting functions int, float and str. Well-known list and string methods, like split or strip. Various standard libraries like os, sys, and math.
It is expected that students know python to this level when starting the course.
Advice
Data and program code are separate entities, which go together. However, data can not be executed as code, and code is not typically thought of as data.
You can assume that the user is well-behaved enough to only input numbers, when numbers are required.
You can not assume that the user will not input numbers which in some way will give rise to a wrong/faulty/misbehaving calculation.
Exercises to be handed in
A new function to be used in these exercises are the input function which gets a string as input from the keyboard.
- First a tricky exercise - read carefully and think what this means. Make a program that asks for number, and then continues asking for numbers as long as you input numbers that are greater or equal to all previous numbers (not the sum of previous numbers). Hint: use a list and you fail.
- You have previously calculated the factorial of a number - this is somewhat similar.Ask for an integer and calculate the sum from 0 up/down to the integer. An example: If you input 5 then calculate 5 + 4 + 3 + 2 + 1 (+ 0) = 15. If input is -4 then calculate -4 + -3 + -2 + -1 (+ 0) = -10.
Exercises for extra practice
- Ask for a word 4 times. Display all the words afterwards, one word per line.
- Ask for a word 7 times. Display all the words afterwards on one line.
- In a loop ask for words. Add them together in the order given so they form a (maybe meaningless) sentence. When you do not enter anything (i.e. just hit the <enter> key), you stop the loop and display the sentence.
- In a loop ask for numbers using the input function. Add the numbers together as the are entered. End the loop when you write STOP instead of a number. Display the sum of the numbers.
Hint: Check for STOP before you try to typecast (convert) the number. - Continue to ask for numbers until you don't enter anything. Then display all the numbers and their sum. Example: The numbers 1, 2 & 3 is entered and "1 + 2 + 3 = 6" is displayed. Hint: You need use the numbers both as string type and number type.