0% found this document useful (0 votes)
3 views8 pages

Workshop Notes-1 Introduction to Python

Uploaded by

aaliabushra1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views8 pages

Workshop Notes-1 Introduction to Python

Uploaded by

aaliabushra1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Workshop Notes-1 Introduction to Python

January 29, 2025

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

[1]: a1=20 # Consider as an integer


print(a1)
print(type(a1))
a11=float(a1)
print(type(a11))

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

[3]: a6=float(10) # Conversion to float


print(a6)

10.0

1.1.3 String

[4]: a3="Python" # Consider as a string


print(type(a3))

<class 'str'>

1.1.4 Boolean
[5]: a4=False
print(a4)
print(type(a4))

False
<class 'bool'>

1.1.5 Complex Number

[6]: a5=10+3j
print(type(a5))

<class 'complex'>

1.1.6 Absolute value/modulas of complex number

[7]: abs(-3+4j)

[7]: 5.0

1.1.7 Round off a number:


[8]: round(3.456789,3) # round-off of a number to 3 decimal places

[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

1.1.8 Logical Operators


1. and: Returns True if both statements are true, Example: x < 5 and x < 10
2. or: Returns True if one of the statements is true, Example: x < 5 or x < 4

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, 50, 60]


<class 'list'>
6
Slicing and Indexing

[19]: print(x1[0]) # To print first elemen


print(x1[1:4]) # To print all elements starting from index 1 and less than 4
print(x1[-4:-2]) # Negative index starts from last and continue in reverse order

10
[20, 30, 40]
[30, 40]
To replace elements by specific values

[20]: x1[1]=10
print(x1)

[10, 10, 30, 40, 50, 60]

[21]: x1[1:4]=[0,0,0]
print(x1)

[10, 0, 0, 0, 50, 60]

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)

(10, 20.0, 30, 40, 50, 'a')

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'}

[24]: x3={"STA202001":"Name 1", "STA202002":"Name 2", "STA202003":"Name 3"}


print(x3)
x3['STA202004']="Name 4"
x3['STA202005']="Name 5"
print(x3)

{'STA202001': 'Name 1', 'STA202002': 'Name 2', 'STA202003': 'Name 3'}


{'STA202001': 'Name 1', 'STA202002': 'Name 2', 'STA202003': 'Name 3',
'STA202004': 'Name 4', 'STA202005': 'Name 5'}

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.

[25]: import numpy as np # np: nickname of numpy


numbers_list = [2, 5, 62, 5, 42, 52, 48, 5]
numbers_array = np.array(numbers_list)
print(numbers_array)
print(type(numbers_array))

[ 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}

1.2.6 Quiz-1 Which of the following is not mutmutable?


(a) List (b) Tupple (c) Set (d) Dictionary

1.3 Conditional statements


1.3.1 If statement
[28]: a=10
if a>0:
print("You entered number " + str(a) + " is a positive number")

You entered number 10 is a positive number

1.4 If-else statement


[29]: a=-1
if a>0:
print("You entered number " + str(a) + " is a positive number.")
else:
print("You entered number " + str(a) + " is not a positive number.")

You entered number -1 is not a positive number.

1.4.1 Nested else-if


[30]: a=0
if a>0:
print("Given number is " + str(a) + ", which is a positive number.")
elif a<0:
print("Given number is " + str(a) + ", which is a negative number.")
else:
print("Given number is zero.")

Given number is zero.

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

1.5.2 For loop

[32]: for i in range(5): # range(a) means i=0, i<a, i=i+1


print("The value of i is: " + str(i))

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

[33]: for i in range(1,10,2): # range(a,b,r) means i=a, i<b, i=i+r


print("The value of i is "+str(i))

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

1.5.3 Break Statement


[34]: x=[]
while True:
a=int(input("Enter a number: "))
if a==0:
break
x.append(a)
print(x)

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

Sum of first n natural numbers:

[37]: def sum(n):


s=0
for i in range(1,(n+1)):
s=s+i
return(s)
sum(5)

[37]: 15

You might also like