PythonPython · Chapterبەش 4 · Lectureوانە 4.1
Variables and Data Typesگۆڕاوەکان و جۆرەکانی دراوە
Store values in variables: assignment with =, changing a value, Python's naming rules and PEP 8 style, the basic data types int, float, str, bool and None, type(), dynamic typing, and converting between types with int(), float() and str().
لە هەڵبگرە: بە =، گۆڕینی ، یاساکانی ناونانی Python و شێوازی PEP 8، سەرەکییەکانی int، float، str، bool و None، type()، و بە int()، float() و str().
What you will learnچی فێر دەبیت
- Explain what a variable is and create, read and change variables with assignment.
- Choose valid, readable names that follow Python's rules and PEP 8.
- Recognise the types int, float, str, bool and None and check them with
type(). - Explain dynamic typing and convert values with
int(),float()andstr().
- ڕوونی بکەیتەوە چییە و بە دروست بکەیت، بیانخوێنیتەوە و بیانگۆڕیت.
- ناوی دروست و ڕوون هەڵبژێریت کە یاساکانی Python و PEP 8 پەیڕەو بکەن.
- جۆرەکانی int، float، str، bool و None بناسیتەوە و بە
type()بیانپشکنیت. - ڕوون بکەیتەوە و بە
int()،float()وstr()بگۆڕیت.
What is a variable?گۆڕاو چییە؟
A named place in that holds a ; the value can change while the program runs. In Python you simply write age = 20; in PHP the name starts with $ ($age = 20;).
شوێنێکی ناونراوە لەناو کە هەڵدەگرێت و دەتوانێت لە کاتی کارکردنی بگۆڕێت. لە Pythonـدا تەنها دەنووسیت age = 20؛ لە PHPـدا ناوەکە بە $ دەست پێ دەکات ($age = 20;).
Programs work with information: prices, names, marks. A stores a piece of information under a name, so the program can use it again and change it later. A good name also labels the data: tea_price tells every reader what the number 500 means, which makes the program easier to understand for others and for you.
کار لەگەڵ زانیاری دەکەن: نرخ، ناو، نمرە. پارچەیەک زانیاری لە ژێر ناوێکدا هەڵدەگرێت، بۆ ئەوەی بتوانێت دووبارە بەکاری بهێنێتەوە و دواتر بیگۆڕێت. ناوێکی باش وەک نووسراوێکە لەسەر : tea_price بە هەموو خوێنەرێک دەڵێت ژمارەی 500 چ واتایەکی هەیە، ئەمەش بۆ خەڵکی تر و بۆ خۆت ئاسانتر دەکات.
Compare two versions of the same program. The first repeats the price everywhere; in the second, the price has a name and the number of glasses can change.
دوو وەشانی هەمان بەراورد بکە. یەکەمیان نرخەکە لە هەموو شوێنێک دووبارە دەکاتەوە؛ لە دووەمیاندا نرخەکە ناوێکی هەیە و ژمارەی ئیستیکانەکان دەتوانێت بگۆڕێت.
print("3 glasses of tea cost", 3 * 500, "dinars")
print("5 glasses of tea cost", 5 * 500, "dinars")3 glasses of tea cost 1500 dinars 5 glasses of tea cost 2500 dinars
tea_price = 500
glasses = 3
print(glasses, "glasses of tea cost", glasses * tea_price, "dinars")
glasses = 5
print(glasses, "glasses of tea cost", glasses * tea_price, "dinars")3 glasses of tea cost 1500 dinars 5 glasses of tea cost 2500 dinars
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
If the price changes to 750, you change one line only.
ئەگەر نرخەکە بگۆڕێت بۆ 750، تەنها یەک دێڕ دەگۆڕیت.
- Lineدێڕی 4
The same name now holds a new value.
هەمان ناو ئێستا نوێ هەڵدەگرێت.
Think of the jars in a spice shop at the bazaar. Each jar has a label (cumin, sumac) and holds something. The seller finds what he needs by its label, and can empty a jar and fill it with something new. A variable is a labelled container for one value.
قوتووەکانی دوکانێکی بەهارات لە بازاڕدا بهێنە بەرچاو. هەر قوتوویەک نووسراوێکی پێوەیە (زیرە، سماق) و شتێکی تێدایە. فرۆشیارەکە ئەوەی پێویستیەتی بە نووسراوەکەی دەدۆزێتەوە و دەتوانێت قوتوویەک بەتاڵ بکاتەوە و شتێکی نوێی تێ بکات. دەفرێکی ناونووسکراوە بۆ یەک .
Where the analogy stops workingسنووری ئەم نموونەیە
A jar holds one spice and never two jars share the same spice. In Python two names can point to the same value; the next screen shows the more exact picture.
قوتوویەک یەک بەهارات هەڵدەگرێت و هەرگیز دوو قوتوو یەک بەهارات بەش ناکەن. لە Pythonـدا دوو ناو دەتوانن ئاماژە بە هەمان بکەن؛ شاشەی داهاتوو وێنە وردترەکە پیشان دەدات.
In Kurdish: what a variable is in Python and the basic data types.
بە کوردی: لە چییە و سەرەکی.
Kurdish video: variables and data types.
ڤیدیۆیەکی کوردی: و .
What is a variable?گۆڕاو چییە؟
Where the value livesنرخەکە لە کوێ دەژی
While a program runs, its values are kept in the computer's (RAM). A variable's name is how your program finds its value again. When the program ends, that memory is given back and the values are gone; to keep data for next time, a program must save it in a file.
کاتێک کار دەکات، لە (RAM) هەڵدەگیرێن. ناوی ئەو ڕێگەیەیە کە پێی دەدۆزێتەوە. کاتێک کۆتایی دێت، ئەو بیرگەیە دەدرێتەوە و نامێنن؛ بۆ هەڵگرتنی بۆ جاری داهاتوو، دەبێت لە فایلێکدا پاشەکەوتی بکات.
The box picture is a good start, but Python works a little differently: a variable is a name tag tied to a value. my_age = age does not copy the box; it ties a second tag to the same value. Giving age a new value later moves only the age tag.
وێنەی سندووقەکە سەرەتایەکی باشە، بەڵام Python کەمێک جیاواز کار دەکات: کارتی ناوێکە کە بە بەستراوە. my_age = age سندووقەکە کۆپی ناکات؛ کارتێکی دووەم بە هەمان دەبەستێت. دانانی نوێ بۆ age دواتر تەنها کارتی age دەجووڵێنێت.
age = 21
my_age = age
print(age, my_age)
age = 22
print(age, my_age)21 21 22 21
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
A second name for the same value.
ناوێکی دووەم بۆ هەمان .
- Lineدێڕی 4
Only the tag
agemoves to the new value 22;my_agestill shows 21.تەنها کارتی
ageدەچێت بۆ نوێی 22؛my_ageهێشتا 21 پیشان دەدات.
What is a variable?گۆڕاو چییە؟
Every value is an object with an idهەموو نرخێک ئۆبجێکتێکە و ناسنامەی هەیە
In Python every value — a number, a piece of text, a list — is an : a thing in memory with a type, a value and an identity. The built-in function id() gives that identity as a whole number. It stays the same while the object exists, and no other object has it at the same time. In CPython, the usual Python, it is the object’s memory address.
لە Pythonـدا هەموو (ژمارەیەک، دەقێک، ) : شتێک لە کە جۆرێک، و ناسنامەیەکی هەیە. id() ئەو ناسنامەیە وەک دەداتەوە. تا بوونی هەبێت وەک خۆی دەمێنێتەوە و هیچ تر لە هەمان کاتدا هەمان ژمارەی نییە. لە CPythonـدا، کە Pythonـە ئاساییەکەیە، ئەم ژمارەیە .
age = 21
my_age = age
print(id(age) == id(my_age)) # one object, two name tags
age = age + 1
print(id(age) == id(my_age)) # age now names a new object
print(age, my_age)
x = 5
y = 5
print(id(x) == id(y)) # True in CPython: small ints are sharedTrue False 22 21 True
Creating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ
In Python you create a variable with : the name, an equals sign, and a value. The general form is var_name = value, for example age = 21. There is no separate declaration: the variable comes into existence the first time you assign to it, and Python works out its type from the value.
لە Pythonـدا بە دروست دەکەیت: ناوەکە، نیشانەی یەکسان و . شێوە گشتییەکەی var_name = valueـە، بۆ نموونە age = 21. هیچ ناساندنێکی جیا نییە: یەکەم جار کە بۆ دادەنێیت دروست دەبێت و Python جۆرەکەی لە دەدۆزێتەوە.
age = 21
print(age)
print("age")21 age
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Without quotes,
ageis the variable: Python prints its value.بەبێ ،
age: Python چاپ دەکات.- Lineدێڕی 3
With quotes,
"age"is just text: Python prints the word.بە ،
"age"تەنها دەقە: Python وشەکە چاپ دەکات.
Read = as “gets” or “becomes”, not “equals”. Python first works out the value on the right, then ties the name on the left to it. So the name must be on the left; a value on the left is an error. (Comparing two values uses ==, taught in Making decisions.)
= وەک «وەردەگرێت» یان «دەبێت بە» بخوێنەوە، نەک «یەکسانە». Python سەرەتا لای ڕاست دەژمێرێت، پاشان ناوی لای چەپ پێیەوە دەبەستێت. بۆیە ناوەکە دەبێت لە لای چەپ بێت؛ لە لای چەپ هەڵەیە. (بەراوردکردنی دوو == بەکار دەهێنێت، کە لە بڕیاردان فێر دەکرێت.)
21 = age File "/Users/aram/python/backwards.py", line 1
21 = age
^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?Using a variable before it has been given a value is a runtime error, because the name does not exist yet:
بەکارهێنانی پێش ئەوەی بدرێتێ ، چونکە ناوەکە هێشتا بوونی نییە:
print(score)
score = 10Traceback (most recent call last):
File "/Users/aram/python/too_early.py", line 1, in <module>
print(score)
^^^^^
NameError: name 'score' is not definedChanging a variableگۆڕینی گۆڕاو
A variable can be given a new value at any time. The old value is then forgotten. Because Python runs from top to bottom, the value you see depends on where in the program you look.
لە هەر کاتێکدا دەتوانێت نوێ وەربگرێت. ئەو کاتە لەبیر دەکرێت. چونکە Python لە سەرەوە بۆ خوارەوە جێبەجێ دەکات، ئەو دەیبینیت بەندە بەوەی لە کوێی سەیر دەکەیت.


score = 0
print(score)
score = 5
print(score)
score = 12
print(score)0 5 12
A variable can even get a new value made from its old value. In visitors = visitors + 1, Python first reads the old value (10), adds 1, and then ties visitors to the result (11). In maths x = x + 1 would be impossible; in Python it is one of the most common lines you will write. The short form visitors += 1 is shown in Operators and expressions.
تەنانەت دەتوانێت نوێ وەربگرێت کە لە خۆی دروست کراوە. لە visitors = visitors + 1ـدا، Python سەرەتا (10) دەخوێنێتەوە، 1ـی بۆ زیاد دەکات و پاشان visitors بە ئەنجامەکەوە (11) دەبەستێت. لە بیرکاریدا x = x + 1 مەحاڵ دەبوو؛ لە Pythonـدا یەکێکە لە باوترین ئەو دێڕانەی دەینووسیت. شێوە کورتەکەی visitors += 1 لە کارپێکەرەکان و دەربڕینەکان پیشان دراوە.
visitors = 10
visitors = visitors + 1
print(visitors)11
Rules for namesیاساکانی ناو
A variable's name is an . Python's rules for it are short:
ناوی . یاساکانی Python بۆی کورتن:
- Use letters, digits and the underscore
_only: no spaces, no-, no$. - Do not start with a digit:
mark2is fine,2nd_markis not. - Do not use a , a word that already has a meaning in Python, such as
if,for,classorTrue. - Capital letters count: Python is , so
age,AgeandAGEare three different variables.
- تەنها پیت، ژمارە و
_بەکار بهێنە: نە ، نە-، نە$. - بە ژمارە دەست پێ مەکە:
mark2دروستە،2nd_markنا. - بەکار مەهێنە، واتە وشەیەک کە پێشتر لە Pythonـدا واتای هەیە، وەک
if،for،classیانTrue. - پیتی گەورە حیساب دەکرێت: Python ، بۆیە
age،AgeوAGEسێ جیاوازن.
| Nameناو | OK?دروستە؟ | What Python 3.12 saysPython 3.12 چی دەڵێت |
|---|---|---|
student_name | yesبەڵێ | |
mark2 | yesبەڵێ | |
_counter | yesبەڵێ | |
2nd_place | no: starts with a digitنەخێر: بە ژمارە دەست پێ دەکات | SyntaxError: invalid decimal literal |
my-name | no: - means minusنەخێر: - نیشانەی لێدەرکردنە | SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? |
my name | no: a spaceنەخێر: | SyntaxError: invalid syntax |
price$ | no: $ is not allowedنەخێر: $ ڕێگەپێدراو نییە | SyntaxError: invalid syntax |
class | no: a keywordنەخێر: | SyntaxError: invalid syntax |
Rules for namesیاساکانی ناو
Keywords and capital lettersوشە کلیلەییەکان و پیتی گەورە
Python has 35 keywords: False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield. A few more words, such as match, case and type, are keywords only in special places, so avoid them as names too.
Python 35 هەیە: False، None، True، and، as، assert، async، await، break، class، continue، def، del، elif، else، except، finally، for، from، global، if، import، in، is، lambda، nonlocal، not، or، pass، raise، return، try، while، with، yield. چەند وشەیەکی تریش، وەک match، case و type، تەنها لە شوێنی تایبەتدا ، بۆیە وەک ناو خۆتیان لێ بپارێزە.
student_name = "Hana"
mark2 = 88
_counter = 0
Age = 19
age = 20
AGE = 21
print(student_name, mark2, _counter)
print(Age, age, AGE)Hana 88 0 19 20 21
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-6
Three different variables, because capital letters count.
سێ جیاواز، چونکە پیتی گەورە حیساب دەکرێت.
Choosing good namesهەڵبژاردنی ناوی باش
The rules say what Python accepts; style says what other programmers expect. Python's style guide, PEP 8, asks for:
یاساکان دەڵێن Python چی قبووڵ دەکات؛ شێواز دەڵێت تر چاوەڕێی چی دەکەن. ڕێبەری شێوازی Python، PEP 8، داوای ئەمانە دەکات:
| Styleشێواز | Exampleنموونە | Used forبۆ چی بەکار دێت |
|---|---|---|
total_price | Variables and functions in Python (all lower case, words joined by _) و لە Pythonـدا (هەمووی پیتی بچووک، وشەکان بە _ بەستراون) | |
| ALL CAPITALSهەمووی پیتی گەورە | TEA_PRICE | : values that should not change. Python does not stop you changing them; the capitals are a promise to the reader.: ئەو نابێت بگۆڕێن. Python ڕێگرت لێ ناکات بیانگۆڕیت؛ پیتە گەورەکان بەڵێنێکن بە خوێنەر. |
totalPrice | Java and JavaScript. The lecturer's slides use it (cubeVolume); it works in Python, but PEP 8 prefers snake_case.Java و JavaScript. سلایدەکانی مامۆستا بەکاری دەهێنن (cubeVolume)؛ لە Pythonـدا کار دەکات، بەڵام PEP 8 snake_caseـی پێ باشترە. |
TEA_PRICE = 500
MAX_STUDENTS = 30
print(TEA_PRICE * 3, MAX_STUDENTS)1500 30
- Choose names that say what the value is:
average_mark, notamorx1. - Short names such as
iorxare fine for a counter or a maths formula. - Avoid
l,OandIalone: in many fonts they look like 1 and 0.
- ناوێک هەڵبژێرە کە بڵێت چییە:
average_mark، نەکamیانx1. - ناوی کورتی وەک
iیانxبۆ یان هاوکێشەیەکی بیرکاری باشن. - خۆت لە
l،OوIبە تەنها بپارێزە: لە زۆر فۆنتدا وەک 1 و 0 دەردەکەون.
Data types: what kind of value?جۆرەکانی دراوە: چ جۆرە نرخێک؟
Every value has a , which decides what you can do with it: you can multiply two numbers, but not two words. The basic types of Python are:
هەموو هەیە، کە بڕیار دەدات چی پێ دەکرێت: دەتوانیت دوو ژمارە لێک بدەیت، بەڵام دوو وشە نا. سەرەکییەکانی Python ئەمانەن:
| Typeجۆر | Name in Pythonناو لە Pythonـدا | Examplesنموونە | Useبەکارهێنان |
|---|---|---|---|
int | 21 0 -7 1_000_000 | Counting, ages, marks without fractionsژماردن، تەمەن، نمرەی بێ کەرت | |
float | 1.62 3.0 -0.5 | Measurements, averages, prices with fractionsپێوانە، تێکڕا، نرخی کەرتدار | |
str | "Shilan" '21' "" | Names, messages, any textناو، پەیام، هەر دەقێک | |
bool | True False | Yes/no answers, conditionsوەڵامی بەڵێ/نەخێر، | |
| No value | NoneType | None | “Nothing yet”«هێشتا هیچ» |
The type() tells you the type of any value:
type() جۆری هەر پێت دەڵێت:
print(type(21))
print(type(21.0))
print(type("21"))
print(type(True))
print(type(None))<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'>
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
<class 'int'>means “this value is an int”. In Python every type is a class.<class 'int'>واتە «ئەم intـە». لە Pythonـدا هەموو جۆرێک .
One print() can show variables of different types together; commas keep them apart:
یەک print() دەتوانێت لە جۆری جیاواز پێکەوە پیشان بدات؛ کۆماکان لێکیان جیا دەکەنەوە:
name = "Shilan"
age = 19
height = 1.62
is_student = True
print(name, age, height, is_student)
print(type(name), type(age), type(height), type(is_student))Shilan 19 1.62 True <class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Data types: what kind of value?جۆرەکانی دراوە: چ جۆرە نرخێک؟
Whole numbers and decimal numbersژمارە تەواوەکان و ژمارە کەرتییەکان
An (int) is a whole number: 21, 0, -7. Python's integers have no size limit; they can be as long as your memory allows. You may write _ between digits to make big numbers easier to read: 1_000_000 is one million.
(int) ژمارەیەکی بێ کەرتە: 21، 0، -7. Python سنووری قەبارەیان نییە؛ دەتوانن هێندە درێژ بن کە ڕێگە دەدات. دەتوانیت _ لە نێوان ژمارەکاندا بنووسیت بۆ ئاسانکردنی خوێندنەوەی ژمارە گەورەکان: 1_000_000 یەک ملیۆنە.
A (float) is a number with a decimal point; in code the decimal point is always a dot, never a comma: 1.62, 3.0, -0.5. Dividing with / always gives a float, even 8 / 4 gives 2.0. Floats are stored in binary, so some decimal answers are a tiny bit off; Operators and expressions explains why and what to do.
(float) ژمارەیەکە کە هەیە؛ لە کۆددا هەمیشە خاڵە (.)، هەرگیز کۆما نییە: 1.62، 3.0، -0.5. دابەشکردن بە / هەمیشە دەدات، تەنانەت 8 / 4 دەدات 2.0. بە هەڵدەگیرێن، بۆیە هەندێک وەڵامی دەیی کەمێک لاری هەیە؛ کارپێکەرەکان و دەربڕینەکان ڕوونی دەکاتەوە بۆچی و چی بکەیت.
Python has a third kind of number, complex (complex), written with a j for the imaginary part, such as 3+4j. Engineers and physicists use it; in this course you will rarely need it.
Python جۆری سێیەمی ژمارەشی هەیە، (complex)، کە بەشە خەیاڵییەکەی بە j دەنووسرێت، وەک 3+4j. ئەندازیاران و فیزیکزانان بەکاری دەهێنن؛ لەم کۆرسەدا کەم پێویستت پێی دەبێت.
population = 1_000_000
big = 123456789012345678901234567890
print(population)
print(big * 10)
print(7 / 2)
print(0.1 + 0.2)
z = 3+4j
print(z, type(z))
z = 3+4j
print(z, type(z))
z = 3+4j
print(z, type(z))1000000 1234567890123456789012345678900 3.5 0.30000000000000004 (3+4j) <class 'complex'> (3+4j) <class 'complex'> (3+4j) <class 'complex'>
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
Not exactly 0.3: a normal float surprise, not a bug in Python.
ڕێک 0.3 نییە: سەرسوڕمانێکی ئاسایی ، نەک لە Pythonـدا.
Data types: what kind of value?جۆرەکانی دراوە: چ جۆرە نرخێک؟
Text, True and False, and Noneدەق، True و False لەگەڵ None
A (str) is text in quotes. Be careful: "21" is text made of the characters 2 and 1, not the number 21, and "True" is a word, not a truth value.
(str) دەقێکە لەناو . ئاگادار بە: "21" دەقێکە لە 2 و 1 پێک هاتووە، نەک ژمارەی 21 و "True" وشەیەکە، نەک ڕاستی.
A (bool) has only two values, True and False, always with a capital first letter. You get them from questions such as 5 > 3; they drive the decisions in Making decisions.
(bool) تەنها دوو هەیە، True و False، هەمیشە بە یەکەم پیتی گەورە. لە پرسیارەکانی وەک 5 > 3 وەریان دەگریت؛ بڕیارەکانی بڕیاردان دەجووڵێنن.
None means “no value yet” or “nothing”. It is useful as a starting value. Many languages call this idea null ().
None واتە «هێشتا هیچ نییە» یان «هیچ». وەک سەرەتا بەسوودە. زۆر زمان ئەم بیرۆکەیە ناو دەنێن null ().
is_open = True
print(is_open)
print(5 > 3)
result = None
print(result)
print(type("True"), type(True))True True None <class 'str'> <class 'bool'>
is_open = trueTraceback (most recent call last):
File "/Users/aram/python/lower_true.py", line 1, in <module>
is_open = true
^^^^
NameError: name 'true' is not defined. Did you mean: 'True'?Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
truein small letters is just an unknown name. Python wantsTrue.trueبە پیتی بچووک تەنها ناوێکی نەناسراوە. PythonTrueـی دەوێت.
Data types: what kind of value?جۆرەکانی دراوە: چ جۆرە نرخێک؟
The family of Python typesبنەماڵەی جۆرەکانی Python
The types you have met belong to a bigger family. The numbers are int, float and complex, and bool is a special kind of int. The sequences keep items in order: str, list and tuple. A set keeps items with no order and no repeats, a dict links keys to values, and None stands alone.
ئەو جۆرانەی ناسیوتن سەر بە بنەماڵەیەکی گەورەترن. ژمارەکان int، float و complexـن و bool جۆرێکی تایبەتی intـە. دانەکان بە ڕیز هەڵدەگرن: str، list و tuple. set دانەکان بێ ڕیز و بێ دووبارە هەڵدەگرێت، dict کلیلەکان بە دەبەستێت و None بە تەنها دەوەستێت.
True counts as 1 and False as 0. Python allows sums like the one below, but its documentation advises against relying on it: write int(True) when you really want a number. Each type has its own lesson later: strings, lists, and tuples, sets and dictionaries.
True وەک 1 و False وەک 0 دەژمێردرێن. Python ڕێگە بە کۆکردنەوەی وەک ئەوەی خوارەوە دەدات، بەڵام بەڵگەنامەکانی ئامۆژگاری دەکەن پشتی پێ نەبەستیت: کاتێک بەڕاستی ژمارەت دەوێت int(True) بنووسە. هەر جۆرێک دواتر وانەی خۆی هەیە: زنجیرەنووسەکان، لیستەکان و تاپڵەکان، کۆمەڵەکان و فەرهەنگەکان.
print(True + True)
print(True == 1)
print(type(True + True))
print(int(True), int(False))2 True <class 'int'> 1 0
Dynamic typing: the type belongs to the valueجۆرداریی ناجێگیر: جۆر هی نرخەکەیە
In Python the value has a type, the name does not. A name can point to an int now and to a string later. This is called : types are checked while the program runs, not before.
لە Pythonـدا جۆری هەیە، نەک ناوەکە. ناوێک دەتوانێت ئێستا ئاماژە بە intـێک بکات و دواتر بە . ئەمە پێی دەڵێن : جۆرەکان لە کاتی جێبەجێبوونی دەپشکنرێن، نەک پێشتر.
x = 5
print(x, type(x))
x = "five"
print(x, type(x))
x = 5.0
print(x, type(x))5 <class 'int'> five <class 'str'> 5.0 <class 'float'>
Languages with , such as Java and C#, fix a variable's type when you declare it, and the compiler refuses a value of another type before the program runs. Dynamic typing makes Python quick to write; the price is that some type mistakes appear only when that line runs. Reusing one name for different kinds of values also makes code harder to read, so do it rarely. Programming languages compares the two ideas without code.
زمانەکانی خاوەن ، وەک Java و C#، جۆری لە کاتی ناساندنیدا جێگیر دەکەن و پێش جێبەجێکردنی جۆرێکی تر ڕەت دەکاتەوە. نووسینی Python خێرا دەکات؛ تێچووەکەی ئەوەیە کە هەندێک هەڵەی جۆر تەنها کاتێک دەردەکەون کە ئەو دێڕە جێبەجێ دەبێت. بەکارهێنانی یەک ناو بۆ جۆری جیاوازی خوێندنەوەی کۆد قورستر دەکات، بۆیە کەم ئەمە بکە. وانەی زمانەکانی بەرنامەسازی بەبێ کۆد ئەم دوو بیرۆکەیە بەراورد دەکات.
Converting between typesگۆڕینی جۆر
Sometimes you have a value of the wrong type, for example the text "21" when you need the number 21. use the type names as functions: int(), float() and str().
هەندێک جار لە جۆری هەڵەدا هەیە، بۆ نموونە دەقی "21" لە کاتێکدا ژمارەی 21ـت پێویستە. ناوی جۆرەکان وەک بەکار دەهێنێت: int()، float() و str().
print(int("21") + 1)
print(float("3.5"))
print(str(21) + " years")
print(int(3.9))
print(int(-3.9))
print(float(7))
print(round(3.9))22 3.5 21 years 3 -3 7.0 4
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Text to whole number, then normal arithmetic.
دەق بۆ ، پاشان حیسابی ئاسایی.
- Lineدێڕی 3
A number to text;
+then joins two strings.ژمارە بۆ دەق؛ پاشان
+دوو پێکەوە دەلکێنێت.- Lineدێڕی 4-5
int()cuts off the fraction (towards zero); it does not round.int()کەرتەکە دەبڕێت (بەرەو سفر)؛ ناکات.- Lineدێڕی 7
To round to the nearest whole number, use
round().بۆ ژمارەیەک بۆ نزیکترین ،
round()بەکار بهێنە.
A conversion fails when the value cannot be read as that type. int("3.5") is a ValueError, because "3.5" is not written as a whole number (use float() first). And Python never mixes text and numbers with + by itself:
شکست دەهێنێت کاتێک ناتوانرێت وەک ئەو جۆرە بخوێندرێتەوە. int("3.5") ValueErrorـە، چونکە "3.5" وەک نەنووسراوە (سەرەتا float() بەکار بهێنە). هەروەها Python هەرگیز خۆبەخۆ دەق و ژمارە بە + تێکەڵ ناکات:
print("Age: " + 21)Traceback (most recent call last):
File "/Users/aram/python/mix.py", line 1, in <module>
print("Age: " + 21)
~~~~~~~~^~~~
TypeError: can only concatenate str (not "int") to strage = 21
print("Age: " + str(age))
print("Age:", age)Age: 21 Age: 21
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Convert the number to text first…
سەرەتا ژمارەکە بگۆڕە بۆ دەق…
- Lineدێڕی 3
…or simply give
print()two arguments.…یان بە سادەیی دوو بدە بە
print().
Changing a value's type with int(), float(), str() and bool(), checking it with type(), and implicit conversion.
بە int()، float()، str() و bool()، پشکنینی جۆری بە type() و .
Whole video (7:37): type() (1:24), explicit casts (2:23), implicit casts (6:17).
هەموو ڤیدیۆکە (7:37): type() (1:24)، (2:23) و (6:17).
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happens (Python 3.12)چی ڕوو دەدات (Python 3.12) | Fixچارەسەر |
|---|---|---|
print("age") | Prints the word age, not the valueوشەی age چاپ دەکات، نەک | No quotes around a variable: print(age). لە دەوری دامەنێ: print(age). |
21 = age | SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? | The name goes on the left: age = 21.ناوەکە دەچێتە لای چەپ: age = 21. |
| Using a name before assigning itبەکارهێنانی ناوێک پێش دانانی | NameError: name 'score' is not defined | Assign first, then use.سەرەتا دابنێ، پاشان بەکاری بهێنە. |
Age in one place, age in anotherAge لە شوێنێک، age لە شوێنێکی تر | Two different variables, or a NameErrorدوو جیاواز، یان NameError | Use exactly the same spelling.ڕێک هەمان نووسین بەکار بهێنە. |
is_open = true | NameError: name 'true' is not defined. Did you mean: 'True'? | Write True and False with capitals.True و False بە پیتی گەورە بنووسە. |
print("Age: " + 21) | TypeError: can only concatenate str (not "int") to str | str(21), or use commas in print().str(21)، یان لە print()ـدا کۆما بەکار بهێنە. |
int("3.5") | ValueError: invalid literal for int() with base 10: '3.5' | Use float("3.5"), then int() if you need a whole number.float("3.5") بەکار بهێنە، پاشان int() ئەگەر پێویستە. |
print = 5, then print("Hello")print = 5، پاشان print("Hello") | TypeError: 'int' object is not callable | Never use names of built-in functions (print, input, type, str, list, sum) for your variables.هەرگیز ناوی (print، input، type، str، list، sum) بۆ بەکار مەهێنە. |
Practiceڕاهێنان
Create four variables for a student called Hana: her name, her age (20), her average mark (87.5) and whether she has passed (yes). Print all four on one line, and then print their types.
چوار بۆ قوتابییەک بە ناوی هانا دروست بکە: ناوەکەی، تەمەنی (20)، تێکڕای نمرەکەی (87.5) و ئایا دەرچووە (بەڵێ). هەر چواریان لەسەر یەک دێڕ چاپ بکە و پاشان جۆرەکانیان چاپ بکە.
Show answerوەڵامەکە پیشان بدە
name = "Hana"
age = 20
average_mark = 87.5
has_passed = True
print(name, age, average_mark, has_passed)
print(type(name), type(age), type(average_mark), type(has_passed))Hana 20 87.5 True <class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Which of these can be variable names? total_marks, 2nd, _temp, class, firstName, my-age, Class, price$
کام لەمانە دەتوانن ببنە ناوی ؟ total_marks، 2nd، _temp، class، firstName، my-age، Class، price$
Show answerوەڵامەکە پیشان بدە
- Valid:
total_marks,_temp,firstName(allowed, but PEP 8 prefersfirst_name),Class(allowed because Python is case-sensitive, but confusing). - Not valid:
2nd(starts with a digit),class(keyword),my-age(-is minus),price$($is not allowed).
- دروستن:
total_marks،_temp،firstName(ڕێگەپێدراوە، بەڵام PEP 8first_nameـی پێ باشترە)،Class(ڕێگەپێدراوە چونکە Python ، بەڵام سەرلێشێوێنەرە). - نادروستن:
2nd(بە ژمارە دەست پێ دەکات)،class()،my-age(-نیشانەی لێدەرکردنە)،price$($ڕێگەپێدراو نییە).
cup_a holds "tea" and cup_b holds "milk". Swap them so that print(cup_a, cup_b) shows milk tea. Why does cup_a = cup_b followed by cup_b = cup_a not work?
cup_a "tea" هەڵدەگرێت و cup_b "milk". بیانگۆڕەوە بۆ ئەوەی print(cup_a, cup_b) پیشان بدات milk tea. بۆچی cup_a = cup_b و دوای ئەو cup_b = cup_a کار ناکات؟
Hintئاماژە
Think of two full cups and an empty third cup.
دوو پەرداخی پڕ و پەرداخێکی سێیەمی بەتاڵ بهێنە بەرچاو.
Show answerوەڵامەکە پیشان بدە
cup_a = "tea"
cup_b = "milk"
cup_a = cup_b
cup_b = cup_a
print(cup_a, cup_b)milk milk
After cup_a = cup_b both names point to "milk"; the tea is gone. Use a third, empty cup:
دوای cup_a = cup_b هەردوو ناو ئاماژە بە "milk" دەکەن؛ چا نەما. پەرداخێکی سێیەمی بەتاڵ بەکار بهێنە:
cup_a = "tea"
cup_b = "milk"
temp = cup_a
cup_a = cup_b
cup_b = temp
print(cup_a, cup_b)milk tea
Python also has a one-line swap, cup_a, cup_b = cup_b, cup_a, which gives the same output; Tuples, sets and dictionaries explains how it works.
Python گۆڕینەوەیەکی یەک دێڕیشی هەیە، cup_a, cup_b = cup_b, cup_a، کە هەمان دەدات؛ تاپڵەکان، کۆمەڵەکان و فەرهەنگەکان ڕوونی دەکاتەوە چۆن کار دەکات.
What does each line print? type(3), type(3.0), type("3"), type(3 > 2), type(int("3")) (each inside print()).
هەر دێڕێک چی چاپ دەکات؟ type(3)، type(3.0)، type("3")، type(3 > 2)، type(int("3")) (هەر یەکەیان لەناو print()ـدا).
Show answerوەڵامەکە پیشان بدە
print(type(3))
print(type(3.0))
print(type("3"))
print(type(3 > 2))
print(type(int("3")))<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'int'>
This program stops with a TypeError. Fix it in two different ways.
ئەم بەرنامەیە بە TypeError دەوەستێت. بە دوو ڕێگای جیاواز چاکی بکە.
age = 19
print("You are " + age + " years old")Traceback (most recent call last):
File "/Users/aram/python/message.py", line 2, in <module>
print("You are " + age + " years old")
~~~~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to strShow answerوەڵامەکە پیشان بدە
age = 19
print("You are " + str(age) + " years old")
print("You are", age, "years old")You are 19 years old You are 19 years old
Practiceڕاهێنان
More practice: names and typesڕاهێنانی زیاتر: ناو و جۆر
Many official forms here ask for three names: your own name, your father’s name and your grandfather’s name. Store the three names in first, father and grandfather, join them with spaces into one variable full_name, and print it.
زۆر فۆرمی فەرمی لێرە داوای سێ ناو دەکەن: ناوی خۆت، ناوی باوکت و ناوی باپیرت. سێ ناوەکە لە first، father و grandfatherـدا هەڵبگرە، بە پێکەوەیان ببەستە بۆ یەک بە ناوی full_name و چاپی بکە.
Hintئاماژە
+ joins strings, but adds no spaces by itself: add " " between the names.
+ پێکەوە دەبەستێت، بەڵام خۆی زیاد ناکات: " " لە نێوان ناوەکاندا دابنێ.
Show answerوەڵامەکە پیشان بدە
first = "Hana"
father = "Karwan"
grandfather = "Soran"
full_name = first + " " + father + " " + grandfather
print(full_name)Hana Karwan Soran
print(first, father, grandfather) would show the same line, but only full_name keeps the joined text, ready to be used again later in the program.
print(first, father, grandfather) هەمان دێڕ پیشان دەدات، بەڵام تەنها full_name دەقە بەیەکەوەبەستراوەکە هەڵدەگرێت و ئامادەیە دواتر لە دووبارە بەکار بێتەوە.
Which type would you use for each value, and why? (a) the number of months in a year, (b) whether a student lives in the dormitory, (c) a mobile number, (d) pocket money in dinars, (e) the volume of a ball, (f) a student’s name, (g) a home address.
بۆ هەر یەکێک لەم کام جۆر بەکار دەهێنیت و بۆچی؟ (أ) ژمارەی مانگەکانی ساڵ، (ب) ئایا خوێندکارێک لە بەشی ناوخۆیی دەژی، (ج) ژمارەی مۆبایل، (د) پارەی گیرفان بە دینار، (هـ) قەبارەی تۆپێک، (و) ناوی خوێندکارێک، (ز) ناونیشانی ماڵ.
Show answerوەڵامەکە پیشان بدە
| Value | Typeجۆر | Whyبۆچی |
|---|---|---|
| (a) months in a year(أ) مانگەکانی ساڵ | int | a whole number: 12: 12 |
| (b) lives in the dormitory?(ب) لە بەشی ناوخۆیی دەژی؟ | bool | yes or no: True or Falseبەڵێ یان نەخێر: True یان False |
| (c) mobile number(ج) ژمارەی مۆبایل | str | it starts with 0 or +964 and you never do sums with itبە 0 یان +964 دەست پێ دەکات و هەرگیز حیسابی پێ ناکەیت |
| (d) pocket money in dinars(د) پارەی گیرفان بە دینار | int | dinar amounts are whole numbersبڕی دینار |
| (e) volume of a ball(هـ) قەبارەی تۆپ | float | the result of has decimalsئەنجامی تێدایە |
| (f) name, (g) address(و) ناو، (ز) ناونیشان | str | textدەق |
A phone number stored as an int loses its first 0:
ژمارەی تەلەفۆن ئەگەر وەک int هەڵبگیرێت سفری یەکەمی ون دەبێت:
phone = "07500000000"
print(phone)
print(int(phone))07500000000 7500000000
Summaryپوختە
- A variable stores a value under a descriptive name so the program can use and change it; in Python it is a name tag on a value in memory.
name = valueis assignment: the right side is worked out first. No declaration is needed; using a name before it has a value is a NameError.- Names: letters, digits and
_, not starting with a digit, not a keyword, case-sensitive. Style: snake_case, CONSTANTS in capitals. - Basic types: int, float, str, bool, and None;
type()shows the type. - Every value is an object with a type and an identity (
id()). Python’s numbers areint,floatandcomplex;boolis a kind ofint. - Python uses dynamic typing: the type belongs to the value, and a name can later point to another type.
int(),float()andstr()convert values;int()cuts off fractions; text and numbers cannot be joined with+without converting.
- لە ژێر ناوێکی ڕوونکەرەوەدا هەڵدەگرێت بۆ ئەوەی بتوانێت بەکاری بهێنێت و بیگۆڕێت؛ لە Pythonـدا کارتی ناوێکە لەسەر لە .
name = value: سەرەتا لای ڕاست دەژمێردرێت. هیچ ناساندنێک پێویست نییە؛ بەکارهێنانی ناوێک پێش ئەوەی هەبێت NameErrorـە.- ناو: پیت، ژمارە و
_، بە ژمارە دەست پێ ناکات، نییە و . شێواز: snake_case، بە پیتی گەورە. - جۆرە سەرەکییەکان: int، float، str، bool و None؛
type()جۆرەکە پیشان دەدات. - هەموو و جۆر و ناسنامەی (
id()) هەیە. ژمارەکانی Python بریتین لەint،floatوcomplex؛boolجۆرێکە لەint. - Python بەکار دەهێنێت: جۆر هی و ناوێک دەتوانێت دواتر ئاماژە بە جۆرێکی تر بکات.
int()،float()وstr()دەگۆڕن؛int()کەرت دەبڕێت؛ دەق و ژمارە بەبێ گۆڕین بە+پێکەوە نالکێنرێن.
What is the difference between print(age) and print("age")?جیاوازیی نێوان print(age) و print("age") چییە؟
The first prints the variable's value; the second prints the word age.
یەکەمیان چاپ دەکات؛ دووەمیان وشەی age.
Why is x = x + 1 correct in Python?بۆچی x = x + 1 لە Pythonـدا دروستە؟
= is assignment, not equality: Python computes x + 1 with the old value and then ties x to the result.
= ، نەک یەکسانی: Python بە x + 1 دەژمێرێت و پاشان x بە ئەنجامەکەوە دەبەستێت.
What does int(9.99) give, and why?int(9.99) چی دەدات و بۆچی؟
9: int() cuts off the fraction. round(9.99) would give 10.
9: int() کەرتەکە دەبڕێت. round(9.99) 10 دەدات.
What does dynamic typing mean?جۆرداریی ناجێگیر واتای چییە؟
Values have types but names do not; a name can point to an int and later to a string, and types are checked while the program runs.
جۆریان هەیە بەڵام ناوەکان نا؛ ناوێک دەتوانێت ئاماژە بە intـێک بکات و دواتر بە و جۆرەکان لە کاتی جێبەجێکردندا دەپشکنرێن.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
What a variable is, how to show it with print() and f-strings, and the four basic types: int, float, str and bool.
چییە، چۆن بە print() و پیشانی دەدەیت و چوار سەرەکی: int، float، str و bool.
Whole video (13:30): variables (0:00), f-strings (3:20), the 4 basic data types (4:04), tips (11:26).
هەموو ڤیدیۆکە (13:30): (0:00)، (3:20)، چوار سەرەکی (4:04) و ئامۆژگاری (11:26).
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Operators and Expressionsکارپێکەرەکان و دەربڕینەکان
Related lecturesوانە پەیوەندیدارەکان
- Operators and Expressionsکارپێکەرەکان و دەربڕینەکان
- Input and Outputنێوخستە و دەرخستە
- Lists, References, Copies and Functionsلیستەکان، ئاماژەکان، کۆپی و کردارەکان
- Python Next to JavaPython لە تەنیشت Javaـەوە
- Tuples, Sets and Dictionariesتاپڵەکان، کۆمەڵەکان و فەرهەنگەکان
- Working with Stringsکارکردن لەگەڵ زنجیرەنووسەکان
- Lists: Creating and Indexingلیستەکان: دروستکردن و ئیندێکسەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python tutorial 3.1.1: Numbers (assignment with =, NameError, int and float)
- Python language reference 2.3: Identifiers and keywords
- Python documentation: Built-in Types (int, float, bool, truth values)
- Python documentation: Built-in Functions (int, float, str, type, round)
- PEP 8: Style Guide for Python Code (naming conventions)
- Harvard CS50P, lecture 0 notes (variables, int, float)
- Python FAQ: General Python FAQ (dynamic typing)
- Liang, Introduction to Programming Using Python: table of contents (chapter 2 Elementary Programming)
- Python documentation: built-in functions — id() (identity; CPython: memory address)
- Python 3.12 C API: integer objects (CPython keeps the integers −5 to 256 as shared objects)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.