Python Basics
Previous: Using Jupyter Notebook | Next: Simple File Reading |
Required course material for the lesson
Powerpoint: Python Basics
Video: Python basics. Language function. Assignment
Video: Python basics. Input and Output. Conditions and Conditional Statements
Video: Python basics. Loops. Data types and Conversion
Resource: Example code - Basics
Subjects covered
Variables (numbers, strings and booleans), assignment (giving values to variables ), some operators (+-*/.), the print function (making data appear on the screen), conditional statements (if statement, which executes some statements, if certain conditions are true), the while loop (looping over statements as long as certain conditions are true), how to give input to the program from the keyboard with the input function. The range function, which gives a range of integers. The int, float and str functions, which typecasts to integer, floating points and strings. Indentation which is part of the syntax.
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
Remember, every exercise consists of 2 cells; One markdown cell to tell what the program is about, and one code cell that contains the program.
- 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.
- Write numbers 1 to 10 to the screen one number per line. Use a loop.
- Write 'Hello World' 10 times using a loop. One Hello per line.
- 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.
- Make a program that ask for two numbers (one at a time) and then prints them and their sum.
- Ask for two numbers and ask what operation to perform on them (+, - , *, /) and display the numbers and the result.
- 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.
- 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.
- 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 a positive integer and calculate the factorial (n!) of that number. Display the result. If input is negative, display an error message.
- 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
- 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.