Module 3 - Section 0 - Forget == Repeat :)

Site: ΕΛ/ΛΑΚ Moodle
Course: Python Lab
Book: Module 3 - Section 0 - Forget == Repeat :)
Printed by: Guest user
Date: Thursday, 21 November 2024, 6:00 PM

Description

This section is the repetition of the previous 2 modules

3.0.1 - What starts with an If, ends with a decision

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

 

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.

3.0.2 - Some nests hide 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:


Don't forget the indentation!

Also, have you noticed the , end="" inside the parenthesis in print command?

Remove it and run the program again to see the difference.

Also, change the print command line with the following :

print("You have passed with grade ", end =": ")

What is the conclusion?

A print command without end inside the parenthesis, inserts a new line. With end we can change the end of the line from new line to whatever we like!