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

Basic

The document provides an overview of basic data types in Python, including integers, floats, strings, and booleans, along with examples of their usage. It also covers operators, assignment, containers like lists and dictionaries, and various list operations such as adding, removing, and sorting elements. Additionally, it explains the characteristics of tuples and sets, emphasizing their mutability and unique properties.
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)
3 views14 pages

Basic

The document provides an overview of basic data types in Python, including integers, floats, strings, and booleans, along with examples of their usage. It also covers operators, assignment, containers like lists and dictionaries, and various list operations such as adding, removing, and sorting elements. Additionally, it explains the characteristics of tuples and sets, emphasizing their mutability and unique properties.
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

5/8/26, 3:07 PM Basic

Data types
integer | Float | String | Boolean

In [1]: 1 # integer

Out[1]: 1

In [2]: 1+5

Out[2]: 6

In [3]: type( 1 )

Out[3]: int

In [4]: 1.5 # float

Out[4]: 1.5

In [5]: 1.8*1.2

Out[5]: 2.16

In [6]: type(1.5)

Out[6]: float

In [7]: 'my name is kunal' # string

Out[7]: 'my name is kunal'

In [8]: type('my name is kunal')

Out[8]: str

In [9]: True

Out[9]: True

In [10]: False

Out[10]: False

In [11]: type(True) # boolean

Out[11]: bool

[Link] 1/14
5/8/26, 3:07 PM Basic

In [12]: 12354*True

Out[12]: 12354

In [13]: 123456*False

Out[13]: 0

In [14]: True + True + False

Out[14]: 2

operators

-- + | - | / | * | ** Power | // Modulo | % Remainder

In [15]: 10+85

Out[15]: 95

In [16]: 5-54

Out[16]: -49

In [17]: 2*78

Out[17]: 156

In [18]: 2**3 # POWER

Out[18]: 8

In [19]: 5 // 2 # Modulo

Out[19]: 2

In [20]: 5 % 2 # Remainder

Out[20]: 1

Membership operator

In [21]: 'z' in 'delhi'

Out[21]: False

In [22]: 'MY' in 'My name is kunal arora'

Out[22]: False

In [23]: 'MY'.lower() in 'My name is kunal arora'.lower()

[Link] 2/14
5/8/26, 3:07 PM Basic

Out[23]: True

In [24]: 'MY'.lower()

Out[24]: 'my'

Logical Operaters > | < | >= | <= | == equals | != not equals

In [25]: 2>5

Out[25]: False

In [26]: 10<50

Out[26]: True

In [27]: 2*8 == 16

Out[27]: True

In [28]: 'a' == 'a'

Out[28]: True

In [29]: 'A' == 'a'

Out[29]: False

In [30]: 'A'.lower() == 'a'

Out[30]: True

In [31]: 2 != 1+1

Out[31]: False

Asignment Operator = Variables Naming Convention

1. name cannot start with number


2. name cannot have special characters
3. name cannot have space in between
4. name cannot be assigned as keyword

In [32]: Number1 = 100

In [33]: Number1*2

Out[33]: 200

In [34]: Number1

[Link] 3/14
5/8/26, 3:07 PM Basic

Out[34]: 100

In [35]: full_name = 'kunal arora'

In [36]: full_name

Out[36]: 'kunal arora'

In [2]: and_ = 55 # this will not work because and is a keyword in python

In [38]: and_

Out[38]: 55

Containers
In [39]: # List [storage which can be updated]
L1 =[ 1,2,3,4,5,6,7 ]

In [40]: L1

Out[40]: [1, 2, 3, 4, 5, 6, 7]

In [41]: type(L1)

Out[41]: list

In [42]: y = [1.1 , 1 , 'a' , True ]

In [43]: y

Out[43]: [1.1, 1, 'a', True]

Add
In [44]: L1

Out[44]: [1, 2, 3, 4, 5, 6, 7]

In [45]: [Link](100) # for single value addition

In [46]: L1

Out[46]: [1, 2, 3, 4, 5, 6, 7, 100]

In [47]: [Link]([786,787]) # addition of more than 1 value

[Link] 4/14
5/8/26, 3:07 PM Basic

In [48]: L1

Out[48]: [1, 2, 3, 4, 5, 6, 7, 100, 786, 787]

In [49]: L1[0]

Out[49]: 1

In [50]: L1[-1]

Out[50]: 787

In [51]: [Link](2,500)

In [52]: L1

Out[52]: [1, 2, 500, 3, 4, 5, 6, 7, 100, 786, 787]

Removal
In [53]: [Link](100)

In [54]: L1

Out[54]: [1, 2, 500, 3, 4, 5, 6, 7, 786, 787]

In [55]: del L1[-1]

In [56]: L1

Out[56]: [1, 2, 500, 3, 4, 5, 6, 7, 786]

In [57]: del L1[-2]

In [58]: L1

Out[58]: [1, 2, 500, 3, 4, 5, 6, 786]

In [59]: backup = [Link](0)

In [60]: backup

Out[60]: 1

In [61]: L1

Out[61]: [2, 500, 3, 4, 5, 6, 786]

[Link] 5/14
5/8/26, 3:07 PM Basic

Sort
In [62]: [Link]()

In [63]: L1

Out[63]: [2, 3, 4, 5, 6, 500, 786]

In [64]: [Link](reverse=True)

In [65]: L1

Out[65]: [786, 500, 6, 5, 4, 3, 2]

Slicing
In [66]: X = [57,43,64,99,33]

In [67]: X[0]

Out[67]: 57

In [68]: X[1]

Out[68]: 43

In [69]: X[-1]

Out[69]: 33

In [70]: X[0:2] # start_index : end_index

Out[70]: [57, 43]

In [71]: X[0:5]

Out[71]: [57, 43, 64, 99, 33]

In [72]: X[0:5:2] # start_index : end_index : step size

Out[72]: [57, 64, 33]

In [73]: X

Out[73]: [57, 43, 64, 99, 33]

In [74]: X[-1:-6:-1]

[Link] 6/14
5/8/26, 3:07 PM Basic

Out[74]: [33, 99, 64, 43, 57]

In [75]: X[-1:-6:-1]

Out[75]: [33, 99, 64, 43, 57]

In [76]: X[:2]

Out[76]: [57, 43]

In [77]: X[2:]

Out[77]: [64, 99, 33]

In [78]: X

Out[78]: [57, 43, 64, 99, 33]

In [79]: X[::-1] # Reverse

Out[79]: [33, 99, 64, 43, 57]

In [80]: X[:]

Out[80]: [57, 43, 64, 99, 33]

In [81]: X

Out[81]: [57, 43, 64, 99, 33]

Count
In [82]: Fruits = ['apple','apple','banana','banana','apple','apple']

In [83]: [Link]('banana')

Out[83]: 2

In [84]: L2 = [1,2,1,5,6,1]

In [85]: [Link](1)

Out[85]: 3

index
In [86]: [Link](5) # index number of a value [first occurance]

[Link] 7/14
5/8/26, 3:07 PM Basic

Out[86]: 3

In [87]: [Link](1,3) # getting index number of 1 in a list while seaching from index 3

Out[87]: 5

item Assignment
In [88]: x = [1,2,3,4]

In [89]: x[-1] = 400

In [90]: x

Out[90]: [1, 2, 3, 400]

In [91]: x[0] = 100

In [92]: x

Out[92]: [100, 2, 3, 400]

Aggregate
In [97]: x=[11,22,33,44]

In [98]: x

Out[98]: [11, 22, 33, 44]

In [104… len(x) # count number of values

Out[104… 4

In [105… sum(x) # total

Out[105… 110

In [101… min(x)

Out[101… 11

In [102… max(x)

Out[102… 44

In [106… sum(x)/len(x) # average = total / count

[Link] 8/14
5/8/26, 3:07 PM Basic

Out[106… 27.5

tuple [storage which cannot be changed ]

In [108… a = 1,2,3

In [109… a

Out[109… (1, 2, 3)

In [110… a =(1,2,3)

In [111… a

Out[111… (1, 2, 3)

In [116… # a[0] = 100 # Hence tuple cannot be changed

In [117… # list is mutable whereas tuple is immutable

SET
In [119… # 1) remove duplicates
# 2) sort in ascending order
{ 1,2,3,1,4,7,5 }

Out[119… {1, 2, 3, 4, 5, 7}

In [120… S1 = {1,2,3,4}
S2 = {1,2,3,5}

In [123… S1 & S2 # common elements in both set [& = intersection ]

Out[123… {1, 2, 3}

In [124… [Link](S2)

Out[124… {1, 2, 3}

In [129… S1 ^ S2 # Anti

Out[129… {4, 5}

In [128… S1 - S2 # LEft Anti

Out[128… {4}

In [127… S2 - S1

[Link] 9/14
5/8/26, 3:07 PM Basic

Out[127… {5}

In [130… [Link](S2)

Out[130… {1, 2, 3, 4, 5}

In [132… [Link](1.5)

In [133… S1

Out[133… {1, 1.5, 2, 3, 4}

In [134… [Link](3)

In [135… S1

Out[135… {1, 1.5, 2, 4}

Dictionary
Key value Pairs

In [182… Employees = { 'Emp_id' : [1,2,3,4] ,


'Name' : ['Raam','Shyam','Hari','Murli'] ,
'Salary' : [120846,799879,49849,12344] }

In [183… Employees

Out[183… {'Emp_id': [1, 2, 3, 4],


'Name': ['Raam', 'Shyam', 'Hari', 'Murli'],
'Salary': [120846, 799879, 49849, 12344]}

add Key

In [184… Employees['HRA'] = (10,20,30,40)

In [185… Employees

Out[185… {'Emp_id': [1, 2, 3, 4],


'Name': ['Raam', 'Shyam', 'Hari', 'Murli'],
'Salary': [120846, 799879, 49849, 12344],
'HRA': (10, 20, 30, 40)}

In [186… Employees['HRA'][0] = 100 # tuple cannot be changed

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[186], line 1
----> 1 Employees['HRA'][0] = 100 # tuple cannot be changed

TypeError: 'tuple' object does not support item assignment

[Link] 10/14
5/8/26, 3:07 PM Basic

In [187… Employees['HRA'] = [10,20,30,40] # we cannot have duplicate keys inside dictionary


# so it overwrites

In [188… Employees

Out[188… {'Emp_id': [1, 2, 3, 4],


'Name': ['Raam', 'Shyam', 'Hari', 'Murli'],
'Salary': [120846, 799879, 49849, 12344],
'HRA': [10, 20, 30, 40]}

In [189… Employees['HRA'][0] = 100

In [190… Employees

Out[190… {'Emp_id': [1, 2, 3, 4],


'Name': ['Raam', 'Shyam', 'Hari', 'Murli'],
'Salary': [120846, 799879, 49849, 12344],
'HRA': [100, 20, 30, 40]}

delete Key

In [191… del Employees['HRA']

In [192… Employees

Out[192… {'Emp_id': [1, 2, 3, 4],


'Name': ['Raam', 'Shyam', 'Hari', 'Murli'],
'Salary': [120846, 799879, 49849, 12344]}

In [193… Employees ['Name'] # view values inside key

Out[193… ['Raam', 'Shyam', 'Hari', 'Murli']

In [194… [Link]('Name') # view values inside key

Out[194… ['Raam', 'Shyam', 'Hari', 'Murli']

In [199… sum(Employees['Salary']) # total Salary

Out[199… 982918

In [200… len(Employees['Salary']) # count of records

Out[200… 4

In [201… min(Employees['Salary'])

Out[201… 12344

In [202… max(Employees['Salary'])

Out[202… 799879

[Link] 11/14
5/8/26, 3:07 PM Basic

In [204… sum(Employees['Salary']) / len(Employees['Salary']) # Average

Out[204… 245729.5

In [205… pwd

Out[205… 'D:\\ducat\\Class Files\\Weekend\\18-00\\rev'

String
In [210… S1 = 'my name is kunal' # normal String
S2 = "my name is 'kunal' arora" # sub string
S3 = ''' my name is
kunal arora ''' # Multiple Line String

In [211… S1

Out[211… 'my name is kunal'

In [212… print(S1)

my name is kunal

In [213… print(S2)

my name is 'kunal' arora

In [214… print(S3)

my name is
kunal arora

In [216… S3 # \n is for next line

Out[216… ' my name is \nkunal arora '

Case Sensativity
In [217… Reviews = 'Good GOOD good gOoD'

In [246… [Link]('good')

Out[246… 1

In [225… [Link]() # all characters will be in small case

Out[225… 'good good good good'

In [220… [Link]().count('good')

[Link] 12/14
5/8/26, 3:07 PM Basic

Out[220… 4

In [224… [Link]() # all characters will be in capital case

Out[224… 'GOOD GOOD GOOD GOOD'

In [223… [Link]() # 1st character of every word will be capital

Out[223… 'Good Good Good Good'

In [226… [Link]() # 1st character of text will be capital

Out[226… 'Good good good good'

In [227… Reviews

Out[227… 'Good GOOD good gOoD'

In [229… [Link]() # capital letters will become small and small will become capita

Out[229… 'gOOD good GOOD GoOd'

In [233… len(Reviews) # total characters in text including space , special , digits , alphab

Out[233… 19

In [235… Reviews

Out[235… 'Good GOOD good gOoD'

In [236… [Link]('good','bad')

Out[236… 'Good GOOD bad gOoD'

In [237… S1

Out[237… 'my name is kunal'

In [238… [Link]('is')

Out[238… 8

In [239… S4 = 'my name i,s kunal'

In [244… [Link]('is')

Out[244… -1

In [20]: { 1,2,1,3,4,7,5 } # 1) remove Duplicate , 2) sort in ascending

In [5]: S1 = { 1,2,1,3,4,7,5 }

[Link] 13/14
5/8/26, 3:07 PM Basic

In [21]: S1

In [7]: S2 = {1,2,3,6}

In [22]: S2

In [23]: S1 & S2 # intersection [Common Elements]

In [24]: S1 ^ S2 # Anti

In [25]: type(S1)

In [26]: S1 - S2 # LEft Anti

In [14]: [Link](58)

In [15]: S1

Out[15]: {1, 2, 3, 4, 5, 7, 58}

In [16]: [Link](3.5)

In [17]: S1

Out[17]: {1, 2, 3, 3.5, 4, 5, 7, 58}

In [18]: [Link](3.5)

In [19]: S1

Out[19]: {1, 2, 3, 4, 5, 7, 58}

In [ ]:

[Link] 14/14

You might also like