Workshop Notes-1 Introduction to Python
Workshop Notes-1 Introduction to Python
1 Introduction
• Python is a high-level, interactive and object-oriented scripting language.
• Python is designed to be highly readable.
• It uses English keywords frequently whereas the other languages use punctuations.
• It is simple and easy to use. It has fewer syntactical constructions than other languages.
• Perform data analysis, Machine learning, image processing, develop software, website
Where we can run python? Google colab, Jupyter Notebook
ipynb: interactive python note book
Shift+enter: to run code
comment: #
Data Types:
1. Primitive: single data like integer, float, string, boolean, complex
2. Core: List, Tuple, Dictionary, Set, Array, Data Frame
1.1 Primitive
1.1.1 Integer
20
<class 'int'>
<class 'float'>
1
1.1.2 Float
[2]: a2=4.6 # Consider as a float
print(type(a2))
int(a2)
<class 'float'>
[2]: 4
10.0
1.1.3 String
<class 'str'>
1.1.4 Boolean
[5]: a4=False
print(a4)
print(type(a4))
False
<class 'bool'>
[6]: a5=10+3j
print(type(a5))
<class 'complex'>
[7]: abs(-3+4j)
[7]: 5.0
[8]: 3.457
2
[9]: x=14
y=5
z=2
Addition
[10]: print(x+y)
19
Subtraction
[11]: print(x-y)
9
Multiplication
[12]: print(x*y)
70
Division
[13]: print(x/y)
2.8
Modulus Operation
[14]: x%y
[14]: 4
Floor division
[15]: x//y
[15]: 2
Exponentiation
[16]: x**z
[16]: 196
[17]: 2**(0.5)
[17]: 1.4142135623730951
3
3. not: Reverse the result, returns False if the result is true, Example: not(x < 5 and x < 10)
1.2 Core
1.2.1 List
• Group of heterogeneous data, i.e., mix of different data types
• It is identified by [a, b, c]
• It is mutable, i.e., can be modified.
[18]: x1 = [10,20,30,40,50,60]
print(x1)
print(type(x1)) # To find the class/type
print(len(x1)) # print length of x1
10
[20, 30, 40]
[30, 40]
To replace elements by specific values
[20]: x1[1]=10
print(x1)
[21]: x1[1:4]=[0,0,0]
print(x1)
1.2.2 Tuple
• It is similar to list but identified by (a,b,c).
• It is immutable, i.e., can not be modified.
[22]: x2=(10,20.0,30,40,50,"a")
print(x2)
4
1.2.3 Dictionary
• It stores the data in the form of key-value pairs where key is unique.
• {“Key 1”:“Value 1”, “Key 2”:“Value 2”, “Key 3”:“Value 3”}
• It is mutable.
[23]: x3={"Key 1":"Value 1", "Key 2":"Value 2", "Key 3":"Value 3"}
print(x3)
{'Key 1': 'Value 1', 'Key 2': 'Value 2', 'Key 3': 'Value 3'}
1.2.4 Array
• Group of homogeneous data.
• It is identified by [ 1 2 3 4]
• It is multidimensional.
• It is mutable.
• There is no in-built function to deal with array.
• Here we use numpy to create numpy.
[ 2 5 62 5 42 52 48 5]
<class 'numpy.ndarray'>
1.2.5 Sets
• It is unordered collection of unique data.
• It is identified by {a,b,c}
• It is mutable.
5
[26]: x4={2,4,3,2,5,9,7,8,8,9,0,0,0,0,0}
print(x4)
{0, 2, 3, 4, 5, 7, 8, 9}
[27]: x=[1,2,3,4,2,3,5]
print(set(x))
{1, 2, 3, 4, 5}
6
1.5 Loops
1.5.1 while loop
[31]: i = 0
while (i < 5):
print("The value of i is "+str(i))
i = i + 1 # Removing this line will make infinite loop
The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 1
The value of i is 3
The value of i is 5
The value of i is 7
The value of i is 9
Enter a number: 12
Enter a number: 4
Enter a number: 5
Enter a number: 0
[12, 4, 5]
7
1.6 Continue Statement
[35]: i = 0
while i < 7:
i += 1
if i == 5:
continue
print(i)
1
2
3
4
6
7
1.7 Functions
[36]: def my_function(x):
return x*x
my_function(9)
[36]: 81
[37]: 15