پرش به مطلب اصلی

Basics

موارد ابتدایی پایتون و شروع توسعه

Jupyter​

Start Jupyter​

jupyter lab

Functions​

type​

print(type("salam")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(123)) # <class 'int'>
print(type([1,2,3])) # <class 'list'>
print(type({"id":12})) # <class 'dic'>

int​

int("0xaa", 16) # 170
int("0100", 2) # 4
int(17.2) # 17

range​

range(1, 5) # 1, 2, 3, 4
range(4) # 0, 1, 2, 3

chr - ord​

ord بلعکس chr است

chr(98) # b

ord('b') # 98

math​

round(3.23) # 3
round(3.232141431, 2) # 3.23

complex('5+2j') # عدد مختلط

abs("5+2j") # 5.38 (قدر مطلق)

divmod(9, 2) # (4, 1) -> (quotient, reminder) -> باقی مانده و خارچ قسمت

pow(2, 4) # 16
pow(3, 3, 5) # 3^3 mod 5 -> 2

input - output​

input​

x = input("please enter a number \t")

output​

print(10, 20, 30, end=" ")
print("salam") # 10 20 30 salam

print(10, 20, 30, sep="@") # 10@20@30

x,y = 10, 20
print("x is %d" % x) # x is 10
print(f"x is {x}") # x is 10 <- better way
print(f"x + y = {x} + {y} = {x+y}") # x + y = 10 + 20 = 30

در این روش هنگام گرفتن ورودی متن داده شده را چاپ میکند

هر ورودی که از کاربر میگیریم به صورت پیش فرض string است

strings​

len("salam") # 5

s = 'salam %s %d sale, %s' % ("amirhossein", 12, "chetori?") # salam amirhossein 12 sale, chetori?

s = "hello"
s.capitalize() # Hello
s.upper() # HELLO
s.replace("h", "mi") # miello
s.strip() # strip = trime -> " salam".strip() -> salam

d = "aaaamiraaa"
d.strip("a") # removes all a words in begining and ending -> mir

Data Structure​

Lists​

num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

num[-1] # 9
num[-3] # 7
num[1:3] # [1, 2]
num[:9:3] # [0, 3, 6] -> start:stop:step


len(num) # 10
min(num) # 0
max(num) # 9
[1, 2, 3, 4] + [1, 4, 5] # [1, 2, 3, 4, 1, 4, 5]


10 in num # False
3 in num # True

num.append(11) # [0, 1, ..., 9, 11]

num.count(1) # 1
num.count(18) # 0

lst = [1, 8, 1, 4, 12, 9]
lst.index(1) # 0
lst.count(1) # 2

lst.insert(4, "salam") # [1, 8, 1, 4, "salam", 12, 9]

lst.pop() # [1, 8, 1, 4, 12]

del lst[1] # [1, 1, 4, 12, 9]

lst.reverse() # [9, 12, 4, 1, 8, 1]

lst.sort() # [1, 1, 4, 8, 9, 12]
lst.sort(reverse=True) # [12, 9, 8, 4, 1, 1]
lst.sort(key=len, reverse=true) # sorts with length of item

Tuple​

همان زوج مرتب ریاضی است با این تفاوت که قابلیت تغییر یافتن ندارد !

ممکن است تاپل بیشتر از ۲ عضو داشته باشد (تاپل ۶ تایی مثلا)

tup = (1, 2, 3)
tup2 = tuple([1, 2, 3]) # tuple برای تبدیل استفاده میشه

tup3 = tuple("hello") # (h, e, l, l, o)
print(tup3[2]) # l

tup3.count("l") # 2
tup3.index("l") # 2

tup3[1] = "s" # Error ! -> Can't change tuple

Set​

همان مجموعه‌ی ریاضی است و خاصیت آن این است که مرتب شده و بدون تکرار است مقادیر آن

st1 = set([1,2,1,3,4,6,5]) # {1, 2, 3, 4, 5, 6}
st2 = set([1,4,7,10])

st1.add(0) # {0, 1, 2, 3, 4, 5, 6}
st1.remove(1) # {2, 3, 4, 5, 6}
st1.clear() # {}

st1.union(st2) # st1 ∪ st2 -> {1, 2, 3, 4, 5, 6, 7, 10}
st1.intersection(st2) # st1 ∩ st2 -> {1, 4}
st1.diffrence(st2) # st1 - st2 -> {2, 3, 5, 6}
st2.diffrence(st1) # st2 - st1 -> {7, 10}
st1.symmetric_diffrence # (st1 ∪ st2) - (st1 ∩ st2) -> {2, 3, 5, 6, 7, 10}
st1.issubset(st2) # st1 ⊂ st2 -> False
st1.isdisjoint(st2)
st1.issuperset(st2) # st1>=st2

Dictionary​

همان آبجکت در زبان js هست

dict = {age: 12, name: "amirhossein", scores: [12,12,14,19,20], address: { city: "tehran", "postal": 132 }}

dict["age"] # 12
dict.keys() # ["age", "name", "scores", "address"]
dict.values() # [12, amirhossein, [12,12,14,19,20], { city: "tehran", "postal": 132 }]
dict.items() # [("age", 12), ("name", "amirhossein"), ("scores", [12,12,14,19,20]), ....] -> returns tuple

for score in dict.scores :
print(score, end=" ") # 12 12 14 19 20

for harchi in dict:
print(dict[harchi], end=" ") # prints values -> 12 amirhossein [12,12,14,19,20] { city: "tehran", "postal": 132 }