Base Types انواع
string str "One\nTwo" رشتهای
integer int عددی
float numeric values with floating decimal points. اعشار
boolean bool True False
Container Types انواع کانتینر
list[1,5,9] فهرست
dictionary dict {"key":"value"} واژه نامه
{1:"one",3:"three",2:"two",3.14:"π"}
collection set {"key1","key2"} مجموعه
{1,9,3,0}
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.
Conversions تبدیل
int("15") → 15
list("abc") → ['a','b','c']
dict([(3,"three"),(1,"one")]) → {1:'one',3:'three'}
set(["one","two"]) → {'one','two'}
if bool(x)==True: ⇔ if x:
if bool(x)==False:⇔ if not x:
if age<=18:
state="Kid"
elif age>65:
state="Retired"
else:
state="Active"
Integer Sequences
range([start,] end [,step])
range(5)→ 0 1 2 3 4
range(3,8)→ 3 4 5 6 7
range(len(seq))→ sequence of index of values in seq
for var in sequence:
for x in range(5):
print(x)
Operations on Lists عملیات روی لیستها
lst.append(val) add item at end
lst.extend(seq) add sequence of items at end
lst.insert(idx,val) insert item at index
lst.remove(val) remove first item with value val
lst.pop([idx])→value remove & return item at index idx (default last)
lst.sort() lst.reverse() sort / reverse liste in place
Operations on Sets
Operators:
| → union (vertical bar char)
& → intersection
- ^ → difference/symmetric diff.
< <= > >= → inclusion relations
Operators also exist as methods.
s.update(s2) s.copy()
s.add(key) s.remove(key)
s.discard(key) s.clear()
s.pop()
Initial اولیه
Operator عمل کننده
Checking چک کردن