Python: Print ("Hello World")
Python: Print ("Hello World")
--------------
first program :-
Print("hello World")
output
hello World
---------------
-----------------
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.
Input
type(x)
Output
<class 'int'>
---------------
Python borrowed many feature from different language
for eg;-
---------------------------
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
--------------------------
Google
Youtube
Dropbox
NASA
------------------------------
Feature of Python:
Flavour of Pthyon :
1. CPython
2. Jython or JPython
3. IronPython
4. Pypy
5. RubyPython
6. AnacondaPython(to handle big data)
----------------------------------------------------------------
what is Identifier?
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
a.Decimal form (0 to 9)
a = 7878
a = 0b1111 or a=0B1111
print(a) # 15
----------------------------------------------------------------------------------------------end4
Base conversion :-
bin()
oct()
hex()
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
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
shivam" --> double quote does not work with multi lines.so use triple quote
s ='''manshu
shivam'''
slice operator
s = shivam
if i want third to fifth letter then i need to do sliceing
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
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
now
x = 10.0
y = 10.0
x is y output False
---------------------------------------------------------------------------------------------------------------
6. bytes
in range of (0 to256)
x=[10,20,30,40]
b= bytes(x)
type(b) output bytes
7. byte array
x=[10,20,30,40]
b = bytearray(x)
type (b) output bytearray
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
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
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
+ 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 ==
Logical Operator
Bitwise operator
applicable only for int and boolean
eg
4&5 output 4 4 = 100 & 5 = 101
add 100
101
100
Ternary Operator
?:
consol window will ask you to enter 3 digit number after you provide it no it will print max
no.
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 ==
Membership Operators
in
not in
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
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
form math import sqrt to import only squar root funtion of math module
print(sqrt(9)) 3.0