177
177
178
178
## Logical Operator
179
179
* ` and ` and ` or ` are a logical operator.
180
- * ` x ` and ` y ` returns ` False ` if ` x ` is ` False ` else it returns evaluation of ` y ` .
180
+ * ` x and y ` returns ` False ` if ` x ` is ` False ` else it returns evaluation of ` y ` .
181
181
If ` x ` is ` True ` , it returns ` True ` .
182
182
```
183
183
>>> 1 and 4
196
196
* ` x operator = expression ` is the syntax for shorthand operators.
197
197
```
198
198
>>> a = 15
199
- >>> b += 30
199
+ >>> b = 2
200
+ >>> b += 30 # Same as b = b + 30
200
201
>>> b
201
202
32
202
203
```
@@ -241,9 +242,9 @@ ValueError: invalid literal for int() with base 10: 'hello'
241
242
'1.5'
242
243
```
243
244
244
- ## String Cancatanation
245
+ ## String Concatenation
245
246
* Same type of string literals can be joined
246
- * if the variable is not same type then convert it by using * + *
247
+ * if the variable is not same type then convert it by using ` + `
247
248
```
248
249
>>> a = 'hello'
249
250
>>> b = 'COEP'
@@ -258,8 +259,8 @@ TypeError: cannot concatenate 'str' and 'int' objects
258
259
'hello10'
259
260
```
260
261
261
- ## Writting a program in file
262
- * All programs are written with ` <filename> .py` .
262
+ ## Writing a program in file
263
+ * All Python programs are written with ` .py ` extension .
263
264
* Create a file named ` first_program.py ` and write the following code.
264
265
```
265
266
#!/usr/bin/env python3
@@ -275,7 +276,7 @@ $ chomd +x first_program.py
275
276
```
276
277
$./first_program.py
277
278
```
278
- * On the first line you can ` #! ` , what we call it [ sha-bang] (https://en.wikipedia.org/wiki/Shebang_(Unix) .
279
+ * On the first line you can ` #! ` , what we call it [ sha-bang] ( https://en.wikipedia.org/wiki/Shebang_\ ( Unix\) ) .
279
280
The ` sha-bang ` indicates that the Python interpreter should run this code.
280
281
281
282
## Decision Making using if, else
@@ -294,7 +295,7 @@ elif expression:
294
295
else:
295
296
do something else
296
297
```
297
- It works only when expression is true otherwise it goes to next line.
298
+ It works only when expression is ` True ` otherwise it goes to next line.
298
299
```
299
300
>>> a = 100
300
301
>>> if a % 2 == 0:
@@ -307,8 +308,10 @@ Even Number
307
308
```
308
309
### Truth value testing
309
310
```
310
- if a:
311
- pass
311
+ >>> a = True
312
+ >>> if a:
313
+ ... print("a is True")
314
+ a is True
312
315
```
313
316
314
317
## Functions
@@ -321,28 +324,28 @@ def function_name(function_arguments):
321
324
do some stuff
322
325
return something
323
326
```
324
- * Here function_arguments are not necessary .
327
+ * Here function_arguments are not mandatory .
325
328
* function arguments can be passed in any fashion
326
- * If return is not define, it will not return anything ( None) .
329
+ * If return is not define, it will ` None ` .
327
330
```
328
331
>>> def sum(a, b):
329
332
... print(a + b)
330
333
...
331
334
>>> sum(2,3)
332
335
5
333
- >>> sum(a=2,b=3)
336
+ >>> sum(a=2, b=3)
334
337
5
335
- >>> x = sum(1, 2)
338
+ >>> return_value = sum(1, 2)
336
339
3
337
- >>> print(x )
340
+ >>> print(return_value )
338
341
None
339
342
>>> def sum(a, b):
340
343
... return a + b
341
344
...
342
- >>> x = sum(1, 2)
343
- >>> x
345
+ >>> result = sum(1, 2)
346
+ >>> result
344
347
3
345
- >>> print(x )
348
+ >>> print(result )
346
349
3
347
350
```
348
351
358
361
```
359
362
360
363
### For loop
361
- In Python loop works over a ` Iterator ` object.
364
+ In Python loop works over a ` Iterator ` object,
362
365
might be a ` list ` , ` string ` , ` tuples ` , ` dictionary ` , ` set ` etc.
363
366
```
364
367
for i in sequence:
0 commit comments