PROGRAMMING WITH PHYTON
variables
variable is a name that refers to a value
message = ‘ good morning’
greeting= ‘good’
morning= ‘morning’
print(greeting+ morning)
Data type
data type is the kind of value that the data represents
(Text)String — str=A sequence of characters: “hello”
(Numeric)Integer — int=Whole numbers: 3 000
(Numeric)Float — float=Numbers with decimal point: 2.5
(Boolean) Boolean — bool= Logical value indicating True or False
(Sequence)List — list=Ordered sequence of objects: [10, “hello”, 200.3]
(Sequence)Tuple — tuple=Ordered immutable sequence of objects: (10, “hello”, 200.3)
(Mapping)Dictionary — dict=Unordered Key Value pairs: {“name”: “Maddie”, “age”: 18}
(Set)Set — set=Unordered collection of unique objects: {“apple”, “orange”, “durian”}
boolean
a boolean value is either true or false.
Comparison operators:
== (equal to)
!= (not equal to)
< (less than)
<= (less than or equal to)
> (greater than)
>= (greater than or equal to)
2. string
sequence of characters inside the quotation marks, e.g. ‘Hello, World!’
string operators
x in s
x not in s
s +t
s*n , n*s
s[i]
len(s)
3. string slicing
string [start: stop]
string [start: slice: stop]
input/output
Built-in Math Functions (in Python)
abs()
print(abs(101))
print(abs(-1,01))
round()
print(round(1.2345,3))
logical operators
control flow
- One-way statement (if)
- Two-way statement (if-else)
- Multi-way statement (if-elif-else)
list
fruits = [‘apple’, ’banana’, ‘watermelon’]
numbers= [10, 8.5,9]
(print)fruits
(print)nunbers
accessing a list
fruits=[‘apple’, ’banana’, ’watermelon’]
print( clasmmates [0])
editing a list
fruits=[‘apple’, ’banana’, ’watermelon’]
fruits[0]=[‘orange’]
print(fruits)
combining a list
fruits=[‘apple’, ’banana’, ‘watermelon’]
neighbours=[lisa]
friends= classmates + neighbours
print(friends)
loops
- for loops
fruits=[‘apple’, ’banana’]
for x in fruits:
print(x)
for x in range (2,10,2):
print(‘number:’, x)
2. while loops
score=1
while score<7
print(score)
score+=2
3. nested loops
adj=[‘fresh’, ’green’]
fruits=[‘apple’, ’banana’]
for x in adj:
for y in fruits:
print(x, y)
function
sum=0
for i in range(1,11):
sum+=1
print(‘sum between1 to 10:’, sum)