CSCI 131: Introduction to Programming with Python



CSCI 131: Introduction to Programming with Python

IF and IF – ELSE statements

if expression:

action

If expression is true the action will be executed, otherwise the program will skip the action and execute the first statement after if

Examples:

a. count = 3

if count = = 3:

print “hello”

print “ yana”

b. count = 3

if count > 5:

print “hello”

print “yana”

c. name = “yana”

if name = = “guy”:

print “ hello”

print “guy”

d. name = “yana”

if name = = “yana”:

print “hello”

print “ guy”

IF – ELSE statement

if expression:

Action 1

else:

Action 2

If expression is true Action1 will be executed, if expression is false Action2 will be executed.

Examples:

1. count = 5

if count % 2 = = 0 :

print “ the number is even “

else:

print “the number is odd “

2. operator means not equal , the same as !=

name = “yana”

if name < > “yana”:

print “ the name is not yana “

else:

print “ the name is yana”

Practice:

1. Write a program sign.py that will read one number and determines if the input number is positive or not. The program will prints an appropriate message.

2. Write a program Lilo.py that will read one name and will check if the name is equal to “LILO”. The program will print an appropriate message.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download