Python Notes
Python Notes
-- 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.
- /, /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.
--------------------------------------------------------
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
- 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
- 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
---
-List comprehension
- offers a shorter syntax when you want to create a new list
Example:l1 = [1,2,3,4,5]
l2=[]
-- 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())
-- 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
------------------------------------------------------------------------------
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}")
error_lines = []
----------------------------------------------------------------------------