You are on page 1of 14

PYTHON

Python is a general purpose high level programming lenguage

Guido van Rossam is the founder of Python.

python was launched in Feb 20th 1991.

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

first program :-

syntax to print hello world

Print("hello World")

output

hello World

there is no semi colon in python

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

C, Java are statically typed programming language

python is dynamically typed proramming language

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

Input

x=5

print(x)

output :-

In python varibale is not assigned type it automatically understand what type it is when we declare
value to it.

To check what type of variable is x follow this

Input

type(x)

Output

<class 'int'>

---------------
Python borrowed many feature from different language

for eg;-

1. Functionall programming from C

2. OOP from c++

3. Scripting langugage feature from perl and shell script

4. Modular Prgramming feature from Modula-3

most of the syntax is borrowed from C and ABC language.

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

Where do we use Python

1. Desktop Application
2. Web application - Djongo
3. Database application
4. For Networking application
5. Games
6. Data Analysis
7. Machine Learning
8. AI
9. for IOT application

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

Which company uses Python?

 Google
 Youtube
 Dropbox
 NASA

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

Feature of Python:

1. Simple ans easy to learn


2. FreeWare (we can download and use it for free) and Open Source(we can see source code
of Python)
3. High level Programming language ( high level language mean that it is human readable
language)
4. Plaform Independent (it can run on any computer like in windows mac linux)
5. Portablilty (moving on different machine is easy without changing anything)
6. Dynamically Typed (we are not requied to give type of the variable it understand what type
of variable it is when we assign value to it.
7. Object Oriented Progaming language also Procedure Oriented Programing
Language(BOTH)
8. Interpreted (no need to compile.)
9. Extensible (we can use c or java program in Python)
10. Embedded(we can use Python Program
11. Extensive (we can use Python Library)
----------------------------------------------------------------------------------------------endL2

Flavour of Pthyon :

1. CPython
2. Jython or JPython
3. IronPython
4. Pypy
5. RubyPython
6. AnacondaPython(to handle big data)

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

what is Identifier?

name of variable or class name or method name

Rules while naming identifiers

1. a to z, A to Z , 0-9, _ only allowed symbols


2. should not starts with digits
3. identifier are case sensitive
4. Reserves words can't be used as identifiers
5. No lenght limit of identifiers but use less

_x  private(starts with single underscore)


__x  Strongly Private(starts with double underscore)
__name__  it is reserved by python

Python has only 33 reserved words

True,False,None
and,or,not,is
if,else,elif
while,for,break,continue,return,in,yield
try,except,finally,raise,assert
import,from,as,class,def,pass,global,lambda,del,with

Data Types:-

1. int
2. float
3. complex
4. bool
5. str
6. bytes
7. bytearray
8. range
9. list
10. tuple
11. set
12. frozenset
13. dict
14. None
Note- In Python everything is an object , int string ,,,,everything is an object .even if do not write
in class then also it is an object

Input-
a = 10
id(a)
output
289389 (address of memeory)

1. Int
int is the intergral value like 10,20,1000

note- in pthon 2 long is available but in Python3 long data type is not available

No. of ways to represent int data type


1.Decimal Form
2.Binary Form
3.Octal Form
4.Hexa Form

a.Decimal form (0 to 9)
a = 7878

b.Binary (0 and 1)base 2


a = 1111 (15 in binary)
Python does not consider it as 15 if you print a it will give you result as 1111. so to make it
a binary no you have to give special form that is

a = 0b1111 or a=0B1111
print(a) # 15

c. Octal (0to7) base 8


same like binary
a = 0o777
print(a)

d. Hexa decimal (0 to 9 and a to f or A to F here case does not matter)base16

a=0x or 0X in begning of any hexa decimal no is required

----------------------------------------------------------------------------------------------end4
Base conversion :-

 bin()
 oct()
 hex()

we can convert any form to any form using this

for ex

bin(15) '0b1111'

bin(0o7777)  '0b111111111'
^
no which is to
be converted

note - conversion or different form is allowed only in int not in others like float
---------------------------------------------------------------------------------------------
2.Float data type
Number with have decimal value

ex
g=9.8
type(g) # float

Exponential Form
y=10e2 (10* 10^2== 10*100)
#se can store long no by such
type(y) #float

3. Complex data type:


a+bj
a  real part
b  imaginary Part
j = square root of -1

note real part can be in any form like in decimal or binary etc
but imaginary part must be in decimal form

a = 10+9j
print(a) ----- to print the value of a
type(a) ------to print the type of a
a.real ---------to get the real part of complex no
a.imaginary --to get the imaginary part of complex no

4.Bool
True and False
True + True = 2
True + False = 1
5. str

s = "manshu" or s= 'manshu' (single or double quote both works)


type(s)  str

s = "manshu
shivam" --> double quote does not work with multi lines.so use triple quote

s ='''manshu
shivam'''

s = "manshu shivam is learning "python" " ------------ > error


s = 'manshu shivam is learning "python" ' --------------> perfect(use double or triple quote)

slice operator
s = shivam
if i want third to fifth letter then i need to do sliceing

indexing from front side


0 1 2 3 4 5
S H I V A M

-6 -5 -4 -3 -2 -1  indexing form backside

from front side


input
s[3:5] (note last index is not included)
output
va

input
s[3:] (when last digit is not given it takes till last)
output
va.

input
s[-1]
output
m

s = manshushivam

input
s[1:6:3] (it means s[begin:end:step)
output
asu
input
s = manshu
s*4 (it multiplies string to n no of times)
len(s) (it gives you length of string)
output
manshumanshumanshumanshumanshu
6

Typecasting
- to int
string should be whole no then only it can convert to int
input
int("10")
output
10

input
int("10.10")
output
error

input
int("0B1111")
output
error (only whole no in decimal form with base 10)

-to float
float(10) --- 10.0
float(10+20j) - error
float("10") -- 10.0
float("10.5")-- 10.5

-to complex
complex(x)  x+0j (if you pass one variable it will make real part)
complex(x,y) x+yj (if you pass two variable it will be real and imaginary part)
complex(True,False) output => 1+0j
complex("1","2") output error
-to bool
bool(0.0) output False
bool(1) output True
bool(10+20j) output True (only false if both part of complex is 0)
bool("durga") output True (false only id empty string "" . )
-to str
str(10+20j) output 10+20j
Immutable Vs Fundamental Data types

Immutable --> which can not be change.


All fundamental data types are immutable
when object is created by user python doesn't directly create a new object it first check if the
object if already made or not if it is available then it point the new varible to the same object
proof :-
v1 = "Dumraon"
v2 = "Dumraon"
v3 = "Dumraon"
v3 = "Dumraon"

id(v1) output 49706244 (address of the object)


id(v2) output 49706244
id(v3) output 49706244
id(v4) output 49706244

since we can see same address this means that every variable is pointing to same object and not
creating new one .
This feature helps for memory utilization and perfomace is faster

x= 10
y =10
x is y (this syntax means that is x and y are pointing to same object or now , is ooperater check
references)

x is y output True
y is x output True

now

x = 256
y = 256

x is y output False

how it became false ?!


note - remember reuablitiy of same object is possible only for 0 to 256 in int data type

now

x = 10.0
y = 10.0

x is y output False

why false again?


Remember for float and complex reusabilty is not there. it create a new object everytime.

---------------------------------------------------------------------------------------------------------------
6. bytes
in range of (0 to256)

x=[10,20,30,40]
b= bytes(x)
type(b) output bytes

bytes data type is immutable its value can not be changed


x[1] = 60 #error coz i said na i can not be changed

7. byte array

x=[10,20,30,40]
b = bytearray(x)
type (b) output bytearray

bytearray also have range of (0 to 256)


only difference between byte and bytearray is byte is immutable but bytesarray is mutable

8. list
list is used to store multiple values in a single variable
eg
thisList = ['apple' , 'banana' , 'cherry']
print(thisList) output ['apple' , 'banana' , 'cherry']

list is -
 Ordered
 Changable(mutable)
 Allow duplicate
 items are indexed
 can contain different data types

 Access Item
thisList[1] = 'blackberry'
print(thisList) output ['apple' , 'blackberry' , 'cherry']

 Slicing

 Check if item is present of not


 change the item
 Insert item
 extent item
 add another list
 Remove specific item
 loop through a list
 list comprehension
 sort list
 copy a list
9.Tuple
tuple is used to store multiple values in a single variable
eg
thisTuple = ('apple' , 'banana' , 'cherry')
print(thisTuple) output ('apple' , 'banana' , 'cherry')

Tuple is -
 Ordered
 Unchangable (immutable)
 Allow duplicate
 items are indexed
 can contain different data types

10. range()
range data type represent a sequence of values
immutable -> we can not change the value of range

r = range(10) (note -10 is not included)


type(r) output range
print(r) r(0,10)
for i in r : print(i) output - it will print all value form 1 - 9

r = range(10,30) (this method takes values from 10 to 29)


r = range(10,50,5) (this will print 5 step ahed from previous
one )

range(e) -- begning is 0 ending is e-1


range(s,e) -- begning is s ending is e-1
range(s,e,s) -- begning is s ending is e-1 and then last s is step it increment values by s

11. set
set is used to store multiple values in a single variable

set is -
 Unrdered
 changable (mutable)
 does not Allow duplicate
 items are not indexed
 slicing is not available
 we can add or remove certain values
 can contain different data types
12.frozenset
set is used to store multiple values in a single variable

set is -
 Unrdered
 Unchangable (immutable)
 does not Allow duplicate
 items are not indexed
 slicing is not available
 we can add or remove certain values
13. dict:
dictionary are used to store data values in key : value pair
dict is -
 ordered
 changable (mutable)
 does not Allow duplicate
 items are indexed
 we can add or remove certain values
Operators:-
o Arthimetic Operation
o Relation Operators or Comparison Operator
o Logical Operator
o Bitwise Operator
o Assignment Operator
o Special Operator
Arthmetic Operators

+ ,-,*,/,%
**  Exponential
//  Floor division
eg
x=5
y=2
print(x**y) output 25 #this means x^y == 5^2
y = -2
print(x**y) output 0.04
eg
x =15
y =2
print(x//y) output 7

# the floor division rounds down to the nearest whole no

print (x/y) output 7.5

# the division operator gives floor value not int

+ operator applicable for str type also as String concatation. but remember both operator
should be string . if one is string and other is int then it wil give error

* operator applicable for str type also as multipling string by n times (n should be int not
string)

Relational Operators :-
> , >= , < , <=

10>20 False
'durga'<'ravi True
'roja' < 'ramya' False
True > False True
Equality Operator (== , !=)
10==20 False
10!=20 True
10==True False
10==20==30 False

= and ==

= single equal operator is assignment operator


== double eequal operator is comparision operator

Logical Operator

and if both arguments are True then only True


or if atleast one argument is True then True
not

Bitwise operator
applicable only for int and boolean

& | ^ ~ << >>

&  if both bits are 1 then only 1 otherwise 0


|  if atleast one bit is 1 then 1 otherwise 0
^  x-or  if both bits are different then 1 otherwise 0 (xor)
~  bitwise complement ooperator
10 and 01
<<  bitwise left shift
>>  bitwise right shift

eg
4&5 output 4 4 = 100 & 5 = 101
add 100
101
100

4| 5 output 5 4 = 100 & 5 = 101


add 100
101
101
4^5 output 1 4 = 100 & 5 = 101
add 100
101
001

Ternary Operator
?:

x (Conditon) ? firstvalue : secondValue


x(10<20) ? 30 :40
print(x) ??error this syntax is for Java
x =30 if 10<20 else 40
print(x) 30
a = int(input("enter First number"))
b=int(input ("Enter Second Number"))
c =int(input ("Ener Third Number'))

max = a if a >b and a>c else b if b>c else c


print("Max value:" , max)

consol window will ask you to enter 3 digit number after you provide it no it will print max
no.

a = int(input("Enter First number")


b = int(input("Enter Second Number")
print("Both number are equal" if a==b else "First Number is greater than second number" if
a>b else "First Number is smaller than second Number")

Identity Operators
a = 10
b = 10
print(a is b) True
print(a is not b) False

r1 is r2 here is operator is identitiy operator and it returns true if it points to the same
object

r1 is not r2 here is not operator is identitiy operator and it returns false if both points to
same object

is vs ==

is operator is used for object comparision


== operator is used for content comparision

Membership Operators
in
not in

s = "hello i am learning python"


print("hello" in s) Output True

Operator Precedence

( )  Paranthesis is , is not
**  Exponential operator in, not in
~,-  Unary Operator not
* , / , % , // and
+,- or
<<,>>
&
^
>, >=, <, <=, ==, !=
=, +=, -=, *=......
Module
module is a group of function
Libraries
it is group of module

to import module follow these syntax

syntax
import math math module have function of squar root, pi etc
print(mat.sqrt(16)) output 4.0
print(math.pi) output 3.141

or

import math as m we converted math word with m nowonwards we will


use m in place of math and not math.
or

form math import sqrt to import only squar root funtion of math module
print(sqrt(9)) 3.0

to take data from keyboard we use input funtion


input()
by default it takes string value if you want int of bool you need to convert it that is you need to do
typecasting.
eg
int x = int(input("enter no '))

write a program to read two number

You might also like