PYTHON SHORT NOTES
Use # to use ase a comment
Use ‘’’’ quotation to add more comments that wont add
If integers and proper functions in the code that will also not include in the code, will be
considered as comment
'''
conditionals
operators:
<
>
<=
>=
== if is equal true or false. checks for truth
!=
logical operators:
and
or
not filps the value
1
'''
# create boolean variables
'''''
x = True
y = False
print(x and y)
print(x or y)
'''''
# write a contiopn for printing all the numbers between 3 and 15
'''''
while number >= 5 and number <= 15
''''
'''
sequence = [1,2,3,4,5]
print(6 in sequence) checks if the element is present or not for both numbers and objects and
variables
can be also written as
print(6 not in sequence)
sequence = 'pasta and pizza'
print('a' in sequence)
will be returned as true
if added less charecters like pizz will return as true but if more charecters like pizzas will be
returning as false
'''
2
Loop used to repeat
Syntax for loop
For variable in sequence :
Instructions
The instruction will be forwarded
If printed separately, it will be removed from
the loop
For repeating
3
For counting instead of len
True or false
4
Single = is used to assign variable
Use != for different variables
5
Greater or smaller than
While loop
6
With while loop, you have to do it
manually or infinite loop printed
Needs to be in the same forwarded
gap to run the code
+1 prints all the numbers
You can add <= which will give
one more repeat
To count
The condition can be changed
Q1.
7
While vc for lope
8
For while loop
Separate both the results of the code with \n
<= gives error
Q2.
For loop only works in sequence
9
Q3
10
Q4
11
Range
Be aware of :
Or else code wont come without proper lining
Listing
12
While using list, brackets are included in the results
But if string is used no brackets
To list it in a sequence
A bit complex way to show listing
13
For 1, its position 2 as counting starts from 0
# create a new list
14
Listing variables manually
Method 1:
Should be separated by coma
Position should be indicated by numbers starting from 0 in brackets
Use insert command, followed by position and the function
List may contain any objects
15
Method 2:
Append means to add in the end
???
Deleting elements from list
Pop function is opposite to append
Manga.pop(position)
if condition:
instruction
< ,> , --, !=, >=, <=
16
logical operators
and, or, not, in
True and True
results: true
T has to be capital
True and False
results: false
true or false
results: true
'''
to check a certain element
our_string = 'pizza'
char = 'i'
check = char in our_string
print(check)
To check smaller or greater
x=5
17
if x < 10:
print('smaller than 10')
print('done')
# check if x is in [0, 10]
x = 13
if x > 0:
print('after first check')
if x < 10:
print('within the range')
print('done')
# check if x is in [0, 10] in a neater way
x = -13
if (x > 0) and (x < 10):
print('within the range')
to check but not very appropriate
x=3
if x > 10:
print('bigger than 10')
if x < 10:
print('smaller than 10')
if x == 10:
print('equal to 10')
ADD ‘elif’ SO IF THE CONDITION IS TRUE THEN THE FOLLOWING COMMAND WILL BE SKIPPED
ADD ‘else’ SO TO JUMP OVER
THE ELSE COMMAND HAVE NO CONDITION
IF PREVEOUS COMMAND DO NOT MATCH THE ELSE COMMAND WILL PRINT
inp = input('Insert temperature: ')
new_temperature = int(inp) + 10
print(new_temperature)
print('done')
18
IN TRY AND ACCEPT METHOD:
inp = input('Insert temperature: ')
try:
new_temperature = int(inp) + 10
print(new_temperature)
except:
print('error in calculation')
print('done')
#write a program that printd all
the elements of a list
my_str = 'hello'
counter = 0
while counter < len(my_str):
print(my_str[counter])
counter += 1
write a program that checks
if a number is prime
a prime number is a number
that is divisible by only 1 and
itself
0 and 1 are not prime
numbers
# define the number to check
19
number = 4
# check if it is 0 or 1
if number == 0 or number == 1:
print('is not prime')
elif number > 1:
for n in range(2,number):
if number % n == 0:
print("is not prime")
break
else:
print("is prime")
Use break only to break the loop
# define the number to check
number = 10
## version 2 by using a flag variable
# if i find a factor, i set flag to true
flag = False
if number == 0 or number == 1:
20
number('is not prime')
elif number > 1:
for n in range(2,number):
if number % n == 0:
# we found a factor
flag = True
break
if flag == True:
print('is not prime')
else:
print('is prime')
casting
n=5
n_str = str(n)
print(n_str)
res = n + 5
adding numbers
# function definition
def add10(number):
new_number = number + 10
print(new_number)
#### MAIN FUNCTION ####
x=3
add10(x)
implement the len function from scratch
def MyLen(input_data):
counter = 0
for i in input_data:
counter = counter + 1
print(counter)
### MAIN ####
our_data = 'some text'
data_len = MyLen(our_data)
21
print(data_len)
OR
def MyLen(input_data):
counter = 0
for i in input_data:
counter = counter + 1
return counter
### MAIN ####
our_data = 'some text'
data_len = MyLen(our_data)
print(data_len)
OR
ADDITION
def AddFive(number):
return number + 5
###main###
result = AddFive(5)
print(result)
22
for empty value
def AddFive(number = 0):
return number + 5
###main###
result = AddFive()
print(result)
OR TO GET A RESPONSE
def AddFive(number = None):
if number == None:
print('please provide a
number')
else:
return number + 5
###main###
result = AddFive()
print(result)
ef AddFive(number = None):
if number == None:
print('please provide a number')
else:
return number + 5
###main###
result = AddFive(20)
if result!= None:
print(result)
23
index
data = [0,1,2,3,4,5,6]
index = 0
while index < len(data):
print(data[index])
index = index + 1
can be <= o to include 0
FOR GETTING SPECIFIC NUMBERS
data = [0,1,2,3,4,5,6]
part = data[0:3]
print(part)
TO GET SEQUENCIAL DATA
data = [0,1,2,3,4,5,6]
part = data[::2]
print(part)
TO GET THE LEFT MOST ELEMENT
data = [0,1,2,3,4,5,6]
print(data[-1])
-0 doest not exists. Use -1, -2, -3….
TO SKIP ELEMENTS
data = [0,1,2,3,4,5,6]
print(data[0:5: 1])
24
25