- syntax and semantics
- scalar objects
- simple operations
- expressions, variables and values
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
08/03/2023
hi = "hello there"
name = "Burkay" greet = hi + name greet
## 'hello thereBurkay'
greeting = hi + " " + name greeting
## 'hello there Burkay'
silly = hi + " " + name * 3 silly
## 'hello there BurkayBurkayBurkay'
len()
name = "Burkay" len(name)
## 6
surname = "Genc" fullname = name + " " + surname len(fullname)
## 11
fullname
## 'Burkay Genc'
fullname[0]
## 'B'
fullname[1]
## 'u'
fullname[5]
## 'y'
lastCharacterIndex = len(fullname) - 1 fullname[lastCharacterIndex]
## 'c'
fullname[-1]
## 'c'
# The string is : Burkay # The indices are : 012345 name[2:4]
## 'rk'
str[i:j]
gives you characters at indices:
i, i+1, i+2, ..., j-1
fullname
## 'Burkay Genc'
fullname[:5]
## 'Burka'
fullname[5:]
## 'y Genc'
fullname[:]
## 'Burkay Genc'
fullname[-4:]
## 'Genc'
fullname[:-4]
## 'Burkay '
fullname[2:-2]
## 'rkay Ge'
print
x = 1 print(x)
## 1
print
adds a space between parameters. To avoid the space, you should concatenate the parameters yourself.print("my fav num is", x, ".", "x =", x)
## my fav num is 1 . x = 1
x_str = str(x) print("my fav num is " + x_str + ". " + "x = " + x_str)
## my fav num is 1. x = 1
text = input("Type anything... ") print(5*text)
## Type anything... 5 ## 55555
input
gives you a string so you must cast if working with numbersnum = int(input("Type a number... ")) print(5*num)
## Type a number... 5 ## 25
your_name is your_age years old.
## What is your name? Burkay ## How old are you? 41 ## Burkay is 41 years old.
i
and j
are variable namesi > j i >= j i < j i <= j
True
if i
is the same as j
i == j
True
if i
is not the same as j
i != j
5 > 3
## True
a = 7 b = 5 b >= a
## False
a == b
## False
a == b + 2
## True
a != b
## True
b = "burkay" c = "cemal" d = "davut" b == c
## False
c < d
## True
b > d
## False
b = "burkay" B = "Burkay" B < b
## True
b < B
## False
a
and b
are variable names (with Boolean values)not
negates the boolean valueand
returns True
only if both operands are True
or
returns False
only if both operands are False
a | b | not a | a and b | a or b |
---|---|---|---|---|
True | True | False | True | True |
True | False | False | False | True |
False | True | True | False | True |
False | False | True | False | False |
a = 5 b = 7 c = 3 a > b
## False
not (a > b)
## True
(a > b) and (b > a)
## False
(a > b) or (b > a)
## True
(a < c) or ((b > c) and not (a < c))
## True
pset_time = 15 sleep_time = 8 print(sleep_time > pset_time)
## False
derive = True drink = False both = drink and derive print(both)
## False
if/else
keywords for that:if <condition>: <expression> <expression> ... elif <condition>: <expression> <expression> ... else: <expression> <expression> ...
elif
keyword:if <condition>: <expression> <expression> ... elif <condition>: <expression> <expression> ... elif <condition>: <expression> <expression> ... else: <expression> <expression> ...
a = 5 b = 3 if (a < b): print("a is less than b.") else: print("a is greater than b.")
## a is greater than b.
a = 7 b = 3 if a > b: print(a) else: print(b)
## 7
a = 7 b = 3 if a > b: print(a) elif a < b: print(b) else: print("they are equal")
## 7
# block level 1 do_something() if True: # block level 2 do_something() if True: # block level 3 do_something() # block level 2 do_something() # block level 1 do_something()
if False: print("This shouldn't print.") print("But this should.")
## But this should.
vs.
if False: print("This shouldn't print.") print("And this shouldn't print as well.")
x = float(input("Enter a number for x: ")) y = float(input("Enter a number for y: ")) if x == y: 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!")
x = 5 if x == 5: print("This will be accepted and printed.")
## This will be accepted and printed.
if x = 5: print("This won't be accepted by Python.")
## Error: invalid syntax (<string>, line 1)
numberOfXs = int(input("Enter a number:")) if numberOfXs == 1: print("X") elif numberOfXs == 2: print("XX") elif numberOfXs == 3: print("XXX") elif numberOfXs == 4: print("XXXX") # ... # where to stop???
numberOfXs = int(input("Enter a number:")) print(numberOfXs * 'X')
aNumber = int(input("Enter a number:")) if aNumber == 1: print("1") elif aNumber == 2: print("1 2") elif aNumber == 3: print("1 2 3") elif aNumber == 4: print("1 2 3 4") elif aNumber == 5: print("1 2 3 4 5") ## Where to stop???
For each student in this class, ask his name
Water every flower in this room
For each number from 1 to 10, compute square of that number
Given k, compute k!
while
loopwhile <condition>: do_something
do_something
again and again until <condition>
becomes False
<condition>
never becomes False
, then the program executes forever!a = 0 while a < 5: print(a) a = a + 1 # Don't forget this!
## 0 ## 1 ## 2 ## 3 ## 4
a = 10 while a > 0: print(a) a = a - 1
## 10 ## 9 ## 8 ## 7 ## 6 ## 5 ## 4 ## 3 ## 2 ## 1
number = 6 factorial = 1 # starting with 1 is important! while number > 1: factorial = factorial * number number = number - 1 print(factorial)
## 720
OR
number = 6 factorial = 1 # starting with 1 is important! i = 1 while i <= number: # why <= and not < ??? factorial = factorial * i i = i + 1 print(factorial)
## 720
number = 36 i = 1 while i <= number: if number % i == 0: print(i) i = i + 1
## 1 ## 2 ## 3 ## 4 ## 6 ## 9 ## 12 ## 18 ## 36
i = 1 while i < 100: if i % 7 == 0: print(i, " ", end = "") elif i % 13 == 0: print(i, " ", end = "") elif i % 19 == 0: print(i, " ", end = "") i = i + 1
## 7 13 14 19 21 26 28 35 38 39 42 49 52 56 57 63 65 70 76 77 78 84 91 95 98
i = 1 while i < 100: if i % 7 == 0: print(i, " ", end = "") if i % 13 == 0: print(i, " ", end = "") if i % 19 == 0: print(i, " ", end = "") i = i + 1
True
if the word contains the letter x (or X), otherwise it will return False
.* *** ***** *******
True
if the word exists in the sentence. Otherwise, it returns False
+----+ | | +----+