0% found this document useful (0 votes)
21 views

Python Notes

Uploaded by

karthikp207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python Notes

Uploaded by

karthikp207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python notes:

for DFT - import subprocess

-- windows cmd
python --version

import sys
print(sys.version)

- Python Interpreter
- Two modes to use python interpreter
1) Interactive mode

2) Script mode: we can store python script code in file with .py extension.

- Python Data Types


-Int, float, boolean, string
print(type(a)) // we can know the datatype
- variables
- print function: print('gd') or print("gd")
print('gd','mng','hyd') or print(gd mng hyd)
print('gd','mng',sep='/n')
//printing a string ;// using + and * in print
ex: a = "hello"
print(a+'bs')
print('It is' +2*''very'+'useful')// it is very very useful
- operators
a = b= z =25; gives same values
x,y,c = 1, 2.5, hi

- /, /n, /t
- comment : # single line
for multiple lines
''' '''
""" """
---------------------------------------------------
Day 2

id(), ord()
- id() // commonly used to check if two variables or objects refer to the same mem
location
ex: a = 5
id(a)
140982930992920
- ord() // used to convert single Unicode character into its integer
representation.

List, Tuple, Set, Dictionary


//these are four in-buit structures in python

- List: written with square brackets. mutable, ordered, allows duplicate


- similar to array.
- any datatype can be used in the list
- list =[1,2,3,'A','B',7,'hello']

example: l1 = [1,2,3,4.6, 'hello',true] or


l2 = list((1,2,3,4)) print(l2), print(type(l2))
[:3], [::]
print(l1)//prints full list,
print(l1[0:3]), print(l1[:3]), //prints specific elements in list
print(l1[::1])// full list
print(l1[-3:-1]) //prints in reverse list with neg indexes
print(l1[::-1])// prints in reverse direction
print(len(l1)) //prints size of list

l1.insert(1,'bs')// inserting at staring of list


l1.append('vlsi')//inserting at end of list
l2 =['dv','rtl']
l1.extend(l2)// extends or adds new elements
l1.remove('hello') // remove any element from list
remove, pop, del // we can remove any element from list

- Tuple: collection of objects. immutable.


- tuple =(2,1,10,4,7)
- Set: unindexed, unordered, duplicate not allowed
- s = {1,2,3,4}
- Dictionary

--------------------------------------------------------
Day 3
- Tuple: collection of objects. immutable: we cannot perform operations/ methods.
- tuple =(2,1,10,4,7)

example: t1 = (1,2,3)
print(type(t1))
t2 = (4,5)
print(t1 + t2)
print(len(t1))
l1 = list(t1) # convert tuple to list
l1.append(4)
t1 = tuple(l1) # convert list to tuple
print(t1)

t1 = (1,2,3)
(a,b,c) = t1 # assigning values to variables
t1 = (1,2,3,4,5,6)
(a,b,*c) = t1 # other values will be converted to list

- Set: unindexed, unordered, duplicate not allowed


- we cannot print reversing,
- using for loops we can reverse in set
- s = {1,2,3,4}
examples: s1 = {1,2,3}
print(s1)
print(type(s1))
print(s1[0]) # throws error, since unindexed
s1 = {1,2,3,3,2,True,0,False} # duplicates are not allowed
print(s1)
s2= {4,5}
s1.update(s2) # update, remove are some methods similar to list methods
-----------------------------------------------------------------------------------
---
Day 4

dictionary : ordered, changable, do not allow duplicates


- the values of the dictionary items can be of
any datatype (mixing of int,float,list)
- removing -pop and popitem
- adding elements: update method

example: d1 = {1:'h2',2.4:'hello',''bitsilica":"vlsi", "bs1"= {1,2,3,4,5}}


print(d1)
print(type(d1)), print(lel(d1)),
print(d1.get(1))
d1[1] = "world"
print(d1)
print(type(d1.get('bs1')))

- if loop

Examples: a =10;
if a==10:
print("a is 10") // press enter after :, so that if loop
will take indentation, if no indention: if loop is ended
- Alternative if

Example: a =input("enter value of a")


print(a)
a1 =10
print(type(a1))

- While loop
- for loop

for i in range(1,10):
print(i)
-----------------------------------------------------------------------------------
-----Day 5
Break and continue
- to alter the flow of a normal loop
- Example: list1 =[11,2,3,4,7]
list2 = []
for num in list1:
if num %2 ==0
l2.append(num)
else :
continue

for num in list1:


if num == 6;
print();
break

---
-List comprehension
- offers a shorter syntax when you want to create a new list
Example:l1 = [1,2,3,4,5]
l2=[]

for num in l1:


if num%2 ==0: l1.append(num)
print(l2)
l3 = [num for num in l1 if num%2 == 0]
print(l3)

-- Strings
- are surrendered by single or double quote
- similar to list
- modify strings, a.upper(), lower, strip, split, formatted strings
str1 = 'bitsilica'
print(str1)
print(type(str1))
print(str1[0:5])
for each_char in str1: print(each_char)
print(str1.upper())

print(f"first value in l1 is {l2[0]}") #formatted strings

-- file
- File is some information or data which stays in computer storage devices
Example: f = open("hello.txt",'w')
w- mode opens file for writing
if doesnot exist, it creates new file
x- creates new file
a- open file in append mode
r- mode opens file for read
if doesnot exist, it creates new file

file1 = open('output12.txt', 'w')


data = [1,2,3,4]
file1.write(str(data))
file1.close()

with open('output23.txt', 'w') as file1:


file1.write("hello bitsilica")

with open('output23.txt', 'w') as file2:


x= file2.read()
print(f"contents in output23 is :{x}")

------------------------------------------------------------------------------
Day 6
python modules
- Module is a file containing python definations and statements
- Module can define functions,claases,variables
Example: def add(a,b):
return a +b

def sub(10,12)

file handling'
x =file1.add(2,5)
print(f"value of x is {x}")
log.info(f"value of x is {x}")
log.debug(f"value of x is {x}")
log.error(f"value of x is {x}")
log.critical(f"value of x is {x}")
log.warning(f"value of x is {x}")

with open('logging.log', 'r') as file1:


data = file1.readlines()

error_lines = []

for each_line in data:


if "ERROR" in each_line:
error_lines.append(each_line)
else if "DEBUG" in each_line:
debug_lines.append(each_line)
else: none

with open ('error_file.txt', 'w') as file2:

----------------------------------------------------------------------------

You might also like