Python recap: Difference between revisions

From 22116
Jump to navigation Jump to search
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{| width=500  style="float:right; margin-left: 10px; margin-top: -56px;"
{| width=500  style="font-size: 10px; float:right; margin-left: 10px; margin-top: -56px;"
|Previous: [[Programme]]
|Previous: [[Programme]]
|Next: [[The path and simple file reading]]
|Next: [[The path and simple file reading]]
Line 6: Line 6:
== Required course material for the lesson ==
== Required course material for the lesson ==
Powerpoint: [https://teaching.healthtech.dtu.dk/material/22116/22116_01-Recap.ppt Python Recap]<br>
Powerpoint: [https://teaching.healthtech.dtu.dk/material/22116/22116_01-Recap.ppt Python Recap]<br>
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=e2e3be56-6210-4e62-84d0-b1f600a3af21 Playing the guessing game], strategies and advice.<br>
Resource: [[Example code - Basics]]<br>
Resource: [[Example code - Basics]]<br>
<!-- Failed transfer
<!-- Failed transfer
Video: [https://video.dtu.dk/media/22110-lesson02-LiveCoding/0_vkcbhv29 Live Coding]
Video: [https://video.dtu.dk/media/22110-lesson02-LiveCoding/0_vkcbhv29 Live Coding]
-->
-->
If you feel your basic python skills are lacking, then there is a discontinued - but available - softer introduction course: [https://teaching.healthtech.dtu.dk/22101/index.php/Programme 22101]<br>
Powerpoint: [https://teaching.healthtech.dtu.dk/material/22101/22101_03-Basics.ppt Python basic] Specially recommended for people lacking python experience


== Subjects covered ==
== 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''.<br>
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 methods like ''max'', ''min'' and ''sum''. Various standard libraries like ''os'', ''sys'', and ''math''.<br>
It is expected that students know python to this level when starting the course.
It is expected that students know python to this level when starting the course.


Line 21: Line 24:


== Exercises to be handed in ==
== Exercises to be handed in ==
# The very first exercise is to write some syntactically wrong python (that should not be hard). The purpose is to show you how a ''stack trace'' looks. You should learn how to read this confusing text, at least to the point where you can understand what the error in your program is.
A new function to be used in these exercises are the ''input'' function which gets a string as input from the keyboard.<br>
# Write numbers 1 to 10 to the screen one number per line. Use a loop.
Hint: use a list in any exercise and you fail.
# Write 'Hello World' 10 times using a loop. One Hello per line.
# 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).
# Make a program ask for a name, and then write a greeting using that name. However, if it is your name you give as input the greeting should be extra nice.
# 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.<br>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.
# Make a program that ask for two numbers (one at a time) and then prints them and their sum.
# Ask for a word 5 times. Display all the words afterwards on one line.
# Ask for two numbers and ask what operation to perform on them (+, - , *, /) and display the numbers and the result.
# 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.
# Ask for two integers and print them and all integers between them. It is not necessary to perform input control - just assume that the user is well-behaved and inputs integers.
# 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.<br>Hint: Check for STOP before you try to typecast (convert) the number.
# Now make the same program work even if you switch the input numbers, so it does not matter if you input the smallest number first or last.
# Continue to ask for numbers until you don't enter anything. Then display all the numbers and their sum.<br>Example: The numbers 1, 2 & 3 is entered and "1 + 2 + 3 = 6" is displayed.<br>Hint: You need use the numbers both as string type and number type.
# This needs to be read carefully: 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).
# Ask for 2 integers. Now print the first 20 numbers of the [https://en.wikipedia.org/wiki/Fibonacci_number Fibonacci sequence] given by those numbers. <br>If the input is 1 & 3, the output would be 1 3 4 7 11 18 29 47 etc.. The next number is the sum of the 2 previous.
# Ask for a positive integer and calculate the factorial (n!) of that number. Display the result. If input is negative, display an error message.
# Now for some playing. Make a program that guesses a number between 1 and 10 that you think of. It should make a guess, and you shall answer yes (if correctly guessed), higher (if the number you think of is higher than the guess) or lower (if the number you think of is lower than the guess). The program ends when the number is guessed correctly, otherwise it tries again. It is NOT considered OK to guess at a number more than once, i.e. no repeats. It is counterproductive to try to make this random guessing. Also think about how simple you can make this and still fulfill the wording of the exercises. Just don't run with the first idea you get. Don't embellish with additional functionality - it will just get difficult and we save that for later. Just to be clear - the number you think of must nowhere be in the program.
# If you solved the previous one then this should be relatively easy. 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 ==
* 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.<br>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.

Latest revision as of 16:33, 2 September 2025

Previous: Programme Next: The path and simple file reading

Required course material for the lesson

Powerpoint: Python Recap
Video: Playing the guessing game, strategies and advice.
Resource: Example code - Basics
If you feel your basic python skills are lacking, then there is a discontinued - but available - softer introduction course: 22101
Powerpoint: Python basic Specially recommended for people lacking python experience

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 methods like max, min and sum. 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.
Hint: use a list in any exercise and you fail.

  1. 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).
  2. 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.
    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.
  3. Ask for a word 5 times. Display all the words afterwards on one line.
  4. 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.
  5. 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.
  6. 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.
  7. Ask for 2 integers. Now print the first 20 numbers of the Fibonacci sequence given by those numbers.
    If the input is 1 & 3, the output would be 1 3 4 7 11 18 29 47 etc.. The next number is the sum of the 2 previous.
  8. Now for some playing. Make a program that guesses a number between 1 and 10 that you think of. It should make a guess, and you shall answer yes (if correctly guessed), higher (if the number you think of is higher than the guess) or lower (if the number you think of is lower than the guess). The program ends when the number is guessed correctly, otherwise it tries again. It is NOT considered OK to guess at a number more than once, i.e. no repeats. It is counterproductive to try to make this random guessing. Also think about how simple you can make this and still fulfill the wording of the exercises. Just don't run with the first idea you get. Don't embellish with additional functionality - it will just get difficult and we save that for later. Just to be clear - the number you think of must nowhere be in the program.

Exercises for extra practice