# #4 – Learn Python With Practice – conditional structures

#4 – Learn Python With Practice – conditional structures

Following the previous lesson, we were asking how to check if the user has the same age as you for example :

```def get_user_age():
user_age = input("What is your age ? ")
return user_age
print("Maybe we have the same age !")
print(get_user_age())```

If you didn’t understand well indentation in Python, you will understand them right now in this chapter …

We can excute some lines of our code with conditions, for example print « We have the same age » only if user’s age is the same us yours.

Start by running the following code in your computer :

```a = 20
if a == 20:
print("a is equal to 20")
print("End of program !")```

You see ! It prints a is equal to 20 and End of program !

Change the value of a to 21 and run again your program.

a is equal to 20 is not printed, because the value of a is 21 and 21 is not equal to 20 !

We can compare variable to variable too :

```a = 5
b = 5

if a == b:
print("a is equal to b")```

DO NOT RUN THE CODE, TRY TO ANSWER BEFORE

```a = 5
b = 5
c = 10
a = 10

if a == c:
print("Nice to meet you!")
if a == b:
print("Have a good day!")```

### #1. What will the program above print ?

Below a list of comporators is Python (and meany other languages)

• a == b means a is equal to b
• a != b means a is not equal to b
• a > b means a is greater than b
• a < b means a is less than b
• a >= b means a is greater than or equal to b
• a <= b means a is less than or equal to b

In the following example, we created three variables a, b, c and assigned them three values in order 20, 23, 17.

Then the c variable will take another value, please don’t run the code and guess what the program will print. (answer the quizz).

I want let you understand by yourself.

```a = 20
b = 23
c = 17
c = a + 3

if b == c:
print("b is equal to c")
if b != a:
print("b is not equal to c")```

### #1. What the program above will print ?

The value of b is 23 and the value of c was 17 and has become 23 (20 + 3).

We compared the values b and c then we print b is equal to c if realy b is equal to c, otherwise we would print b is not equal to c.

```a = 20
b = 23
c = 17
c = a + 3

if b == c:
print("b is equal to c")
else:
print("b is not equal to c")```

The program above will do the same thing, try it!

if b is equal to c, print b is equal to c, otherwise (not equal means, greater than or less than).

Imagine if we want to check if b is equal to c and if b is not equal to c we want to check again if b is less than c.

```a = 20
b = 22
c = 17
c = a + 3

if b == c:
print("b is equal to c")
else:
if b < c:
print("b is less than c")```

That works, the program will show you b is less than c

note : I changed the value of b to 22.

We can write the same code in fewer lines in Python :

```a = 20
b = 22
c = 17
c = a + 3

if b == c:
print("b is equal to c")
elif b < c:
print("b is less than c")```

elif in Python means else if.

```a = 20
b = 22
c = 17
c = a + 3

if b == c:
print("b is equal to c")
elif b < c:
print("b is less than c")
else:
print("b is not equal to c and is not less than c, so it is greater than c")```

Try the example above.

That’s all for conditional structures in Python for today, we will learn more and in deep by the next chapters.

### Exercice

We take the example from the chapter #2 and let’s change it

```print("This program will help you to create awesome resume and find \n new job")
user_name = input("What is your full name ?")
user_age = input("What is your age ?")
user_school = input("What is your school name ?")
user_looking_for_new_job = input("Are you looking for new job ?\n if yes tape 'YES'")

# let's print the user resume

print("Name : ", user_name)
print("age : ", user_age)
print("School : ", user_school)
print("Looking for new job : ", user_looking_for_new_job)```

The program above will ask user for his information and show them as a resume.

In your resume, you wouldn’t print sentence like « Looking for new job: YES ».

We would like print I’m looking for new job as a software developer for example.

Your work now is to do some modifications in the previous example :

• Check if the user_looking_for_new_job value is equal to « YES ».
• If user_looking_for_new_job value is equal to « YES ». than print « Looking for a new job ».
```print("This program will help you to create awesome resume and find \n new job")
user_name = input("What is your full name ?")
user_age = input("What is your age ?")
user_school = input("What is your school name ?")
user_looking_for_new_job = input("Are you looking for new job ?\n if yes tape 'YES'")

# let's print the user resume

print("Name : ", user_name)
print("age : ", user_age)
print("School : ", user_school)

if user_looking_for_new_job == "YES":
print("Looking for new job : ", user_looking_for_new_job)```

In the resume software we are developping, we ask user and print name, age, school and if he is looking for new job, how to list his experiences ?

That’s what we will see in next Chapter : #5 – Lists.