mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 23:12:24 +03:00
140 lines
3.4 KiB
Plaintext
140 lines
3.4 KiB
Plaintext
-> String:
|
|
|
|
Multi-line string:
|
|
For ex:
|
|
'''
|
|
Hello
|
|
There
|
|
'''
|
|
|
|
->Formatting String:
|
|
|
|
for python 3:
|
|
f'....{<variable-name>}.....{<variable-name}...'
|
|
|
|
'Hello there {} and {}'.format(name1, name2)
|
|
|
|
'Hello there {1} and {0}'.format(name1, name2)
|
|
|
|
{1} : name2
|
|
{0} : name1
|
|
|
|
'Hello there {var1} and {var2}'.format(var1 = value, var2 = value)
|
|
|
|
-> Creating virtual environment:
|
|
|
|
For creating virtual environment:
|
|
|
|
Use:
|
|
python -m venv <environment-folder-name>
|
|
source .venv/bin/activate
|
|
python -m pip install --upgrade pip setuptools wheel
|
|
|
|
-> Referencing the list and copying the list:
|
|
|
|
for example:
|
|
|
|
amazon_cart = [
|
|
'notebooks',
|
|
'sunglasses',
|
|
'toys',
|
|
'grapes'
|
|
]
|
|
|
|
amazon_cart[0] = 'laptop'
|
|
new_cart_2 = amazon_cart[:] # Copying the list
|
|
# As list slicing creates a new
|
|
# list
|
|
|
|
new_cart_1 = amazon_cart # Referencing the list
|
|
|
|
|
|
print(new_cart_1)
|
|
print(new_cart_2)
|
|
print(amazon_cart)
|
|
|
|
-> Sorted function and sort method:
|
|
|
|
Sorted function creates a new list.
|
|
|
|
sort method do the sorting inplace in the list.
|
|
|
|
-> List unpacking:
|
|
|
|
a,b,c = [1,2,3]
|
|
|
|
#a = 1 , b = 2 , c = 3
|
|
|
|
a, b, c, *other, d = [1, 2, 3, 4, 5, 6, 7]
|
|
|
|
#a = 1, b = 2, c = 3, other = [4,5,6], d = 7
|
|
|
|
|
|
-> Dictionary key:
|
|
|
|
Dictionary key need to be immutable i.e. value of key shouldn't be
|
|
modified in any case.
|
|
|
|
The key also has to unique. If the same key which has been defined
|
|
is used again then the key with the new assigned value will override
|
|
the old value and access the value memory with that same key will
|
|
provide the new overrided value instead of previous value.
|
|
Hence, key of Dictionary should be unique
|
|
|
|
tuple can be used as key for Dictionary but not list.
|
|
|
|
-> list and tuple:
|
|
|
|
list are mutable but tuple are immutable
|
|
|
|
-> using keyword and using methods:
|
|
|
|
using keyword to access anything which may not exist
|
|
might result in error, but to avoid that we can use
|
|
methods instead of keyword for accessing any value
|
|
and avoiding error.
|
|
|
|
-> difference_update method in sets:
|
|
|
|
It is used to delete members which is present in set
|
|
which is passed as parameter to this method and the
|
|
object which used this method having same elements
|
|
as present in the set passed as parameter is removed
|
|
and the object set is modified.
|
|
|
|
|
|
-> union and intersection operator of set:
|
|
|
|
union : |
|
|
intersection : &
|
|
|
|
-> Truthy and falsey:
|
|
|
|
some value if their type is converted using
|
|
type() function.
|
|
|
|
Truthy: Conversion to True value from some value is a
|
|
Truthy value.
|
|
|
|
Falsey: Conversion to False value from some value is a
|
|
Falsey value.
|
|
|
|
For ex:
|
|
|
|
bool("hello") -> True (Truthy)
|
|
bool('') -> False (Falsey)
|
|
bool(0) -> False (Falsey)
|
|
bool(199) -> True (Truthy)
|
|
|
|
-> Ternary Operator or conditional expression:
|
|
|
|
<statement_if_true> if <condition> else <statement_if_false>
|
|
|
|
-> Short Circuiting:
|
|
|
|
true and true : Both are evaluated
|
|
false and true : first is evaluated
|
|
false and false : first is evaluated
|
|
true or true : first is evaluated
|
|
false or true : both are evaluated
|
|
false or false : both are evaluated |