1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #!/usr/bin/python var = 100 if ( var = = 100 ) : print "Value of expression is 100" print "Good bye!" if 2 + 2 = = 4 : print "2 and 2 is 4" print "Arithmetic works." else : print "2 and 2 is not 4" print "Big Brother wins." #-------------------------------------------------------------- num = 15 if num < 10 : print "number is less than 10" elif num > 10 : print "number is greater than 10" else : print "number is equal to 10" #-------------------------------------------------------------- if 1 < 2 : print "1 is less than 2" elif 3 < 4 : print "3 is less than 4" else : print "Who moved my cheese?" #-------------------------------------------------------------- want_cake = "yes" have_cake = "no" if want_cake = = "yes" : print "We want cake..." if have_cake = = "no" : print "But we don't have any cake" elif have_cake = = "yes" : print "And it's our lucky day" else : print "The cake is a lie." #------------------------------------------------------------ phrase = "it marks the spot" for letter in phrase: if letter = = "X" : break else : print ( "There was no 'X' in the phrase" ) #--------------------------------------------------------------- tries = 0 while tries < 3 : password = input ( "Password: " ) if password = = "I<3Bieber" : break else : tries = tries + 1 else : print ( "Suspicious activity. The authorities have been alerted." ) #--------------------------------------------------------------- if age > 18 : print "adult person" for i in range ( 5 ): print i #--------------------------------------------------------------- age = 17 if age > 18 : print "Driving licence issued" else : print "Driving licence not permitted" #--------------------------------------------------------------- name = "Luke" if name = = "Jack" : print "Hello Jack!" elif name = = "John" : print "Hello John!" elif name = = "Luke" : print "Hello Luke!" # Hello Luke! else : print "Hello there!" #--------------------------------------------------------------- grades = [ "A" , "B" , "C" , "D" , "E" , "F" ] grade = "L" if grade not in grades: print "unknown grade" # unknown grade #--------------------------------------------------------------- sex = "M" age = 26 if age < 55 and sex = = "M" : print "a young male" # a young male #--------------------------------------------------------------- name = "Jack" if ( name = = "Robert" or name = = "Frank" or name = = "Jack" or name = = "George" or name = = "Luke" ): print "This is a male" #--------------------------------------------------------------- x = 10 y = 0 if (y ! = 0 and x / y < 100 ): print "a small value" #--------------------------------------------------------------- x,y = 10 , 40 if (x < y): st = "x is less than y" else : st = "x is greater than y" print (st) #--------------------------------------------------------------- total = 500 #country = "US" country = "AU" if country = = "US" : if total < = 50 : print ( "Shipping Cost is $50" ) elif total < = 100 : print ( "Shipping Cost is $25" ) elif total < = 150 : print ( "Shipping Costs $5" ) else : print ( "FREE" ) if country = = "AU" : if total < = 50 : print ( "Shipping Cost is $100" ) else : print ( "FREE" ) #--------------------------------------------------------------- a = 100 b = 100 if b > a: print ( "b is greater than a" ) elif a = = b: print ( "a and b are equal" ) else : print ( "a is greater than b" ) #--------------------------------------------------------------- a = 200 b = 33 c = 500 if a > b and c > a: print ( "Both conditions are True" ) #--------------------------------------------------------------- a = 6 b = 5 # Basic comparisons if a < b: print ( "a is less than b" ) if a > b: print ( "a is greater than b" ) print ( "Done" ) |
article
Tuesday, May 29, 2018
Python IF and Else Condition Example code
Python IF and Else Condition Example code