Introduction to Python programming
Python is Popular Programming language.
Features of Python Programming Language
1. Open Source - Free and publicly Available
2. Python is Dynamically typed Programming Language
3. Python is Interpreter Based programming language
4. High Level Programming Language
5. Object oriented Programming language
6. Rich Library support
7. Python have Exception handling Techniques
8. Platform Independent Programming language
9. Easy to Lern & Code
10. Python syntax allows programmers to write Fewer Lines of code
In [1]: print('Welcome to Innomatics')
Welcome to Innomatics
In [2]: print('Welcome to innomatics')
print('Greetings of the day !!')
Welcome to innomatics
Greetings of the day !!
In [3]: a = 23
b = 56
In [4]: print(a)
23
In [5]: print(b)
56
In [6]: print('The value of a is :',a)
The value of a is : 23
Keywords
In [7]: import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not',
'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
In [8]: print(len(keyword.kwlist))
35
Identifiers
Identifier is a name of a variable,class, function, module or an object.
In [9]: a = 23
In [10]: area = 78
Identifier Name Rules
1. Identifier Should only contain Alphanumerics and an Underscore ( _ )
2. Identifier should start with a character or an underscore( _ )
3. Keywords should not be used as identifiers
4. white spaces are not allowed in Identifiers
5. No special Characters are allowed except an underscore ( _ )
Variables
1. Variable is a Data Container which stores a value
2. Variable names may be a shorter names like a, b ,k ,l...etc or More descriptive Names like area, width, data...etc
In [11]: a = 20
In [12]: b = 34
In [13]: a
Out[13]: 20
In [14]: b
Out[14]: 34
In [15]: c = a+b
In [16]: c
Out[16]: 54
In [17]: print(a)
20
In [18]: print(a,b)
20 34
In [19]: print(a,b,c)
20 34 54
In [20]: a , b, c = 10,20,30 # assigning multiple values to multiple variables in single line
In [21]: a
Out[21]: 10
In [22]: b
Out[22]: 20
In [23]: c
Out[23]: 30
In [24]: a,b,c
Out[24]: (10, 20, 30)
In [25]: print(a,b,c)
10 20 30
In [26]: a = 20
In [27]: # comments starts with #
In [28]: #assigning same value to multiple variables
In [29]: a = b = c = 45
In [30]: a,b,c
Out[30]: (45, 45, 45)
In [32]: Width = 20
Height = 25
Rectangle_area = Width * Height
print('Area of Rectanglle is : ',Rectangle_area)
Area of Rectanglle is : 500
1. Write a program for addition of two variables a,b
In [33]: a = 10
b = 20
c = a+b
print(c)
30
In [34]: A = int(input('Enter A value: ')) #taking dynamic input for variable A
B = int(input('Enter B value: ')) #taking dynamic input for variable B
C = A+B # Addition of A,B
print('The addition Result is: ',C)
Enter A value: 45
Enter B value: 34
The addition Result is: 79
Input ( ) Method
In [38]: print(help(input))
Help on method raw_input in module ipykernel.kernelbase:
raw_input(prompt='') method of ipykernel.ipkernel.IPythonKernel instance
Forward raw_input to frontends
Raises
------
StdinNotImplementedError if active frontend doesn't support stdin.
None
Input( ) takes inputs dynamically from the user ( keyboard) By default it takes data as a string
In [41]: data = input('Enter your Data: ')
Enter your Data: 45
In [42]: data
Out[42]: '45'
In [43]: D = input()
Greetings of the Day!!
In [44]: D
Out[44]: 'Greetings of the Day!!'
Data Types
Numeric Data Types
1. Integer
2. Float
3. Complex
In [45]: #Integers
a = 23
b = 4567
c = -789
Type ( )
In [48]: # type( ) - It returns data type of a variable
In [50]: print('The data type of a variable a is : ',type(a))
print('The data type of a variable b is : ',type(b))
print('The data type of a variable c is : ',type(c))
The data type of a variable a is : <class 'int'>
The data type of a variable b is : <class 'int'>
The data type of a variable c is : <class 'int'>
In [51]: type(a)
Out[51]: int
In [52]: type(b)
Out[52]: int
In [53]: # float
a = 34.78
b = 45.900234545
c = -67.432
In [54]: print('The data type of a variable a is : ',type(a))
print('The data type of a variable b is : ',type(b))
print('The data type of a variable c is : ',type(c))
The data type of a variable a is : <class 'float'>
The data type of a variable b is : <class 'float'>
The data type of a variable c is : <class 'float'>
In [55]: #complex
a = 3+7j
b = -5-8j
c = -3j
print('The data type of a variable a is : ',type(a))
print('The data type of a variable b is : ',type(b))
print('The data type of a variable c is : ',type(c))
The data type of a variable a is : <class 'complex'>
The data type of a variable b is : <class 'complex'>
The data type of a variable c is : <class 'complex'>
Type Conversion / Type Casting - Data Type Conversion
1. Integer ----> Float
2. Integer ----> Str
In [57]: a = 10
print(type(a))
<class 'int'>
In [58]: b = float(a) # converting a into float datatype
print(type(b))
<class 'float'>
In [64]: #integer to string
In [62]: c = str(a)
In [63]: print(type(c))
<class 'str'>
1. Float ----> Int
2. Float ----> Str
In [59]: a = 45.89
print(type(a))
<class 'float'>
In [60]: b = int(a) #converting a into integer data type
print(type(b))
<class 'int'>
In [65]: b
Out[65]: 45
In [66]: #float to str
a
Out[66]: 45.89
In [67]: c = str(a)
print(type(c))
print(c)
<class 'str'>
45.89
In [68]: c
Out[68]: '45.89'
1. str ---> int
2. str ---> float
This is possible if at all if the string literal is equal to any numerical value of base10
In [69]: a = '167'
In [70]: type(a)
Out[70]: str
In [71]: b = int(a)
print(b,type(b))
167 <class 'int'>
In [72]: x = 'satya'
type(x)
Out[72]: str
In [73]: #str to float
print(a,type(a))
167 <class 'str'>
In [74]: b = float(a)
print(b,type(b))
167.0 <class 'float'>
String
String is sequence of characters which are enclosed with either single quotes ' ' or double quotes " "
In [75]: a = 'Innomatics research labs'
In [76]: type(a)
Out[76]: str
In [77]: b = "Welcome to Innomatics Research Labs"
In [78]: type(b)
Out[78]: str
Multiple line string
In [79]: a = '''Hello all , Greetings of the day!!!!
Welcome to Innomatics Research labs
Happy Learning!! Thank you'''
In [81]: a
Out[81]: 'Hello all , Greetings of the day!!!!\nWelcome to Innomatics Research labs\nHappy Learning!! Thank you'
Operators in Python
1. Arithmetic Operators
2. Assignment Operators
3. Comparision Operators/Relationship operators
4. Logical Operators
5. Membership Operator
6. Identity Operators
7. Bitwise Operators
Arithmetic Operators
1. Afddition --> +
2. Subtraction -->-
3. Multiplication --> *
4. Division --> /
5. modulus --> %
6. Floor Division --> //
7. Exponentation --> **
In [82]: #Addition
In [83]: a = 20
b = 10
In [84]: a+b
Out[84]: 30
In [85]: #subtraction
a-b
Out[85]: 10
In [86]: #multiplication
a*b
Out[86]: 200
In [87]: #division
a/b
Out[87]: 2.0
In [88]: #modulus - returns remainder of a division
a%b
Out[88]: 0
In [89]: a = 25
b = 8
a%b
Out[89]: 1
In [90]: #floor Division
a = 10
b = 3
a//b
Out[90]: 3
In [91]: a/b
Out[91]: 3.3333333333333335
In [93]: import numpy as np
In [94]: np.floor(a/b)
Out[94]: 3.0
In [95]: np.ceil(a/b)
Out[95]: 4.0
In [96]: np.abs(a/b)
Out[96]: 3.3333333333333335
In [97]: #exponentation
a = 2
b = 3
a**b
Out[97]: 8
In [98]: b**a
Out[98]: 9
Assignment Operators
Assignment operator ( = ) is used to assign values to the variable
In [99]: a = 20
In [100… a+= 10 # a = a+10
In [101… a
Out[101]: 30
In [102… a-=10 # a= a-10
a
Out[102]: 20
In [104… a/=2
a
Out[104]: 5.0
In [106… a//=2
a
Out[106]: 1.0
Comparision Operators
Comparision Operators are used to compare values and variables
1. Greater Than >
2. Less Than <
3. Greater Than or Equal to >=
4. Less than or equal to <=
5. Equal to ==
6. Not Equal to !=
In [107… #greater than
10 > 5
Out[107]: True
In [108… a = 34
b = 25
a >b
Out[108]: True
In [109… b>a
Out[109]: False
In [110… #less than
3 < 5
Out[110]: True
In [111… 5 < 3
Out[111]: False
In [112… a,b
Out[112]: (34, 25)
In [113… b < a
Out[113]: True
In [114… #greater than or equl to
In [115… a = 23
b = 23
In [116… a >= b
Out[116]: True
In [117… a = 56
b = 23
a >= b
Out[117]: True
In [118… #less than or equal to
a = 45
b = 45
a <= b
Out[118]: True
In [120… a = 53
b = 26
a <= b
Out[120]: False
In [121… #equal to - Checking for equality
In [122… a = 23
b = 56
a == b
Out[122]: False
In [123… a = 10
b = 10
a == b
Out[123]: True
In [124… # not equal to
a = 45
b = 56
a != b
Out[124]: True
In [125… a = 10
b = 10
a != b
Out[125]: False
In [ ]: