0% found this document useful (0 votes)
2 views6 pages

Notes-1 Introduction To Python

This document provides an introduction to Python, highlighting its features such as high-level readability, ease of use, and applications in data analysis and machine learning. It covers data types including primitive types (integer, float, string, boolean, complex) and core types (list, tuple, dictionary, array, set), along with examples of their usage. Additionally, it explains basic operations and logical operators in Python.

Uploaded by

hajerakhatunth
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Notes-1 Introduction To Python

This document provides an introduction to Python, highlighting its features such as high-level readability, ease of use, and applications in data analysis and machine learning. It covers data types including primitive types (integer, float, string, boolean, complex) and core types (list, tuple, dictionary, array, set), along with examples of their usage. Additionally, it explains basic operations and logical operators in Python.

Uploaded by

hajerakhatunth
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Notes-1 Introduction to Python

September 16, 2024

1 Introduction
• Python is a high-level, interpreted, 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
• Create an API (Application program interface)
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, Tupple, 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(1.25456,2) # round-off of a number to 2 decimal places

[8]: 1.25

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

[9]: 3.457

[10]: x=14
y=5
z=2

Addition

[11]: print(x+y)

19
Subtraction

[12]: print(x-y)

9
Multiplication

[13]: print(x*y)

70
Division

[14]: print(x/y)

2.8
Modulus Operation

[15]: x%y

[15]: 4

Floor division

[16]: x//y

[16]: 2

Exponentiation

[17]: x**z

[17]: 196

[18]: 2**(0.5)

[18]: 1.4142135623730951

3
1.2 Python 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. not: Reverse the result, returns False if the result is true, Example: not(x < 5 and x < 10)

1.3 Core
1.3.1 List
• Group of hetarogeneous data, i.e., mix of different data types
• It is identified by [a, b, c]
• It is mutable, i.e., can be modified.

[19]: 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

[20]: print(x1[0]) # To print first element


print(x1[0:3]) # To print first 3 elements
print(x1[1:]) # To print all elements starting from index 1
print(x1[:4]) # To print element whose index is less than 4
print(x1[1:4]) # To print all elements starting from index 1 and less than 4
print(x1[-1]) # To print last element
print(x1[:-2]) # To print element whose index is less than -2
print(x1[-2:-4])
print(sum(x1))

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

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

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

4
[22]: x1[-1]=7
print(x1)

[10, 10, 30, 40, 50, 7]

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

[10, 0, 0, 0, 50, 7]

1.3.2 Tupple
• It is similar to list but identified by (a,b,c).
• It is immutable, i.e., can not be modified.

[24]: x2=(10,20.0,30,40,50,"a")
print(x2)
print(type(x2[-1]))
print(x2[0],x2[1],x2[2],x2[3],x2[4],x2[5],x2[-1],x2[-2],x2[-3],x2[-4],x2[-5],x2[-6])
print(x2[1:3])

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


<class 'str'>
10 20.0 30 40 50 a a 50 40 30 20.0 10
(20.0, 30)

[25]: #x2[1]=100

1.3.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.

[26]: 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'}

[27]: 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'}

5
1.3.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.

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


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

[ 2 5 62 5 42 52 48 5]
<class '[Link]'>

1.3.5 Sets
• It is unordered collection of unique data.
• It is identified by {a,b,c}
• It is mutable.

[29]: 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}

[32]: x=[1,2,3,4,2,3,5]
print(set(x))

{1, 2, 3, 4, 5}

You might also like