Python Note With Code
Python Note With Code
Python Note With Code
print('Numpy :{}'.format(numpy.__version__))
import math
print(f'The Degree of {x} is:',math.degrees(x)) # using single quote with {x} is used to display entered
values
math.radians(x)//prints radians
print(name)
print(name[-7:-1])
print(name[3:10]) #prints all characters starting from index 3 to end index 7 with excluding others.
print(sentence.strip()) # removes the white space from statring end and last end.
print(len(sentence))
#Lists in Python used too store different data types and it is mutable eg list=[3.2,'Dere',4]
list1=[1,2,3]
print(list1[0:2]) #prints values starting from index 0 to index 1 that mean it excludes the value of index 2
list2=['Dere','Hewi','Kuku']
print(list3[0]) #[1, 2, 3] because it assigns index 0 to list1 elements and index 1 to list2 elements#
#NB: You can't use the following methods the same time with print() method.Use them diffirently
list1.append(4) # it appends the number 4 at the end of the list NB: you can append only one element at
a time
print(list1)
list1.insert(1,5) # inserts the new single value between lists elements and it takes two parameters those
refer index and its value
print(list1) # output:[1, 5, 2, 3, 4]
list1.remove(4) #it removes the element 4 from the list NB: It takes element to be removed but not index
print(list1) #output:[1, 5, 2, 3]
list1.pop(1) # it takes index but not element. Here it removes the index 1 element which is 5
print(list1) # output:[1, 2, 3]
list1.pop() # If you did not provide the index number, then it pops the element from the end as stack does
which is 3 will be poped
print(list1) #[1, 2]
del list2[1:] # del method used to remove many elements from the list. You have to provide index
numbers.Here it removes all elements startrting from index 1.
list1.extend([6,7,8,9]) # used to add many elements to the list. Accepts only elements to be added in to the
list rear part but not indexes
print(list1) #output:[1, 2, 6, 7, 8, 9]
print(list1)
tup=(1,3,6,2)
list1[1]=3 # it is possible to change the list value but in tuple changing is impossible
print(list1)
print(tup[0])
print(tup[1:4])
# tup[1]=3 => impossible because it is immutable
print(len(tup))# possible
#Set in Python=> It is a collection of unique element and it uses curly bracket {}. It has no
sequence(indexing) like others
set1={3,6,1,8,9}
print(set1)
set1={98,14,8,3,7,98}
print(set1)
#Dictionary or Mapping in python=====> uses curly barces==> {}. It has no Index value but we can
provide unique key values as index.
#How to display the address of a variable=====> By using built-in method called id()
#NB:One or more variables can point the same value in the memory with the same address content.
x=3
print(x)
print(id(x)) #output:140726574126816 # id() is a pyhton built-in method that displays the address of a
variable.
print(y)
print(id(y)) # output:140726574126816
# x and y pointing the same value 3 and they have the same address
# One or more variables can point the same value in the memory with the same address content
#In python we don't have char type but we can write the string as char!
# none means a variable does not assined any value;which is the same with null in other programming
languages.
py=3
print(type(py))#<class 'int'>
print(py)#3
pyf=float(py)
print(type(pyf))#<class 'float'>
print(pyf)#3.0
complex1=3+2j
print(complex1)# (3+2j)
print(type(complex1))#<class 'complex'>
a=4
b=2
print(complex(a,b))#(4+2j) #it converts two integer(float) numbers in to complex number. Vice versa is
may not true
bool1=True # bool==> True has integer value 1 and False has integer value 0. We can convert boolean
value in to integer
print(bool1)
print(int(bool1))#output:1
#what about if we want to display all odd numbers starting from 3 to 25.
print(list(range(3,26,2))) #Here number 3 is the starting odd number, Number 25 is the last odd number to
be displayed and number 2 is the increamental value.
# Arithematic (+,-,*,/) ======> especial operators(x//y==> used to retun only integer value)
val2=2
print("Subtraction:",val1+val2)
print("Division:",val1/val2) # output:5.0,prints float value. if we want to display integer value, we can use
as val1//val2
print("Multiplication:",val1*val2)
#a,b=6,2 ===> In python assigning variable with values in a single line is possible by using comma.
x=7
print(x)
x=-x
print(x)
a,b=6,2
print(a<b)#outpu:False
x,y=5,4
bool1=False
bool1=True
print(not bool1)
print(0b1010) # output: 10 ====> mean that we have provided binary number and the compiler displays
its decimal equivalent number
print(oct(10)) # output:0o12 ===>the 0o(zero with alphabet o) tells us the number is octal
print(0o12) #output: 10
print(hex(10)) #output:0xa ===> the 0xa(zero with x) tells the number is hexadecimal. NB: in
hexadecimal the number 10 is refered by a.
print(0xa) #output:10
#1. complement(~)
#It first converts the given decimal numbers in to 8 digit binary numbers and then performs 1's
complement===>output
# Note:We don't store a negative numbers in the computer but we store positive numbers. To store
negative numbers, we have to convert them to the positive number
print(~10) #Here first it converts decimal 10 to 8 digit binary and then performs 1's complement. You will
get 11110101
#output:-11, Take postive 11, convert it in to 8 digit binary and then again convert it in to 2's
complement. You will get also 11110101
#2. Bitwise And===> &, which is a little bit different from Logical And(used for comparson)
print (5&6) #output:4, How?, Convert each decimal number in to 8 digit binary and perform AND
GATE(AND Truth Table).Finally, it prints binary equivalent decimal num.
print(5|6) #output:7, The operation is the same to above but it performs OR GATE(OR Truth Table)
operation
#2. Bitwise XOR===> ^, It provides output 1,if both values have different binary number i.e
(1^0=1,0^1=1,0^0=0,1^1=0)
print(5^6)#output:3, The operation is the same to above bitwise operators but it performs XOR
GATE(XOR Truth Table) operation.Finally, it prints binary equivalent decimal num.
#2. Bitwise leftshift==> <<, First convert the given decimal number in to 4 digit binary and then add extra
zeros' to the right side.Finally, find its equivalent decimal num.
print(5<<2) # convert 5 in to 4 digit binary and add to it 2 extra zeros' to the right side. Then convert it in
to equivalent dec num.
#3. Bitwise rightshift==> >>, First convert the given decimal number in to 4 digit binary and then remove
digits from right side.Finally, find its equivalent decimal num.
print(5>>2)# output:1, 5(0101),after removing 2 bits from right side, we will get(01),The equivalent
decimal num=1.
#Mathematical Functions
#To use mathematical methods, you have to import the 'math' module
import math
x=math.sqrt(49)
print(x)
#Again we can import the modules by providing our name to use our time efficiently
import math as m # m is user defined variable. Here we can both of them as shown below
print(m.sqrt(25))
print(math.sqrt(25))
#if you want to import a specific methods, you can write as follows. So it is not mandatory using (math.).
print(pow(2,3))
print(sqrt(25))
print()
while i>=1:
j=1
while j<=5:
print(i, " ",end="")
j=j+1
print()
i=i-1
print("Number Pattern 5:")
for i in range(5,0,-1):
for j in range(5,0,-1):
print(j, " ",end="")
print()
import time as t
import calendar as cal
localtime=t.asctime(t.localtime(t.time()))
print("The Date is:",localtime)
ca=cal.month(2021,4)
print("Calendar for April:")
print(ca)
import math
root=tkt.Tk()
root.title("Calculator")
expression=""
def add(value):
global expression
expression+=value
label1.config(text=expression)
def clear(value):
global expression
expression=""
label1.config(text=expression)
def calculate():
global expression
result=""
if expression!="":
try:
result=eval(expression)
expression=str(result)
except:
result="error"
expression=""
label1.config(text=expression)
label1=tkt.Label(root,text=" ",bg="Blue",width=15)
# Lambda method is used to call the methods with braces and arguments
#Unless the lambda is not used, after 'command' the method should be written its name only.
button1=tkt.Button(root,text="1",command=lambda: add("1"))
button1.grid(row=1,column=0)
button2=tkt.Button(root,text="2",command=lambda: add("2"))
button2.grid(row=1,column=1)
button3=tkt.Button(root,text="3",command=lambda: add("3"))
button3.grid(row=1,column=2)
button_div=tkt.Button(root,text="/",command=lambda: add("/"))
button_div.grid(row=1,column=3)
button4=tkt.Button(root,text="4",command=lambda: add("4"))
button4.grid(row=2,column=0)
button5=tkt.Button(root,text="5",command=lambda: add("5"))
button5.grid(row=2,column=1)
button6=tkt.Button(root,text="6",command=lambda: add("6"))
button6.grid(row=2,column=2)
button_mult=tkt.Button(root,text="*",command=lambda: add("*"))
button_mult.grid(row=2,column=3)
button7=tkt.Button(root,text="7",command=lambda: add("7"))
button7.grid(row=3,column=0)
button8=tkt.Button(root,text="8",command=lambda: add("8"))
button8.grid(row=3,column=1)
button9=tkt.Button(root,text="9",command=lambda: add("9"))
button9.grid(row=3,column=2)
button_sub=tkt.Button(root,text="-",command=lambda: add("-"))
button_sub.grid(row=3,column=3)
button_clear=tkt.Button(root,text="C",command=lambda: clear(" "))
button_clear.grid(row=4,column=0)
button0=tkt.Button(root,text="0",command=lambda: add("0"))
button0.grid(row=4,column=1)
button_point=tkt.Button(root,text=".",command=lambda: add("."))
button_point.grid(row=4,column=2)
button_add=tkt.Button(root,text="+",command=lambda: add("+"))
button_add.grid(row=4,column=3)
button_eqauls.grid(row=5,column=0,columnspan=4)
root.mainloop()
x=98
print(chr(x))# prints b
import random
random1=[2,6,8,3,0]
print(random.choice(random1))
class Fer_to_cel:
user_input=""
def getinput(self): # adding self as argument is mandatory for methods which are found in the class.
pass
def calculates(self):
try:
number=float(fer)
cel=(fer-32.0)*(5.0/9.0)
except:
Fer_to_cel.user_input=""
tempr=Fer_to_cel()
tempr.calculates()
arr= ary.array('i',[4,6,1,9,7])
arr=array('i',[3,5,8,9,1])
print("Array created:",arr)
arr[4]=10
print(arr)
import os
f = open('C:/Users/KUKU/Desktop/file.txt', 'r')
print(f.read())
f.close()
import os
f = open('C:/Users/KUKU/Desktop/file.txt', 'r')
f.close()
import os
f = open('C:/Users/KUKU/Desktop/file.txt', 'r')
f.close()
import os
print(file.readlines())
f.close()
f = open('C:/Users/KUKU/Desktop/file.txt', 'w')
f.close()
f=open('C:/Users/KUKU/Desktop/pythonfile.txt', 'x')
f.close()
os.remove('C:/Users/KUKU/Desktop/pythonfile.txt')
print(datetime.date,today())
import cv2
file='E:/Camera/deree.jpg'
image1=cv2.imread(file)
image2=cv2.imshow('image',image1)
image3=cv2.resize(image2,(224,224))
print(image3)
Build a Mobile Application With the Kivy Python Framework – Real Python