Week 3 - Section 0 - Those who forget the lessons of Python are doomed to repeat them :-)

Site: ΕΛ/ΛΑΚ Moodle
Course: Python Lab - May 2017
Book: Week 3 - Section 0 - Those who forget the lessons of Python are doomed to repeat them :-)
Printed by: Guest user
Date: Saturday, 20 April 2024, 8:31 AM

Description

In this section we will learn about lists and tuples and we will scratch the surface of pygame. Be prepared for excitement.

Chapter 0.1 - What starts with an If, ends with a decision


Perhaps the most well-known statement type is the if statement. For example:

x = int(input("Please enter an integer: "))
if x < 0:
     print('Negative number')
elif x == 0:
     print('Zero')
else:
     print('Positive number')

Be careful of the indentation. One space character in the wrong place can damage a program of a thousand lines.

There can be zero or more elif parts, and the else part is optional. The keyword ‘elif‘ is short for ‘else if’, and is useful to avoid excessive indentation. An if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other programming languages.


Chapter 0.2 - Some nests hides many IFs in Python


Once you are feeling comfortable with the if, elif, and else statements, you can move on to nested conditional statements. We can use nested if statements for situations where we want to check for a secondary condition if the first condition executes as true. For this, we can have an if-else statement inside of another if-else statement. Let’s look at an example:


grade=int(input("Give me the grade: "))
if grade >= 65:
    print("Passing grade of:")

    if grade >= 90:
        print("A")

    elif grade >=80:
        print("B")

    elif grade >=70:
        print("C")

    elif grade >= 65:
        print("D")

else:
    print("Failing grade")

Don't forget the indentation!




Chapter 0.3 - Loops make Python go round and range is the guide



Using loops in computer programming allows us to automate and repeat similar tasks multiple times.

A for loop implements the repeated execution of code based on a loop counter or loop variable. This means that for loops are used most often when the number of iterations is known before entering the loop, unlike while loops which are conditionally based.

Let’s look at a for loop that iterates through a range of values:

for i in range(0,5):
   print(i)

When we run this program, the output looks like this:

>>>
0
1
2
3
4

This for loop sets up i as its iterating variable, and the sequence exists in the range of 0 to 5(not printing).

Then within the loop we print out one integer per loop iteration. Keep in mind that in programming we tend to begin at index 0, so that is why although 5 numbers are printed out, they range from 0-4.

Another example, using start, stop and step is:

for i in range(0,15,3):
   print(i)

In this case, the for loop is set up so that the numbers from 0 to 15(not printing) print out, but at a step of 3, so that only every third number is printed, like so:

0
3
6
9
12

We can also use a negative value for our step argument to iterate backwards, but we’ll have to adjust our start and stop arguments accordingly:

for i in range(100,0,-10):
   print(i)

Here, 100 is the start value, 0 is the stop value, and -10 is the range, so the loop begins at 100 and ends at 0(not printing), decreasing by 10 with each iteration. We can see this occur in the output:

100
90
80
70
60
50
40
30
20
10