0% found this document useful (0 votes)
13 views

Python Fundamentals for Non - Programmers

Uploaded by

Prajwal Jagadish
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)
13 views

Python Fundamentals for Non - Programmers

Uploaded by

Prajwal Jagadish
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
You are on page 1/ 15

7/4/23, 7:52 PM training python basic's

In [1]: employee_name = "'vismit'"

In [2]: employee_name

Out[2]: "'vismit'"

In [6]: employee_name = "'nandan'"


employee_name

Out[6]: "'nandan'"

In [7]: employee_name

Out[7]: "'nandan'"

In [11]: employee_name = "vinil"


employee_name

Out[11]: 'vinil'

In [16]: employe_name = "vismit"


employee_name

Out[16]: 'vismit'

In [17]: data_type = 3.88


data_type

Out[17]: 3.88

In [18]: type(data_type)

Out[18]: float

In [19]: data_type2 = "'23'"


data_type2

Out[19]: "'23'"

In [20]: type(data_type2)

Out[20]: str

In [21]: type(employee_name)

Out[21]: str

In [22]: data_type3 = 88
data_type3

Out[22]: 88

In [23]: type(data_type3)

Out[23]: int

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 1/15


7/4/23, 7:52 PM training python basic's

In [28]: data_type5 = True


data_type5

Out[28]: True

In [29]: type(data_type5)

Out[29]: bool

In [30]: a1 = 80+2843j
a1

Out[30]: (80+2843j)

In [31]: type(a1)

Out[31]: complex

In [33]: a=60

b=20
#+,-,*,/ (all arthematic operators)

In [34]: a+b

Out[34]: 80

In [35]: a-b

Out[35]: 40

In [36]: b-a

Out[36]: -40

In [37]: a*b

Out[37]: 1200

In [38]: a/b

Out[38]: 3.0

In [43]: #just for testing


data_type6 = 3
data_type7 = 3.0
data_type6 , data_type7

Out[43]: (3, 3.0)

In [57]: print("data_type6 =",type(data_type6), "data_type7 =",type(data_type7),"-hence , we can co

data_type6 = <class 'int'> data_type7 = <class 'float'> -hence , we can conclude that 3 is an i
nt data type and 3.0 is an float data type

In [58]: #Relational Operators >,< ,==,!=

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 2/15


7/4/23, 7:52 PM training python basic's

In [60]: a>b # as assigned a = 60 , b = 20

Out[60]: True

In [61]: a<b

Out[61]: False

In [62]: a==b

Out[62]: False

In [63]: a!=b

Out[63]: True

In [64]: #if
a1=100
a2=100
#then
a1==a2

Out[64]: True

In [65]: # Logical Operatiors & , | (press shift + \ this type hash button above enter button)

In [67]: a=True
b=False

In [69]: a&b

Out[69]: False

In [70]: b&a

Out[70]: False

In [71]: b&b

Out[71]: False

In [72]: a&a

Out[72]: True

In [73]: a|b

Out[73]: True

In [75]: b|a

Out[75]: True

In [76]: b|b

Out[76]: False

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 3/15


7/4/23, 7:52 PM training python basic's

In [77]: a|a

Out[77]: True

In [84]: a= 'hello world'


a

Out[84]: 'hello world'

In [85]: a[0]

Out[85]: 'h'

In [86]: a[3]

Out[86]: 'l'

In [87]: a[-1]

Out[87]: 'd'

In [88]: a[-0]

Out[88]: 'h'

In [89]: a[-2]

Out[89]: 'l'

In [90]: a[5]

Out[90]: ' '

In [91]: a[10]

Out[91]: 'd'

In [92]: a[-1]

Out[92]: 'd'

In [93]: a[-10]

Out[93]: 'e'

In [94]: a[-11]

Out[94]: 'h'

In [96]: a= 'hi people iam a human'


a

Out[96]: 'hi people iam a human'

In [101… a[3:9]

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 4/15


7/4/23, 7:52 PM training python basic's

Out[101]: 'people'

In [109… a[-5:23]

Out[109]: 'human'

In [111… len(a)

Out[111]: 21

In [113… a[-21]

Out[113]: 'h'

In [114… a.upper()

Out[114]: 'HI PEOPLE IAM A HUMAN'

In [118… a

Out[118]: 'hi people iam a human'

In [123… a.replace('people', 'ppl')

Out[123]: 'hi ppl iam a human'

In [124… a

Out[124]: 'hi people iam a human'

In [125… a.count('p')

Out[125]: 2

In [126… a

Out[126]: 'hi people iam a human'

In [141… a.find('human')

Out[141]: 20

In [147… a.find('m')

Out[147]: 14

In [130… a[10:13]

Out[130]: 'iam'

In [131… a

Out[131]: 'hi people iam a human'

In [132… a= 'hi people . iam . a human'


a.split('.')

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 5/15


7/4/23, 7:52 PM training python basic's

Out[132]: ['hi people ', ' iam ', ' a human']

In [133… a

Out[133]: 'hi people . iam . a human'

In [136… a= 'hi people % iam a % human' #you can use any symbol to split examples used here = . & %
a.split('%')

Out[136]: ['hi people ', ' iam a ', ' human']

In [2]: a = 'hi'
a.replace('hi',"hey")

Out[2]: 'hey'

In [3]: a.count('hi')

Out[3]: 1

In [4]: a.count('i')

Out[4]: 1

In [5]: a.count("h")

Out[5]: 1

In [6]: a[0:1]

Out[6]: 'h'

In [7]: a[0:2]

Out[7]: 'hi'

In [8]: a= "hi my name is xyz"


a

Out[8]: 'hi my name is xyz'

In [11]: a.count('n') #count function is to find how many time the letter or albhabet is repeated

Out[11]: 1

In [12]: #find function is to find in which place letter or number is .


a.find('n')

Out[12]: 6

In [13]: #Concept Tuples ( a type of data structure ) (tuples are immutable or the values inside it ca
#NOTE = the major difference in assigining lists and tuples are :
# IN LIST we use square brackets [] to assign values
# BUT in TUPLES we use round brackets () to assign values .
# eg, list = [1,2,3]
# tuple = (1,2,3)

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 6/15


7/4/23, 7:52 PM training python basic's

In [56]: tuple1 = (1,'hello',True,3.14)

In [57]: tuple1

Out[57]: (1, 'hello', True, 3.14)

In [24]: type(tuple1)

Out[24]: tuple

In [63]: tuple1[-1]

Out[63]: 3.14

In [58]: tuple1[-2]

Out[58]: True

In [59]: tuple1[:]

Out[59]: (1, 'hello', True, 3.14)

In [30]: tuple1

Out[30]: (1, 'hello', True, 3.14)

In [60]: tuple1[2]

Out[60]: True

In [38]: tuple1 = (1,'hello',True,3.14)

In [61]: tuple1[3]

Out[61]: 3.14

In [62]: tuple1.replace ('hello','jet') #tuples are immutable (cant be changed once assigned)

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[62], line 1
----> 1 tuple1.replace ('hello','jet')

AttributeError: 'tuple' object has no attribute 'replace'

In [48]: a = 'hi'

In [50]: a.replace ('hi', 'hey') #but normal variables can be changed

Out[50]: 'hey'

In [65]: tuple2 = (88,"hey",34+5j,False)


tuple3 = (8, "op",70+45634232j,3.5623323323,True,True,'feeadfqqdefwevwfadvwdqcwewdawd

In [66]: tuple1 + tuple2

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 7/15


7/4/23, 7:52 PM training python basic's

Out[66]: (1, 'hello', True, 3.14, 88, 'hey', (34+5j), False)

In [67]: tuple2+tuple3

Out[67]: (88,
'hey',
(34+5j),
False,
8,
'op',
(70+45634232j),
3.5623323323,
True,
True,
'feeadfqqdefwevwfadvwdqcwewdawdwfc you need not to understand lol ')

In [68]: tuple1+tuple3

Out[68]: (1,
'hello',
True,
3.14,
8,
'op',
(70+45634232j),
3.5623323323,
True,
True,
'feeadfqqdefwevwfadvwdqcwewdawdwfc you need not to understand lol ')

In [69]: tuple2+tuple1

Out[69]: (88, 'hey', (34+5j), False, 1, 'hello', True, 3.14)

In [70]: #repeating the elements in tuple agian and agian how much times we want

In [71]: tuple4 = (8,"hi",True)

In [72]: tuple4

Out[72]: (8, 'hi', True)

In [74]: tuple4*2

Out[74]: (8, 'hi', True, 8, 'hi', True)

In [77]: tuple1

Out[77]: (1, 'hello', True, 3.14)

In [78]: tuple4

Out[78]: (8, 'hi', True)

In [79]: tuple4*3 + tuple1

Out[79]: (8, 'hi', True, 8, 'hi', True, 8, 'hi', True, 1, 'hello', True, 3.14)

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 8/15


7/4/23, 7:52 PM training python basic's

In [80]: # to find the minimum or smallest number value of tuple and maximum or largest number pre
#remember its just for finding largest and smallest number dosent apply for letters in tuple.

In [85]: tuple4 = (88,556,421,54,-32,)


tuple4

Out[85]: (88, 556, 421, 54, -32)

In [86]: min(tuple4)

Out[86]: -32

In [87]: max(tuple4) #hence here min number is -32 and max number is 556

Out[87]: 556

In [88]: #concept LISTS they are mutable or can change values in it. ( a type of data structure )
#NOTE = the major difference in assigining lists and tuples are :
# IN LIST we use square brackets [] to assign values
# BUT in TUPLES we use round brackets () to assign values .
# eg, list = [1,2,3]
# tuple = (1,2,3)

In [174… list1 = [8,'hey',True,3+5j]

In [175… type(list1)

Out[175]: list

In [176… list1

Out[176]: [8, 'hey', True, (3+5j)]

In [177… list1[1]

Out[177]: 'hey'

In [178… list1[0]

Out[178]: 8

In [179… list1[1:4]

Out[179]: ['hey', True, (3+5j)]

In [180… #changing the values inside list as lists are mutable or changable.

In [181… list1

Out[181]: [8, 'hey', True, (3+5j)]

In [182… list1[2] = 'hello people'

In [183… list1

Out[183]: [8, 'hey', 'hello people', (3+5j)]

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 9/15


7/4/23, 7:52 PM training python basic's

In [184… #if we want to add an element at the end of list .

In [185… list1.append('ok bye bye ')

In [186… list1

Out[186]: [8, 'hey', 'hello people', (3+5j), 'ok bye bye ']

In [187… #to remove the last element in list .

In [188… list1.pop()

Out[188]: 'ok bye bye '

In [189… list1 #here in the output the last emelment (which was ok bye bye) is popped or removed ou

Out[189]: [8, 'hey', 'hello people', (3+5j)]

In [190… list1.pop(2)

Out[190]: 'hello people'

In [191… list1

Out[191]: [8, 'hey', (3+5j)]

In [192… #reverse the elements of list

In [193… list1

Out[193]: [8, 'hey', (3+5j)]

In [194… list1.reverse()
list1

Out[194]: [(3+5j), 'hey', 8]

In [195… #inserting an new element inside or in middle or a specified index in list.

In [196… list1

Out[196]: [(3+5j), 'hey', 8]

In [197… list1.insert(2, True)

In [198… list1 #type the index number of element in which u have to insert the new element and the g

Out[198]: [(3+5j), 'hey', True, 8]

In [199… #to arrange a list in alphabetical order

In [200… list2 = ['c','b','d','a']

In [201… list2

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 10/15


7/4/23, 7:52 PM training python basic's

Out[201]: ['c', 'b', 'd', 'a']

In [202… list2.sort()

In [203… list2

Out[203]: ['a', 'b', 'c', 'd']

In [204… #adding or concatinating 2 lists

In [205… list1

Out[205]: [(3+5j), 'hey', True, 8]

In [206… list2

Out[206]: ['a', 'b', 'c', 'd']

In [207… list1+list2

Out[207]: [(3+5j), 'hey', True, 8, 'a', 'b', 'c', 'd']

In [208… list2+list1

Out[208]: ['a', 'b', 'c', 'd', (3+5j), 'hey', True, 8]

In [209… #to repeat the elements of the list .

In [212… list2*3

Out[212]: ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']

In [213… list2*2+list1

Out[213]: ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', (3+5j), 'hey', True, 8]

In [214… #concept DICTIONARY , ( a type of data structure )


#it is mutable and in dictonary we use flower bracket { } to assign values

In [215… # dictonary can be assigned any name for ex fruits , cars , bikes ,foods .
#means it is having the specific category like fruit dictonary have set of fruits like apple orrang

In [1]: fruit = {'apple':30, 'banana':50} #here 30 and 50 are cost or number of apple and b

In [2]: #here the apple and banana (the alphabets ) is called as KEYS of dictonary .
# And 30 and 50 (the numbers) are called as values of dictonary.

In [3]: fruit

Out[3]: {'apple': 30, 'banana': 50}

In [4]: type(fruit)

Out[4]: dict

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 11/15


7/4/23, 7:52 PM training python basic's

In [5]: #to extract only keys or alphabets of the dictonary called fruits

In [6]: fruit

Out[6]: {'apple': 30, 'banana': 50}

In [7]: fruit.keys()

Out[7]: dict_keys(['apple', 'banana'])

In [8]: #to extract the values or numbers of dictonary

In [9]: fruit

Out[9]: {'apple': 30, 'banana': 50}

In [10]: fruit.values()

Out[10]: dict_values([30, 50])

In [11]: #to extract the items in dictonary .

In [12]: fruit

Out[12]: {'apple': 30, 'banana': 50}

In [13]: fruit.items()

Out[13]: dict_items([('apple', 30), ('banana', 50)])

In [14]: #to add a new item in dictonary (an item consists of a key and value )

In [15]: fruit

Out[15]: {'apple': 30, 'banana': 50}

In [18]: fruit['mango'] = 40

In [19]: fruit

Out[19]: {'apple': 30, 'banana': 50, 'mango': 40}

In [20]: #to change the value of a key .


#ex. to change cost of apple from 30 to 80.

In [21]: fruit['apple']= 80

In [22]: fruit

Out[22]: {'apple': 80, 'banana': 50, 'mango': 40}

In [23]: #to add a new set of items into a dictonary from another dictonary or to merge the items in a

Out[23]: {'apple': 80, 'banana': 50, 'mango': 40}

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 12/15


7/4/23, 7:52 PM training python basic's

In [24]: fruit

Out[24]: {'apple': 80, 'banana': 50, 'mango': 40}

In [25]: # specialfruits = 2nd dictonary or sub - dictonary

In [30]: specialfruit= {'watermelon':45, 'musk melon':28}


specialfruit

Out[30]: {'watermelon': 45, 'musk melon': 28}

In [31]: # updating or adding specialfruits sub - dictonary to main fruit dictonary.

In [32]: fruit

Out[32]: {'apple': 80, 'banana': 50, 'mango': 40}

In [33]: specialfruit

Out[33]: {'watermelon': 45, 'musk melon': 28}

In [34]: fruit.update(specialfruit)

In [35]: fruit #hence the special fruit dictonary is added to a single dictonary named fruit .

Out[35]: {'apple': 80, 'banana': 50, 'mango': 40, 'watermelon': 45, 'musk melon': 28}

In [36]: # to pop or remove an item from dictonary. (item is a collection of a key [alphabet] and a

In [37]: fruit

Out[37]: {'apple': 80, 'banana': 50, 'mango': 40, 'watermelon': 45, 'musk melon': 28}

In [38]: fruit.pop("banana")

Out[38]: 50

In [39]: fruit #hence, here the banana item is been removed from dictonary .

Out[39]: {'apple': 80, 'mango': 40, 'watermelon': 45, 'musk melon': 28}

In [40]: # concept SET ( a type of data structure ) (sets are MUTABLE or can change values inside
# set is a unordered and unindexed (the single elements dosent have its specific place or num
# we use curly brackets {} to assign a set .
# NOTE : in dictonary also we use curly brackets but there we have a key (word) and a value
# - ents present in set
# no elements can be repeated twice in set . (there should not be the same number or word a

In [74]: set1 = {1,'hi','hi',6} # ex here in the set i hve repeated hi 2 times but in output it came only onc
set1

Out[74]: {1, 6, 'hi'}

In [75]: #adding a new element to set .

In [76]: set1

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 13/15


7/4/23, 7:52 PM training python basic's

Out[76]: {1, 6, 'hi'}

In [77]: set1.add("ok")
set1

Out[77]: {1, 6, 'hi', 'ok'}

In [78]: #to remove an existing element from set .

In [79]: set1

Out[79]: {1, 6, 'hi', 'ok'}

In [80]: set1.remove("hi")
set1

Out[80]: {1, 6, 'ok'}

In [81]: # updating or adding or merging a random group of elements to a set

In [82]: set1

Out[82]: {1, 6, 'ok'}

In [83]: set1.update(['hello']) # note : seee output u cant even repeat letters in a set .
# In set for updating u shd use both round and square brackets ([
set1 #if u dont use both brackets it comes like {}'h','e','l','o', } if u use it c

Out[83]: {1, 6, 'hello', 'ok'}

In [84]: set3 = {1,2,3,4}


set4 = {3,4,5,6}

In [85]: #to find union of 2 sets

In [86]: set3

Out[86]: {1, 2, 3, 4}

In [87]: set4

Out[87]: {3, 4, 5, 6}

In [88]: set3.union(set4)

Out[88]: {1, 2, 3, 4, 5, 6}

In [89]: #to find intersection of 2 sets

In [90]: set3

Out[90]: {1, 2, 3, 4}

In [91]: set4

Out[91]: {3, 4, 5, 6}

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 14/15


7/4/23, 7:52 PM training python basic's

In [92]: set3.intersection(set4)

Out[92]: {3, 4}

In [ ]:

localhost:8888/nbconvert/html/training python basic's.ipynb?download=false 15/15

You might also like