- Dveoped by Guido Van Rossum in 1980s.
- He Devloped this language while working on a programming language named ABC.
- Name python is taken from the T.V show in America The Complete Monty Python's Flying Circus.
- Huge community Support.
- Future is AI.
- Easy to learn and Implement.
- General Purpose programming language.
- Highly Extensible.
- Simple and straight forword.
- Dynamic Type language. (varible declare at run time)
- Large libraries.
- Platform independent.
- Precise Coding.
- Ranked #1 (TIOBE)
- Reduce code almost 1/6th times.
- Devloping website.
- Task automation.
- AI, ML, IOT.
- Data Analysis.
- Data Visualisation.
- Devloping desktop & mobile applications.
-
It is type of software which enable our python code to run on multiple platforms.
-
It takes byte code instruction to convert into machine code instruction and display final output.
Note: PVM is machine dependent.
- PVM consists two things 1.Interpreter 2. Memory Manager.
- Interpreter: It executes the code line by line.
- Compile file is mainly given when we have to delvier the software after completion.
CMD: python3 -m py_compile your_script.py
-
Comments helps reader to understand the code which is written by other programmers.
-
Comments were ignored by python interpreter.
- Single line comment.
- Multiple line comment.
- Use # symbol.
# Sum of two numbers- Use """ """
"""
This is my multiline comment
"""-
Variables are the memory locations where data is saved.
-
In Python, no variable declaration is required like c or c++
# variable
a = 10- Garbage value: means no variable is referring to that particular object.
- Id: Id belongs to that particular object.
- It is combination of alphabet, digit, and underscore.
- cannot start with digit.
- variable names is case sensitive.
- Keywords cannot be used as variable names.
- To delete a variable use del keyword along with variable name.
- Example:
del a
- use
type(variable_name)
int: Integer numbersfloat: Floating-point numberscomplex: Complex numbers
list: Ordered, mutable sequencestuple: Ordered, immutable sequencesrange: Immutable sequence of numbers
str: Strings (immutable sequences of Unicode characters)
dict: Dictionaries (key-value pairs)
set: Mutable setsfrozenset: Immutable sets
bool: Boolean values (True or False)
bytes: Immutable sequences of bytesbytearray: Mutable sequences of bytesmemoryview: Memory views of bytes-like objects
NoneType: The None object, representing null or no value
# Numeric types
integer = 42
float_number = 3.14
complex_number = 1 + 2j
# Sequence types
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_range = range(5)
# String
my_string = "Hello, World!"
# Dictionary
my_dict = {"key": "value"}
# Set types
my_set = {1, 2, 3}
my_frozenset = frozenset([1, 2, 3])
# Boolean
is_true = True
# Binary types
my_bytes = b"hello"
my_bytearray = bytearray(5)
my_memoryview = memoryview(bytes(5))
# None
my_none = NoneNote: If you delete x variable then it will only delete the reference varaible but it will not delete the instance object. Then those objects also become garbage value.
- It is a program invoked by python itself whenever required whose job is to release garbage blocks.
- It is also known as automatic garbage management.
-
It is mainly used to display the values on the monitor.
-
whenever you try to print multiple values in print then it will print those values by adding a space as default seperator.
-
You can custimize your seperator using sep="" in print function.
- Here a comma and space will be treated as seperator
print(a,b,c,sep=", ") # 10, (10+3j), 10print(a,b) # 10 (10+3j)
print(c,d) # 10 20
# end
print(a,b,end=",") # 10, (10+3j), 10
print(c,d)# format specifiers
age = 20
print("value of age is %d"%(x))
y = "Ankit"
print("Name is %s and age is %d" %(y,age) )- Before diving into the import let's discuss the module, package, and library
- Module: Our python file is a module.

- Package: Collection of module is known as Package.

- Library: Collection of package is known as library.
# Importing whole module
import operations
# importing only particular things from a module
from bioData import name, age
print(operations.sum)
print(operations.mul)
print("Your name is %s and age is %d "%(name,age))- we can also import data from another module using alias.
from operations import sum as add- Keywords are the predefine words in python.
- example: false,name, class, or, try, etc
- There is mainly two type converions
- Implicit conversion: Automatic conversion.
a = 10 #int
b = 10.5 #float
print(a + b) # float --> 20.5- Explicit conversion: Forcefully conversion.
num_str = "10"
num_int = int(num_str)
print(num_int) # Output: 10
num_float = float(num_str)
print(num_float) # Output: 10.0- Inputs can be taken using
input()function in python. input()can take at most one argument of str type.input()always return string type values.
x = (input("Enter value of X"))
y = int(input("Enter value of Y"))
z = float(input())
comp = complex(input())
print(type(x), type(y), type(z), type(comp)) #<class 'str'> <class 'int'> <class 'float'> <class 'complex'>
print(x) #Ankit Choubey- +,-, *, **,,//, /,%
- Division
/: this will always give result in float. - Floor division
//: result in always floor value and if any operand is float then ans will be in float.
print(17/2) # 8.5
print(17//2)# 8- <=, >=, !=, ==, etc
- and, or, not
- Logical operators should be wrtiten in lowercase
# Logical operator
print('logical operator')
print(not True)
print(not 0)- Bitwise mainly deals with the binary numbers
- &, |, ~, ^ (xor), >>, <<
#Bitwise and
0&0 -> 0
0&1 -> 1
1&1 -> 1
1&0 ->0
#Bitwise or
0|0 ->0
0|1 ->1
1|1 ->1
1|0 ->1
# Bitwise xor: here if both values are oppsite then result will be 1
0^0 -> 0
0^1 ->1
1^1 ->0
1^0 ->1- =, +=, -=, *=,etc.
- Left side always consist a variable.
a *= 5
is,is not- Here,
ismanly compare object Id of variables. x is y
x = 34
y = 34
print(x is y) #truein,not in.- To use membership operator there is some condition.
- These operator are applicable only on container(iterables).
- They result True or False value.
# membership operator
list = [12,23,54,65]
print('is 23 is present?',23 in list) #true- Match statement is very similar to
switchcase in c, C++
x = int(input("Enter number: "))
match x:
case 1:
print("one")
case 2:
print("Two")
case 3:
print("Three")
case 4:
print("Four")- Note:
breakkeyword connot be used in match block.
-
The key function for working with files in Python is the open() function.
-
The
open()function takes two parameters; filename, and mode. -
There are four different methods (modes) for opening a file:
-
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
-
"a" - Append - Opens a file for appending, creates the file if it does not exist
-
"w" - Write - Opens a file for writing, creates the file if it does not exist
-
"x" - Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
-
"t" - Text - Default value. Text mode
-
"b" - Binary - Binary mode (e.g. images)
f = open("demofile.txt", "r")
print(f.read())- It will give you an error if the file is not found.
#create
f = open("myFile.txt", "x") # creating file
f.close()f = open("myFile.txt", "w") # write will overwrite the content of the file
f.write("This is something which is written using python file write funtion.")
f.close()# append
f = open("myFile.txt", "a") # appending into file
f.write(" This is new code written using the python")
f.close()- Python has two primitive loop commands:
- while loops
- for loops
- With the while loop we can execute a set of statements as long as a condition is true.
count = 0
while(count <= 5):
print(count, " Ankit") # 6 times Ankit
count+=1
# break statement
i = 0
while i < 6:
i += 1
if i == 3:
break
print(i) # 0 1 2
#continue statement
i = 0
while i < 6:
i += 1
if i == 3:
continue # 3 will be skiped from iteration
print(i) # 0 1 2 4 5- Here,
breakis a keyword which is used to stop the loop and transfer the control to outside the loop. continueis also a keyword used to skip that particular iteration.passkeyword is used to create empty block
if condition:
pass
#example 2
while(condition):- Note::
while loopis used when no. of iteration unknown.
- A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)- The
forloop does not require an indexing variable to set beforehand.
Note: for loop is mainly used when no. of iterations is known.
- We can also use
elsewithforandwhileloop.
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")Note: The else block will NOT be executed if the loop is stopped by a break statement.
An itreaalbe is something that contains a countable number of values.
Technically, In python an iterator is an object which implements the operator protocol which consists of mehtods __iter__() and __next__()
examples of iterables:
- range
- list
- tuple
- dict, etc.
- it is a class.
- Immutable sequence.
- contains only
inttype values. - it contains a sequance with comman difference.
- These are indexed elements.
# range(start,end,step)
r1 = range(1, 30, 2) #here third parameter is optional and by default it is 1
for x in r1:
print(x)For more notes please refer to w3Schools.com




