0% found this document useful (0 votes)
54 views

If Statement in Python

The document discusses if statements in Python. It provides an example of using an if statement to check if a person's age is less than 18, and print that they cannot drive if it is. It translates this into a Python program that gets a person's age as input, uses an if statement to check if it is less than 18, and prints that they cannot drive if so. It then explains how the if statement works - if the condition is true, the code block under it will execute, and if false it will skip to endif. It adds an else statement to also check if the age is greater than 18 and print they can drive. Finally, it discusses comparison operators used in conditions like <, >, ==,

Uploaded by

Moh Yousif
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

If Statement in Python

The document discusses if statements in Python. It provides an example of using an if statement to check if a person's age is less than 18, and print that they cannot drive if it is. It translates this into a Python program that gets a person's age as input, uses an if statement to check if it is less than 18, and prints that they cannot drive if so. It then explains how the if statement works - if the condition is true, the code block under it will execute, and if false it will skip to endif. It adds an else statement to also check if the age is greater than 18 and print they can drive. Finally, it discusses comparison operators used in conditions like <, >, ==,

Uploaded by

Moh Yousif
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

IF Statement in Python

to be able to put a test or a certain condition , if statement should be added 


Ex. if the age of a person is less than 18 , he can not drive a car . if Not
( bigger than ) he can drive a car 

let  us translate above mentioned word to be a program in python


age =  eval( input ( " age = "))

if age <18 then 

Print(" you can not drive ")

Endif 

If ( age < 18) :                                           if the answer is true or right  immediately the
next line is executed  
print (" you can not drive ")
Endif 
if you focused in above program we will deduct the following 
1- if the answer of question ( if age < 18) is age less than 18 ? answer is YES or TRUE 
that is mean this person can not drive  a car 
2- the next line will be executed immediately  in case the answer of if statement is true
3- if the answer of question or statement if ( if age < 18) that is mean his age if bigger
than 18 ? answer will be FALSE or NO the program will go to ENDIF to finish the
program 
Cont. If statement with testing two cases Else statement
2020-03-11 10:30:08
if ( age <18 ): 
print ("you can not drive)                                         in case of true    your age is less than
18 and go to ENDIF
else                                                                           in case of False    your age is bigger
than 18 and go to Endif
print (" you can  drive a car" )                
endIf

comparison and logical operators


2020-03-11 10:37:38
Many of the conditions you will work with depend on a compriosn of variables . 
the result is a boolean ( True or False)
==    equal 
!=  Not equal
<  less than
<= less than or equal 
> Greater than
>= Greater or equal 

You might also like