22
Lesson 4 of 44

Flow controls: Conditions

Immerse yourself in the captivating world of control structures and conditions in programming with this lesson. You will discover how to leverage these crucial tools to make decisions in your programs and efficiently manage the flow of execution. This lesson provides you with the essential skills to develop programs with logical and effective decision-making. Enhance your programming proficiency through hands-on exercises and take another stride in your journey towards mastering control structures and conditions.

  • 7 theory articles
  • 15 practice exercises
Start lesson

Fundamentals concepts 7

The if statement

Until now your programs ran all their lines, one after the other. With conditions the program can decide : it runs a block of…

Theory
Read more

else and elif: several paths

A lone if only acts when the condition is true. To choose between two or more paths you add else and elif .

Theory
Read more

Compound conditions: and, or, not, in

Many decisions depend on more than one thing at once. Logical operators let you combine conditions into one.

Theory
Read more

Nested conditions

Inside the block of an if you can write another if . This is called nesting conditions, and it is used to make decisions on…

Theory
Read more

The conditional operator

When an if/else is only used to choose between two values, Python lets you write it on a single line. It is called the…

Theory
Read more

Common mistakes with conditions

Conditions are simple, but they have traps that almost everybody falls into at first. These are the most common mistakes and how…

Theory
Read more

The match/case statement

Since Python 3.10 there is the match statement, which compares a value against a series of patterns and runs the first one that…

Theory
Read more

Practice exercises 15

Basic calculator using if

Write a Python program that functions as a basic calculator. The program will prompt the user to input two numbers and a…

Exercise Easy
Solve it

ABS - Absolute value

Develop a Python program to determine and display the absolute value of a number, whether it is positive or negative.

Exercise Easy
Solve it

Positive and negative

Write a Python program that asks the user to enter a number (x). The program should determine whether this number is positive or…

Exercise Easy
Solve it

Check vowels

Create a Python program that prompts the user for a letter x and checks whether it is a vowel or any other symbol. The program…

Exercise Easy
Solve it

Check even numbers

Create a Python program that uses the conditional operator ( and ) to assign a value to a boolean variable. Assign True if both…

Exercise Easy
Solve it

The largest of three numbers

Write a Python program that prompts the user to enter three numbers (x, y, z) and determines which of them is the largest number…

Exercise Easy
Solve it

Conditional operator

Create a Python program that prompts the user for two numbers and responds whether they are positive or if both are positive…

Exercise Medium
Solve it

Nested conditional operator

Create a Python program that prompts the user to enter two integers (a, b) and determines how many of them are positive using…

Exercise Medium
Solve it

Student grade evaluation

Create a Python program that calculates a student's grade based on an integer input. Request a number (x) from the user and…

Exercise Easy
Solve it

Leap year

A year is a leap year if it is divisible by 4, except century years (divisible by 100), which are only leap years if they are…

Exercise Easy
Solve it

Classifying a triangle

Three lengths form a triangle only if each side is smaller than the sum of the other two . If they do, it is classified by its…

Exercise Medium
Solve it

Rock, paper, scissors

In the game of rock, paper, scissors , rock beats scissors, scissors beat paper and paper beats rock. If both choose the same, it…

Exercise Medium
Solve it

FizzBuzz of a number

FizzBuzz is a classic for practicing conditions: depending on the divisors of the number, one word or another is shown. The order…

Exercise Easy
Solve it

Shipping rate by brackets

The price of a shipment depends on the bracket in which the weight of the package falls. The conditions are evaluated in order…

Exercise Medium
Solve it

Menu with match

The match statement (Python 3.10 or later) compares a value against several patterns , even structures such as lists. It is ideal…

Exercise Hard
Solve it