Download presentation
Presentation is loading. Please wait.
Published byAnnis Suzanna Mills Modified over 8 years ago
1
Python Fundamentals: If Statements and Functions Eric Shook Department of Geography Kent State University
2
If statements if(1<2): print "1 IS less than 2!" If statement An if conditional statement will execute all indented code if the condition within the parenthesis evaluates to be true. if(condition): do something
3
If statements if(1<2): print "1 IS less than 2!" If statement An if conditional statement will execute all indented code if the condition within the parenthesis evaluates to be true. if(condition): do something 1 IS less than 2! The Result
4
If statements if(1>2): print "1 IS greater than 2!" If statement An if conditional statement will execute all indented code if the condition within the parenthesis evaluates to be true. if(condition): do something The Result
5
If statements if(condition):...do something in Python... Example conditions ConditionOperatorExampleT/F Equal to==(1 == 1) T Not equal to!=(1 != 1)F Greater than >(1 > 1)F Gt /equal to>=(1 >= 1)T Less than <(1 < 1)F lt /equal to<=(1 <= 1)T
6
If statements if(condition):...do something in Python... Example conditions ConditionOperatorExampleT/F Equal to==(1 == 1) T Not equal to!=(1 != 1)F Greater than >(1 > 1)F Gt /equal to>=(1 >= 1)T Less than <(1 < 1)F lt /equal to<=(1 <= 1)T These examples execute the … do something...
7
If / else statements if(condition):...do something in Python... else: …do something else... If condition is true such as (1==1) then execute the first portion of indented code if the condition is false such as (1<1) then execute the second portion of indented code. Note: Only one portion is ever executed.
8
If / else statements x=5 y=10 if(x>=5): if(y!=10): print "option A" else: print "option B" else: if(y<11): print "option C" else: print "option D" ????? The Result
9
If / else statements x=5 y=10 if(x>=5): if(y!=10): print "option A" else: print "option B" else: if(y<11): print "option C" else: print "option D" option B The Result
10
If / else statements x=4 y=10 if(x>=5): if(y!=10): print "option A" else: print "option B" else: if(y<11): print "option C" else: print "option D" The Result
11
If / else statements x=4 y=10 if(x>=5): if(y!=10): print "option A" else: print "option B" else: if(y<11): print "option C" else: print "option D" option C The Result
12
If / else statements x=4 y=13 if(x>=5): if(y!=10): print "option A" else: print "option B" else: if(y<11): print "option C" else: print "option D" The Result
13
If / else statements x=4 y=13 if(x>=5): if(y!=10): print "option A" else: print "option B" else: if(y<11): print "option C" else: print "option D" option D The Result
14
If / else statements x="R" y="" if(x=="S"): if(y!="T"): print "option A" else: print "option B" else: if(y=="T"): print "option C" else: print "option D" option D The Result
15
Functions def greeting(first,last): print("Hello",first,last) print("") print("It is nice to meet you.") Function A function is defined using the def keyword. Functions can accept arguments and execute code that is indented. Statements within functions must be indented. def function_name( parameters ) Python code
16
Functions - comparison function myfunct(first,last) { print "Hello "+first+" "+last+"."); document.write(" "); document.write("It is nice to meet you."); } myfunct("Harry","Potter"); def f(v1,v2,v3): print "v1=",v1 print "v2=",v2 print "v3=",v3... f(1,2,3) f(3,9,10) f(2,13,4) f(6,7,8)... Using functionsNot using functions print "v1=1" print "v2=2" print "v3=3" print "v1=3" print "v2=9" print "v3=10" print "v1=2" print "v2=13" print "v3=4" print "v1=6" print "v2=7" print "v3=8"
17
Functions - comparison function myfunct(first,last) { print "Hello "+first+" "+last+"."); document.write(" "); document.write("It is nice to meet you."); } myfunct("Harry","Potter"); def f(v1,v2,v3): print "v1=",v1 print "v2=",v2 print "v3=",v3... f(1,2,3) f(3,9,10) f(2,13,4) f(6,7,8)... Using functionsNot using functions print "v1=1" print "v2=2" print "v3=3" print "v1=3" print "v2=9" print "v3=10" print "v1=2" print "v2=13" print "v3=4" print "v1=6" print "v2=7" print "v3=8"
18
Function return def greetingstring(first,last): string="Hello",first,last," How are you." return string print greetingstring("Bob","Smith") Function A function can return a value to the code that called it def function_name( parameters ) Python code return variable_or_value
19
Functions Functions are a powerful way to reduce the amount of repetitive code you have to write They can also be used to organize your code by moving long related code statements into functions Functions can also be confusing It may take a little while to understand when a function should be used and when it should not be used. Try to experiment.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.