Python 3 latest code crumbs
While loop
Jan 16, 2022 by erik
While the for-loop in Python is a bit hard to understand, the while loop is actually much simpler! In this example, we see an expression that follows the while statement: i <= 4
. As long as this expression evaluates to...
Python for loop and lists
Jan 16, 2022 by erik
A Python list can contain zero or more objects. It's a frequently used data type in Python programming. In other programming languages, lists are often called arrays. Python does not have arrays, though, only lists. We can loop, or iterate,...
The print function
Jan 16, 2022 by erik
The Python print
function can print text to your screen. We can feed it one or more arguments, and these arguments will get printed on the screen. In its most basic form, we can call print
without any argument. In...
If statement
Jan 16, 2022 by erik
The if statement is a so-called conditional statement. It is followed by an expression that can evaluate to either True
or False
. If the expression evaluates to True
, the block of code that follows is executed. If it evaluates to...
Python exception handling with try.. except
Dec 30, 2021 by erik
This is a small assignment from the Python try-except tutorial. Please do the following: 1. Run the code below, notice the file name (it doesn't exist). See what happens. 2. Change the file name in the Python code to myfile.txt
...
How to reverse a Python list
Dec 27, 2021 by erik
There are actually three methods you can use to reverse a list: 1. An in-place reverse, using the built-in reverse method that every list has natively 2. Using list slicing with a negative step size, which results in a new...