mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 23:12:24 +03:00
Basic is complete
This commit is contained in:
159
Basic/wiki.txt
159
Basic/wiki.txt
@@ -137,4 +137,161 @@
|
|||||||
false and false : first is evaluated
|
false and false : first is evaluated
|
||||||
true or true : first is evaluated
|
true or true : first is evaluated
|
||||||
false or true : both are evaluated
|
false or true : both are evaluated
|
||||||
false or false : both are evaluated
|
false or false : both are evaluated
|
||||||
|
|
||||||
|
-> Multiple logical operator in one statement:
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
a < b > c < d
|
||||||
|
|
||||||
|
-> is v/s == :
|
||||||
|
|
||||||
|
== : It checks if the value on both side are same. Both
|
||||||
|
side should of same value and type to equate to true.
|
||||||
|
|
||||||
|
is : It check if the location where the value is stored
|
||||||
|
same for both side.
|
||||||
|
|
||||||
|
-> Type conversion:
|
||||||
|
|
||||||
|
Some operands cannot be compared because of their incompatible type
|
||||||
|
and even type conversion will also not be allowed as it would make
|
||||||
|
sence.
|
||||||
|
|
||||||
|
Note: keep in mind what could be compared and what couldn't be.
|
||||||
|
|
||||||
|
-> What is a iterable?
|
||||||
|
|
||||||
|
for item in 'Zero to mastery'
|
||||||
|
^
|
||||||
|
|
|
||||||
|
This is a iterable.
|
||||||
|
Anything which can be looped through is a iterable.
|
||||||
|
|
||||||
|
Iterable is an object or a collection which can be iterable over.
|
||||||
|
|
||||||
|
#iterable - list, dictionary, tuple, set, string.
|
||||||
|
|
||||||
|
-> To iterate in reverse using for loop:
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
for x in range(10, 0 , -1):
|
||||||
|
|
||||||
|
-> while loop with else:
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
while <condition>:
|
||||||
|
....Statement....
|
||||||
|
else:
|
||||||
|
....Statement....
|
||||||
|
|
||||||
|
This else part is executed only when the while condition fails.
|
||||||
|
|
||||||
|
-> pass keyword:
|
||||||
|
|
||||||
|
pass keyword doesn't do anything. It's just a dummy statement.
|
||||||
|
For example, when we don't what to do inside for loop but still
|
||||||
|
want to run the program without throwing error we can use "pass"
|
||||||
|
keyword to do nothing indicating some other might replace it later.
|
||||||
|
|
||||||
|
-> nested functions:
|
||||||
|
|
||||||
|
User-defined function in python can have function defined inside
|
||||||
|
another function and its scope is limited that function itself.
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
def function1():
|
||||||
|
def function2():
|
||||||
|
|
||||||
|
-> Docstring:
|
||||||
|
|
||||||
|
This can be used inside function to write it as comment
|
||||||
|
and mention what the function can do. It's just like a comment.
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
def function():
|
||||||
|
'''
|
||||||
|
What function can do.
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
To print the Docstring. use help(<function-name>) or <fucntion-name>.__doc__
|
||||||
|
which is a dunder function.
|
||||||
|
|
||||||
|
-> *args && **kwargs:
|
||||||
|
|
||||||
|
*args : It's just like variable argument in C/C++ (var_arg).
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
def fun(*args):
|
||||||
|
print(args)
|
||||||
|
|
||||||
|
-> args : It stores the number of argument as a tuple.
|
||||||
|
|
||||||
|
**kwargs : It stores argument with keywords. It stores it like a
|
||||||
|
dictionary.
|
||||||
|
|
||||||
|
#Rule: params, *args, default parameters, **kwargs
|
||||||
|
name , *args, i = 'hi' , **kwargs
|
||||||
|
|
||||||
|
-> Walrus operator (:=):
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
if n := len(a) > 0:
|
||||||
|
...do something...
|
||||||
|
|
||||||
|
"n = len(a)" is not allowed in python.
|
||||||
|
So, to achieve this we use := operator.
|
||||||
|
|
||||||
|
-> Scope in Python:
|
||||||
|
|
||||||
|
Different function have different scope.
|
||||||
|
If and other things use global scope.
|
||||||
|
|
||||||
|
|
||||||
|
Order to check the scope of variable:
|
||||||
|
|
||||||
|
#1 - start with local.
|
||||||
|
#2 - Parent local?
|
||||||
|
#3 - global
|
||||||
|
#4 - built in python functions.
|
||||||
|
|
||||||
|
-> global keyword:
|
||||||
|
|
||||||
|
Use this to access global variable in a function scope.
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
def count():
|
||||||
|
global total
|
||||||
|
total += 1
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
-> nonlocal keyword:
|
||||||
|
|
||||||
|
This is the very latest feature of feature
|
||||||
|
|
||||||
|
To access the non-local variable which is not present
|
||||||
|
in the local scope of the function but is present in the
|
||||||
|
scope of parent function, in case of nested function.
|
||||||
|
|
||||||
|
for ex:
|
||||||
|
|
||||||
|
def outer():
|
||||||
|
x = "local"
|
||||||
|
def inner():
|
||||||
|
nonlocal x
|
||||||
|
x = "nonlocal"
|
||||||
|
print("inner:", x)
|
||||||
|
|
||||||
|
inner()
|
||||||
|
print("outer:", x)
|
||||||
|
|
||||||
|
outer()
|
||||||
Reference in New Issue
Block a user