1.Python Fundamentals
Language fundamentals
Introduction
Applications of python
Features of python
Limitations of python
Flavors of python
Python versions
Identifiers
Reserved words
Data types(14 data types)
Type casting
Introduction
Python is a programming language is used to develop applications (gmail, web development), it is a high level(programmer friendly language rather than that of machine friendly language), General purpose(not specific to one type of application)
a =10
b=20
c=30 if a>b else 40
print(c)
Who developed python? Guido Van Rossum in 1989 National Research Institute in Netherlands and released in 1991 on Feb 20, java came in 1995 and released officially in 1996. Python is older than java.
Python Trend: market requirements changes from time to time.
1. Simple language & easy to understand
2. Very less code/ concise code
3.AI,ML,DL,NN
4.IOT
Benefits of python
easy to understand
short
There is no pre-requisite to learn python, no need to declare type explicitly(Dynamically typed programming languages python, javascript).
Same variable can be defined as different type of variable in dynamically typed programming language
Statically typed programming languages doesn't allow to use undeclared variables(java, c, c++).
Python came from the comedy show "THE COMPLETE MONTY PYTHON'S FLYING CIRCUS" broadcasted in BBC from 1969 to 1974.
C- functional programming
c++, java- OOPS
perl,shell script-scripting language
Python borrowed features of functional programming language from C, borrowed OOPs concept from c++, scripting language features from perl, shell script, also features of modular programming from modula-3.
Most of the syntax in python adopted from C & ABC
Where to use python?
Desktop applications: running in single system (e.g. calculator)
Web applications: gmail, fb (Django, flask, pyramids)
Network applications
Games development
Data analysis
ML,DL,NN,AI, IOT applications
Features of Python
1. Simple and easy to learn
python has 33 reserved words, where as java has 53 reserved words
Python code is just like reading English language
concise code: complex code can be written in small code, development time can be reduced and project cost gets reduced(readability gets reduced).
#print the contents of the file and show in the console
python code
print(open('abc.txt').read())
2. Freeware and open source
To use python, there is no need to pay the license.
Python is being maintained by Python Software Foundation(python.org)
Java owned by Oracle
C#, .net owned by Microsoft
Opensource: source code is open to everyone, for fulfilling of ones requirement customization can be done.
Different flavors of python
Jython-java
Iron python- C#,.net
Anaconda python- Big data requirements
3.High level programming language
Anyone can understand the code of python, being programmer you don't need to worry about the memory allocation and object destruction and the security of the code which are considered to be the low level activities of a programmer.
Internally all the low level activities will be handled and taken care by python virtual machine(PVM)
4. Platform independent (Write once and run anywhere)
The applications of other programming languages like C, Java are meant to be platform dependent which means that each operating system should have its own application for the app to run. But this is not required for python, it is just simply write once and run anywhere code or the app. However you need to have the PVM(Python Virtual Machines) for the corresponding OS that application supposed to run on. PVM is responsible for converting the python application code from one OS to the other.
Python application- platform independent
PVM- platform specific
5.Portability
Python application can be migrated from one platform to another very easily with zero or minimum changes in your code.
6. Dynamically typed
In other languages like Java, you have to declare the variable every time in order to run the program.
Each every variable type has to be declared explicitly. But in python there is no need to declare the type of the variable, by default python can identify the variable type, this is done because python is a dynamically typed programming language.
This has one more advantage of assigning multiple data types to the same variable which is more flexible to the programmer.
7. Both procedure and object oriented
C is procedure oriented language, hence missing the benefits of OOPs
Java is an object oriented language, missing the benefits of procedure oriented programming language.
Python uses all the programming paradigms which includes modular and scripting language.
8.Interpreted
We don't need to compile the python program, we just need to run.
Interpreter compiles the code and PVM will not start its execution until interpreter clears the code.
9.Extensible
a. We can use legacy non-python code in python application, project cost gets reduced.
b. Performance of the application can be increased by writing the specific task in other languages like C
10.Embedded
Python code can be implemented in other programming languages like Java, C#, .Net. This enables to increase the scope of the application.
11.Extensive library
No other language have as many as the libraries that are available for the python.
# write a program to generate 6-digit OTP
random library randint
from random import randint
print(randint(0,9),randint(0,9),randint(0,9),randint(0,9),randint(0,9),randint(0,9), sep='')
Limitations of python
1. Not suitable for developing mobile applications (because the library support is not available)
2. End to end enterprise applications (banking, telecom applications), python is not suitable because of library support.
3.Python is interpreted programming language (line by line execution) hence performance is low. In order to increase performance (JIT compiler+PVM) comes into play where the series of lines of code will be interpreted at a time. This is pypy version for fast python, however performance is still not up to the mark.
Flavors of python:
As python is open source, anyone can customize python by editing source code to fulfill their requirements which results in the different flavors of python. Various different flavors of python are
1.CPython Standard flavor developed by PVF
2.JPython/Jython to work with java applications, can be run by JVM
3.Iron Python to work with C#, .net
4.Ruby Python to work with ruby language
5.Anaconda python to work with data science applications and large volumes of data.
6.Stackless (Python for concurrency)
7.Pypy (python for speed)
Python Versions
Python 0.9.0 on Feb 20,1991
Python 0.9.1 in Feb,1991
Python 0.9.2 in Autumn,1991
Python 0.9.4 on Dec 24,1991
Python 0.9.5 on Jan 2,1992
Python 0.9.6 on Apr 6,1992
Python 0.9.8 on Jan 9,1993
Python 0.9.9 on Jul 29,1993
Python 1.0 in January 1994
Python 1.2 on Apr 10, 1995
Python 1.3 on Oct 12, 1995
Python 1.4 on Oct 25, 1996
Python 1.5 on Dec 31, 1997
Python 1.6 on Sep 5, 2000
Python 2.0 on Oct 16,2000 py2k
Python 2.1 on Apr 15,2001
Python 2.2 on Dec 21,2001
Python 2.3 on Jul 29,2003
Python 2.4 on Nov 30,2004
Python 2.5 on Sep 19,2006
Python 2.6 on Oct 1,2008
Python 2.7 on Jul 1,2010
Python 3.0 on Dec 3, 2008 py3k
Python 3.1 on Jun 27, 2009
Python 3.2 on Feb 20, 2011
Python 3.3 on Sep 29, 2012
Python 3.4 on Mar 16,2014
Python 3.5 on Sep 13, 2015
Python 3.6 on Dec 23, 2016
Python 3.7 on Jun 27, 2018
Python 2.X vs Python 3.X
Backward compatibility of versions is not required in python as compared to other languages like Java.
One of the example is print function, in python 2.X print function doesn't need parenthesis, but in python 3.X print function should be defined in parenthesis.
Python is developed as whole new version, this gives us an advantage of adding new features as per new market requirements without worrying about its compatibility with older versions.
In 2020, python stopped providing services to python 2.X
Python Identifiers
A name in python program is called an identifier which can be variable, class and method.
a =10 here a is an identifier which is used for identification purpose.
Rules to create identifier:
1. Alphabet characters, numbers, underscore is allowed.
a-z, A-Z,0-9, _ are allowed
E.g. cash=10, ca$h=20, all@hands=30 gives syntax error
2.Identifier should not start with digit.
total123=10
123total=20 gives syntax error
3.python is case sensitive, and hence python identifiers are case sensitive too.
total =10
Total=20
TOTAL=30
4.No length limit for python identifier, however it is recommended to use short length for the identifier for best programming practices.
5.Reserved words/ Keywords can't be used as python identifier.
x is a normal variable
_x protected variable
__x private variable
__x__ magic variable (language specific predefined identifiers)
Python Reserved words/ Key words
Keywords are used to define specific meaning or functionality. Python has 33 keywords/ reserved words.
the keywords can be seen by using the following code
import keyword
keyword.kwlist
the above code prints all the 33 keywords in the form of a list.
- contains only alphabets
- except True, False and None, all contains only lower case alphabet symbols
- switch, do-while are not present in python
- python is dynamically typed programming and hence there is no need of the reserved words for the data type in python like int, float, bool as keywords.
True, False, None
if, elif, else
while, for, break, continue, return, in, yield
try, except, finally, raise, assert
import, from, as, class, def, pass
global, nonlocal, lambda, del, with
Datatypes in python
Type of the data stored inside a variable is known as datatype. In python don't need to declare type of the variable explicitly. (javascript is also dynamically typed programming language like python).
int
float
complex
bool
str
list
tuple
set
frozen set
dict
bytes
bytearray
range
None
Everything in python is object.
a =10 #variable declaration
type(a) #type of the variable
id(a) #address of the variable
print(a)
Datatype int:
The numbers without decimal values can be known as integer numbers, to represent long integral numbers long int is available(but this is only available in python 2.X).
To represent integral values there are four ways
1.decimal form (base-10)
allowed digits are 0 to 9, this is by default in python.
2.binary form (base-2)
allowed digits are 0 and 1
a=1111
print(a) #this will be considered as decimal number as default type
a=0B(1111)#to declare explicitly the binary number by 0B or 0b
print(a)
3.octal form (base-8)
allowed digits are 0 to 7
this can be represented with 0o or 0O
a=0o(123)
print(a)
4.hexa-decimal form (base-16)
allowed digits are (0-9, A to F), this is represented by 0x/0X
A-10
B-11
C-12
D-13
E-14
F-15
a=0x(10)
print(a)
a=0xFace
print(a) #64206
Base conversion functions
Python provides three inbuilt functions to convert the integers from one base to others.
1.bin() is used to convert other base forms into binary base
bin(15)
#'0b1111'
bin(0o123)
bin(0xFace)
2.oct() is used to convert other base forms into octal base
oct(100)
#'0o144'
oct(0b111101)
#'0o75'
oct(0xFace)
#'0o175316'
3.hex() is used to convert other base forms into hexa-decimal base
hex(1000)
#'0x3e8'
hex(0b10111111)
#'0xbf'
hex(0o123456)
#'0xa72e'
Datatype Float
Floating values can only be represented by decimal values, no other bases are available for this data type. If you try to give other bases for the floating type, you will get syntax error.
Floating can be represented with scientific notation or exponential form, this will be helpful especially in representing larger values with in lesser space.
f = 1.2e3
#1200.0
Datatype complex
the syntax of complex numbers is x = a+bj
a = real part
b= imaginary part
j =sqrt(-1)
In python j/J should used to represent the imaginary part not any other alphabet
x = 10+20j
print(x.real)
print(x.imag)
Real and imaginary can be int or float.
x = 10.5+ 20j
x = 10.5+ 20.6j
However the point to be noted is that for real part you can take any base forms like decimal, octal and hexa-decimal but for the imaginary part you should only take the decimal number.
x = 0B1111+20j works fine
x=15+0B1111j will get the syntax error.
We can perform all the arithmetic operations on two complex numbers like addition, subtraction, multiplication and division. These kind of operations used in scientific calculations, electrical applications.
Datatype Bool
To represent the logical values this data type is used, the only allowed values are True, False (case sensitive) for boolean datatype.
true, false are not allowed, hence throws the Name error.
a =10
b=20
c = a>b
print(c) #False
print(type(c))
Internally the values of bool data type defined as follows
True-1, False-0
print(True+True) #2
print(True-False) #1
print(True*False) #0
Datatype String
str datatype representations by using single, double and triple quotes
Any sequence of characters within single or double quotes is known as string.
s = 'asdgjab'
print(type(s)) #str
In python there is no char datatype, even single character in quotes is considered as a string.
Triple Quotes applications:
- To define multiline string literals (single and double quotes only support single line string literals)
- if you want to use single quote as normal character in a string, then enclose the string in double quotes.eg: s="class by 'durga' is very good"
- if you want to use double quote as normal character in a string, then enclose the string in single quotes.eg: s='class by "durga" is very good.
Slice Operator:
If you need sub-strings in a particular sting then the slicing operator comes into play.
s ='abcdefghijklmnopqrstuvwxyz'
the syntax to use the slice operator is s[begin:end] returns sub-string from begin index to end-1 index.
print(s[3:9]) # returns string from 3 to 8 index
If end index is not defined the default index is end of string index
If start index is not defined the default index is 0.
s[:9]
'abcdefghi'
s[:]
'abcdefghijklmnopqrstuvwxyz'
s[3:1000]#slice operator never gives the index out of bound error
'defghijklmnopqrstuvwxyz'
s[5:1]#always moves in forward direction, empty string will be returned
''
Applications of Slice operator
s="durga"
#need output as Durga
print(s[0].upper()+s[1:])
#need output durgA
print(s[:len(s)-1]+s[-1].upper())
#need output DurgA
print(s[0].upper()+s[1:len(s)-1]+s[-1].upper())
For string concatenation using "+" in python, both variables should be string type, otherwise you will get the type error.
* operator acts as string repetition operator.
s = 'durga'
s = 'durga'*3
#returns ''durgadurgadurga''
s = 3*'durga'
#returns ''durgadurgadurga''
You can't operate the * with string, it should be int
Important conclusions
1. The following data types are considered as the fundamental data types int, float, complex, bool,str
2.Long data type available in python 2 but not in python 3.
3.There is no char datatype in python, char values also represented by using str datatype.
Type casting
Process of converting the data from one type to another is called type casting or type coersion.
Five inbuilt functions for the process of type casting are
- int() to convert from other types to int
float to int: int(10.989) returns value as 10
complex to int: can't be done, Type error will be thrown
bool to int: int(True) returns 1 , int(False) returns 0
str to int: this will be done only when string internally contains integral values that is defined in the base 10.
int('15') returns 15
int('0B111') return error
int('10.5') returns error
- float() to convert other types to float
int to float: float(15) returns 15.0, float('0B1111') returns 15.0, float(0XFace) returns 64208.0
complex to float is not possible, returns the type error.
bool to float: float(True) returns 1.0 , float(False) returns 0.0
str to float: Internally string should contain either int value or float value only in base 10
float('10') returns 10.0
float('20.6') returns 20.6
float('0XFace') returns value error
float('durga') returns error
- complex() to convert other types to complex
Form1 with one argument:
complex(10) returns 10+0j
complex(0B1111) returns 15+0j
complex(10.5) returns 10.5+0j
complex(True) returns 1+0j
complex(False) returns 0+0j
complex('10') returns 10+0j
complex('10.5') returns 10.5+0j
complex("0b1111") returns malformed string error
Form2 with two argument:
complex(10,20) returns 10+20j
complex(10.5,20.6) returns 10.5+20.6j
complex('10','20') returns type error, there is an exception that second argument should not be string if first argument is string
complex(10,'20') returns 10+20j
- bool() converts other type of elements to bool
If argument is 0 it is treated as True otherwise it is False.
int argument
bool(10) returns True
bool( 0) returns False
bool(-10) returns True
float argument: rule remains same
bool(0.0) returns False
bool(0.1) returns True
bool(-0.000001) returns True
complex argument:
bool(0+0j) returns False
bool(1+0j) returns False
str argumment: only empty string returns False
bool('True') returns True
bool('False') returns True
bool('yes') returns True
bool('no') returns True
bool('') returns False
str() converts other data types to string
str('10') returns '10'
str('0B1111') returns '15'
str(10.5) returns '10.5'
str(10+20j) returns '10+20j'
str('True') returns 'True'
str('False') returns 'False'
Fundamental Data types vs Immutability:
Mutable means changeable, Immutable means non-changeable.
All fundamental datatypes are immutable.
Once we create an object we can't perform any changes in that object.
If we try to perform any changes, with new changes another object will be created.
This non-changeable behavior is called immutable.
e.g:
x = 10
print(id(x))
x = x+1
print(id(x))
Elements with no reference will be collected by garbage collector.
x=10
y =x
print(id(x))
print(id(y))
y =y+1
print(x)
print(y)
print(id(x))
print(id(y))
Memory Utilization
Performance enhancement
a = 1000.234
b = 1000.234
print(a is b)
a = True
b = True
print(a is b)
a = 'durgasoft'
b = 'durgasoft'
print(a is b)
Immutability vs Mutable:
Object reusability is not available for complex data types.
a = 10+20j
b =10+20j
print(a is b) #returns False
You can use IDLE(python console) are REPL (Read, Evolve, Print, Loop) tools used for small testing purposes, hence they are not standard and may give wrong results sometimes. We can use standard versions such as ATOM, Pycharm.
Mutability:
l =[10,20,30]
print(id(l))
l[0]=7777
print(l)
print(id(l))
The addresses will be same for the above code which proves mutability.
l1 = [10,20,30,40]
l2=l1
print(l1)
print(l2)
l1[0]=7777
print(l1)
print(l2)
l2[1]=8888
print(l1)
print(l2)
As l1,l2 are referencing same object, the changes made in either of the list reflects in other.
This is called Mutability.
Python collection related Data types
List:
1.insertion order is important(order is preserved)
2.duplicates are allowed
3.should be represented in []
4.heterogeneous objects are allowed
5. indexing and slicing applicable
6.growable in nature
7.list is mutable
l =[10,'durga',20,10,30] #duplicates are allowed
print(l) #will display the elements in the same order which is given
print(type(l)) #datatype
print(l[0]) #indexing
print(l[-1]) #indexing
print(l[1:4]) #slicing
#create an empty array
l =[]
#add element
l.append(10)
l.append(20)
l.append(30)
l.append(40)
print(l)
#remove particular element
l.remove(30)
Tuple
Exactly same as list except that it is immutable.
Read only version of list is tuple.
It is represented by ()
t = (10,20,30,10,'durga')
#datatype
print(type(t))
#indexing
print(t[0])
print(t[-1])
#slicing
print(t[1:4])
#immutable
t[0]=7777 returns type error
t.append(50) returns attribute error
t.remove(10) returns attribute error
t =()
print(type(t))
t =(10)
print(type(t))#int, it is not tuple
#specifying tuple explicitly for single valued tuple
t =(10,)
Differences between list and tuple?
List is mutable, tuple is immutable
List elements is represented by [], tuples is represented by ()
List requires more memory, tuple requires less memory
List performance is less, tuple performance is more
List example: reviews on twitter, tuple example: savings, current accounts in bank, wending machines with fixed deposits.
Set
Duplicates are not allowed
Order is not important
Index is not applicable, slicing is not allowed
Heterogeneous objects are allowed
Set is growable and mutable
e.g: messages to all the subscribers through mail(duplicates not allowed, order not important)
s1 ={1,3,2}
s2={1,2,3}
s = {10,20,10,'durga',30,40}
print(s) #{10,20,'durga',30,40} order may be varied
print(s[0]) #type error
print(s[1:4]) #type error: set object is not sub scriptable
s ={10,20,30,40}
s.add(50)
print(s)
s.remove(30)
print(s)
append vs add:
Append will add the values at the end to the list, in the set there is no importance for the order of the elements, hence add will be used to add the elements for set.
s ={} is not an empty set, by default it is an empty dictionary
Special privilege's available for dictionary but not for set as the dictionary is most frequently used data type.
How to create empty set?
s = set()
print(type(s))
Difference between list and set?
In list order is preserved, in set order is not preserved
In list duplicates are allowed, in set duplicates are not allowed.
List represented by [], set is represented by {}
Frozen Set
Immutable set is known as Frozen set.
Set is mutable
s ={10,20,30,40}
s.add(50)
s.remove(30)
print(s)
Set is mutable
s ={10,20,30,40}
fs=frozenset(s)
print(type(fs))
fs.add(50) # attribute error thrown
fs.remove(30) # attribute error thrown
print(fs)
Difference between tuple and frozen set?
Order is preserved in tuple, order is not preserved in frozen set.
Duplicates are allowed in tuple, duplicates are not allowed in frozen set.
Indexing and slicing applicable in tuple, indexing and slicing are not available in froze set.
Dict
If you want to represent group of objects in the form of key value pairs then we should use the data type called dictionary.(Till now list, tuple, set, frozen set are used to represent the list of single elements with different types).
E.g.
rollno- name
mobileno. - address
dictionary syntax:
d ={k1:v1,k2:v2,k3:v3,....}
d ={100:'durga', 200:'sunny', 300:'chinni'}
How to add the items in dictionary?
d[key] = value
d = {}
d[100]='durga'
d[200]='ravi'
In dictionary, order is not preserved order will be based on the hash code of elements.
In dictionary duplicate keys are not allowed, duplicate values are allowed.
In dictionary heterogeneous objects are allowed in keys and values.
Dictionaries are mutable.
Indexing and slicing are not allowed.
Range
This will be used when we need to print the numbers in the sequence of numbers.
r = range(10) # 0 to 9
print(type(r))
print(r) #range(0,10)
for i in r:
print(i)
How to create range object?
Form1 : One argument
r =range(n)
from 0 to n-1
r = range(10)
r = range(100)
Form2 : Two arguments
r =range(begin,end)
from begin to end-1
r = range(1,10)#1 to 9
for x in r:
print(x)
r = range(1,11) #1 to 10
Form3 : Three arguments
r =range(begin, end, increment /decrement value)
from begin to end-1 with an an increment or decrement value
r = range(1,20,1)#1 to 19
for x in r:
print(x)
r = range(1,21,2) #1,3,5,7,9,11,13.....19
r = range(1,21,3) #1,4,7,10,13,16,19
r = range(1,21,4)
r = range(20,1,-5)
Order is preserved, hence indexing and slicing are applicable.
r = range(10,21)
print(r[0])
print(r[-1])
r1 = r[1:5]
for x in r1:
print(x)
Range object is immutable(as the sequence can't be misplaced)
Bytes
If you want to represent group of bytes elements, then we should use this bytes data type.
l =[10,20,30,40]
b = bytes(l)
print(type(b))
for x in b:
print(x)
If we want to handle binary data type such as images, bytes data type is used.
Bytes values ranges from 0 to 255(error will be thrown if out of range value is given as input).
Bytes data type is immutable, bytes object can't be supporting item assignment .
Order is preserved, indexing and slicing is applicable.
Bytearray
l =[10,20,30,40]
b = bytearray(l)
print(type(b))
print(b[0])
print(b[-1])
Bytearray values ranges from 0 to 255(error will be thrown if out of range value is given as input).
Bytearray is mutable, can be updated.
Order is preserved, indexing and slicing is applicable.
None Data Type
To handle the cases where value is not available, None data type is used.
def f1():
return 10
x=f1()
print(x)
def f1():
print('Hello')
x=f1()
print(x)
Internally in python None is also an object but PVM creates only one None object
a = None
print(type(a))
print(id(a))
a = None
b = None
c = None
def f1():
pass
d = f1()
print(a,b,c,d)
print(id(a),id(b),id(c),id(d))
Escape characters
\n new line
\t horizonal line
\r back space
\f form feed
\' single quote
\" double quote
\\ back slash
print("helloworld!")
print("hello\tworld!")
print("hello\nworld!")
print('This is \' symbol')
print('This is \" symbol')
print('This is \\ symbol')
Comments
# this is a single line comments
There are no such things called multiline comments in python
This is document string
'''
line1
line2
line3
'''
Constants
In python there is no concept of constants in python.
In program if we don't change the value of a variable represent that value in UPPER CASE.
Comments
Post a Comment