-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.1 Operators in python
More file actions
166 lines (148 loc) · 2.87 KB
/
9.1 Operators in python
File metadata and controls
166 lines (148 loc) · 2.87 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Operator in python
Operator : operator which operator on operands ,its symbol used to calc,compare,assign
operand: operand constant and variable in programming instruction
x=y+2
varible x,y
constant--2
operator ===>+,=
Types of operator
1)Arithmetic operator
+,-,,/ ,//(floor division),*(exponent)
%(remainder)
2)Relational/Comparision operator
<,<=,>=,==,!=
3)* Logical operators
&&,||,!===C/C++/java
//change
and,or,not---> keywords operator level
4) bitwise operator
&,|,~ ^,>>,<<
5) Assignment operator
=
//shorthand
+=,-= ,=,/=,//=,*=
&=,/=,^=,>>=,<<=
//special operator
6) Identity operator
is ,is not
7) Membership operator
in ,not in
PRACTICE
>>> var1=100
>>> var2=200
>>> var1+var2
300
>>> var1=100
>>> var2="one"
>>> var1+var2
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
var1+var2
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> var1="One"
>>> var2="two"
>>> var1+var2
'Onetwo'
>>> #concate
>>> var1=100
>>> var2=22.22
>>> var1+var2
122.22
>>> var1=True
>>> var2=20
>>> var1+var2
21
>>> True*2+False*5+True
3
>>>
>>> 20-20
0
>>> 20-2.2
17.8
>>> 20-"20"
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
20-"20"
TypeError: unsupported operand type(s) for -: 'int' and 'str'
>>> "20"-"20"
Traceback (most recent call last):
File "<pyshell#60>", line 1, in <module>
"20"-"20"
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> "20"+"20" #concat
'2020'
>>> 20-True
19
>>> True-True
0
>>> 2+3j-2
3j
>>> 2*3
6
>>> 2*2.2
4.4
>>> 2*"sushil"
'sushilsushil'
>>> 2.2*"sushil"
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
2.2*"sushil"
TypeError: can't multiply sequence by non-int of type 'float'
>>> True*2
2
>>> False*2
0
>>> 2+3j*2
(2+6j)
>>> (2+3j)*2
(4+6j)
>>>
>>> 2*2.2
4.4
>>> 2*"sushil"
'sushilsushil'
>>> 2.2*"sushil"
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
2.2*"sushil"
TypeError: can't multiply sequence by non-int of type 'float'
>>> True*2
2
>>> False*2
0
>>> 2+3j*2
(2+6j)
>>> (2+3j)*2
(4+6j)
>>> 20/2
10.0
>>> 20/3
6.666666666666667
>>> 20//3
6
>>> True/True
1.0
>>> True//True
1
>>> "One"/"One"
Traceback (most recent call last):
File "<pyshell#78>", line 1, in <module>
"One"/"One"
TypeError: unsupported operand type(s) for /: 'str' and 'str'
>>> (20+30j)/2
(10+15j)
>>> 2**2
4
>>> 2**5
32
>>> 2**10
1024
>>> 2.2*2
4.4
>>> 2.2**5
51.53632000000002
>>> "sushil"**2
Traceback (most recent call last):
File "<pyshell#85>", line 1, in <module>
"sushil"**2
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'