Python Flashcards
value = 42
print(f’{value=}’)
value=42
*=
+=
-=
/=
%=
**=
meghdaro hesab mikona mirize dakhele khode var
def func(a, b, /):
ghable / hame bayad positional bashan va nemitoona keyword begiran. ba dashtane meghdare default tanaghozi nadare
old version of def func(x: int | str):
from typing import Union
def func(x: Union[int , str]):
s = ‘TestString’
remove Test from beginnign
s = s.removeprefix(‘Testr’)
s = tavajoh kon
age aval ya akharessh nadashte bashe chi mishe?
baraye removeprefix va removesuffix
hichi, error nemide. kalame khodesh mimune
s = ‘TestString’
remove ing from last part of word
s = s.removesuffix(‘ing’)
s = tavajoh kon
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
nums1 = [nums2[i - n] if not j else j for i, j in enumerate(nums1)]
what is nums1?
nums1 will not changed
how to change list in place inside of a function/method?
change elements
nums1[m:]=nums2
nums1[m:]=nums2[:n]
these are changing elements?
Yes. both of them
k%=10 ?
1- k=k%10
2- k = 10% k
1- k=k%10
roman_dyn = {“IV”: 4, “IX”: 9, “XL”: 40, “XC”: 90, “CD”: 400, “CM”: 900}
for i, j in roman_dyn.values():
or
for I, j in roman_dyn.items(): ?
for I, j in roman_dyn.items():
s = s.replace(i,””)
or s.replace(i,””)
s = s.replace(i,””)
a=” fly me to the moon “
print(a.strip())
space ro az 2 taraf pak mikone
a=” fly me to the moon “
print(a.split())
[‘fly’, ‘me’, ‘to’, ‘the’, ‘moon’]
anagram_map = defaultdict(dict)
anagram_map[1]
print(anagram_map)
{1:{}}
remove repatitive elemnts out of a list
nums=list(dict.fromkeys(nums))
list(set(nums))
use hash map for get unique keys of a list
nums = [‘a’,’b’,’a’,’b’,’b’,’b’,’b’]
result={}
for i in nums:
result[i]=True
all, any
hame azaye yek list ro true age bashe mide true baraye all va any ham mesle or amal mikone
count
tedad ro mishmare. dakhele list va string
all any ruye dict
rouye keys amal mikone !
all any ruye dict values?
all(dict.values())