-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter3.py
134 lines (103 loc) · 3.26 KB
/
Chapter3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# (#)Used for comment
# (=) used for assignment
a = 16
b = 3
c = a+b
# used to print anything
print(c)
#this // operator gives Floored Output
c = a//b
print(c)
#gives the remainder value of calculation
c = a%b
print(c)
# gives the a to the power of b
c = a**b
print(c)
# operators within the mixed operands convert the integer to floating point
c = a*3.75-1
print(c)
# in the interactive mode (_)variable automatically stores the last calculation
# round(var, upto) round off the variable to the given (upto) value
print(round(3.748564,2))
#Python has built-in data types to support complex numbers it is written in (j) OR (J)
# if we have numbers we can do d=3+4j but in variables case below method is used
d = a + b *1j
print(d)
print(type(d))
#STRINGS
#can be written in single or double quotes
name = 'zaid'
name = "zaidy"
print(name)
# (\) is used to skip special characters or quotes
# it will print zaidy's computer
name = 'zaidy\'s computer'
print(name)
# if outer sign is used inside the string then skip that otherwise there is no need
name = "zaidy's computer"
print(name)
# (\n) represents a new line character.
name = 'zaid amjad\ncomputer science'
print(name)
#if we want to skip the effect of backslash(\) then simply write (r) before the first quote
name = r'zaid amjad\ncomputer science'
print(name)
#we can write multiline String using tripe double/single quotes ("""...""") or ('''...''')
#we can also skip the first line if we write \.
name = '''\
Zaid
Amjad
Mughal
'''
print(name)
#Concatination in a String is done by + operator and Strings are repeated using * operator
name = 'zaid'
fname = ' amjad'
#it means 3 times name + 1 time fname
print(3*name+fname)
#there is another way of doing concatination that word with only literals not variable or expressions
#this method is useful in breaking strings to write more lines
name = 'za' 'id' ' amjad'
print(name)
#in python there is not a specific character type the string of length 0 is character.
#String can be indexed and sliced
print(name[0])
#prints the last character of the string
print(name[-1])
#Slicing is used to get the subString from the String
#in slicing first always included and last always excluded in this 0th index included and 2nd index excluded
print(name[0:2])
#useful Defaults of Slicing [:2] get the substring from start to 2nd pos and [2:] gives substring from 2nd to end of String
#that is why name[:2]+name[2:] is always equals to (name)
print(name[:2])
print(name[2:])
#when writing out of bound index this will generate error
#print(name[35])
#but if we used out-of-bound index in slicing it will handle very efficiently
print(name[42:])
print(name[5:35])
#we cannot assign values using String index or slicing (IMMUTABLE)
#len(name) gives the total length of string
print(len(name))
#LISTS IN PYTHON
#can be indexed and sliced and returns shallow copy of list when sliced
list = [1,2,3,4,5,6,7]
print(list)
#we can also concatenate list
print(list+[8,9])
#content is changable (mutable)
list[2] = 4
print(list)
#list can also be sliced
print(list[:4])
#len(list) gives the length of list
print(len(list))
#we can attach new element at the end of list using append() method
list.append(10)
print(list)
#lists can be nested
list.append([7,5,3,1])
print(list[-1])
#multiple assignment in python
a,b=0,1