hi = "hello there"
name = "ajay" greeting = hi + " " + name #"hi ajay" silly = name * 3 #"ajayajayajay"
print
print
x = 1 print(x) # 1 x_str = str(x) print("my fav num is", x, ".", "x =", x) #my fav num is 1 . x = 1 print("my fav num is " + x_str + ". " + "x = " + x_str) #my fav num is 1. x = 1(notice the difference in spaces).
input
text = input("Type anything... ") # say we type "hello" print(2*text) # hellohello
input
gives you a string so must cast if working with numbers
num = int(input("Type a number... ")) print(5*num)
int
, float
, string
i
and j
are variable names.i > j i >= j i < j i <= j i == j i != j
bools
a
and b
are variable names (with Boolean values)
not a
a and b
a or b
Example:
study_time = 15 sleep_time = 8 good_marks = (study_time > sleep_time) good_sleep = not (sleep_time < 8) both = good_marks and good_sleep print(both)
if <condition>: <expression> <expression>
Form 2:
if <condition>: <expression> <expression> else: <expression> <expression>
Form 3:
if <condition>: <expression> <expression> elif <condition>: <expression> <expression> else: <expression> <expression>
<condition> has a value of True
or False
.
Evaluate expressions in that block if <condition>
is
True
.
x = float(input("Enter a number for x: ")) y = float(input("Enter a number for y: ")) if x == y: #syntax error is use x=y here! print("x and y are equal") if y != 0: print("therefore, x/y is", x/y) elif x < y: print("x is smaller") else: print("y is smaller") print("thanks!")
Example:
You are in the Lost Forest. ************ ************ Karel ************ ************ Go left or right?Each time Karel goes right, it gets lost in a forest. As soon as it goes left, it gets out of the forest.
Try this with if-else
:
if input("You're in the Lost Forest. Go left or right? ") == right: if input("You're in the Lost Forest. Go left or right? ") == right: if input("You're in the Lost Forest. Go left or right? ") == right: if input("You're in the Lost Forest. Go left or right? ") == right: if input("You're in the Lost Forest. Go left or right? ") == right: .... #goes on forever else: print("You got out of the Lost Forest!") else: print("You got out of the Lost Forest!") else: print("You got out of the Lost Forest!") else: print("You got out of the Lost Forest!") else: print("You got out of the Lost Forest!")
To do this, we need a new construct:
while <condition>: <expression> <expression> ...
True
, do all the steps inside the while code block.<condition>
is False
.
n = input("You're in the Lost Forest. Go left or right? ") while n == "right": n = input("You're in the Lost Forest. Go left or right? ") print("You got out of the Lost Forest!")
while
and for
loops# more complicated with while loop n = 0 while n < 5: print(n) n = n + 1
Shortcut with for
loop:
for n in range(5): print(n)
for
Loops
for <variable> in range(<some_num>): <expression> <expression> ...Each time through the loop,
variable
takes a value. First time, variable
starts at the smallest value. Next time, variable
gets the prev value + 1.
range(start, stop, step)
. The default values are start=0
and step=1
. Loop until value is stop-1
.
mysum = 0 for i in range(7, 10): mysum += i print(mysum) mysum = 0 for i in range(5, 11, 2): mysum += i print(mysum)
break
statementwhile <condition_1>: while <condition_2>: <expression_a> break <expression_b> <expression_c>
Example:
mysum = 0 for i in range(5, 11, 2): mysum += i if mysum == 5: break mysum += 1 print(mysum)Output?
for
vs. while
loopsfor loops |
while loops |
---|---|
know number of iterations | unbounded number of iterations |
can end early via break
|
can end early via break
|
uses a counter | can use a counter but must initialize before loop and increment it inside loop |
can rewrite a for loop using a while loop
|
may not be able to rewrite a while loop using a for loop
|