Values, types and input/output
مُقدَّم| English | المعنى |
|---|---|
| type/taɪp/ | نوع |
| value/ˈvæljuː/ | قيمة |
| string/strɪŋ/ | سلسلة نصية |
| variable/ˈveərɪəbl/ | متغير |
| f-string/ef strɪŋ/ | f-string |
Why does the till say 66 coffees?
- A café till asks "Coffees?" and the cashier types
6. The program doubles it for a two-for-one offer and prints66, not12. - Nothing is broken. The program got the text "6", and doubling text repeats it. This lesson is about knowing what kind of thing each value is.
coffees = input("Coffees? ") # the user types 6
print(coffees * 2) # prints 66
Every value has a type
- A value 值 is a piece of data. Its type 类型 says what kind it is:
int(whole number),float(decimal number),str(text, a string 字符串) orbool(TrueorFalse). type()tells you the type:type(42)isint,type(4.0)isfloat,type("42")isstr. The quotes make"42"text.
Match each value to its type.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
A decimal point makes a float, quotes make a string, and True/False are the two bool values.
The type decides what an operator does
+adds numbers but joins strings:3 + 4is7, but"3" + "4"is"34". Mixing them,"3" + 4, raises aTypeError: Python refuses to guess.- Division has three forms:
7 / 2is3.5(always afloat),7 // 2is3(whole part) and7 % 2is1(the remainder).
print(3 + 4, "3" + "4", 7 / 2, 7 // 2, 7 % 2)
# 7 34 3.5 3 1
What does print("3" + "4") show?
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Both values are strings, so + joins them. Only numbers would give 7.
What is the value of 17 % 5?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
% gives the remainder: $17 = 3 \times 5 + 2$, so 17 % 5 is 2 و 17 // 5 is 3.
A variable is a name tag on a value
- A variable 变量 is a name bound to a value by
=.price = 3.50makes the namepricepoint at the value3.50. - Assigning again moves the tag to a new value. After
a = 5,b = a,a = 7, the namebstill points at5.

Names point at values
Step through the program. b = a makes b point at the same value 5; moving a to 7 afterwards leaves b at 5.
After a = 5, then b = a, then a = 7, what does print(b) show?
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
b = a points b at the value 5. Moving the name a to 7 later does not move b.
input always gives a string
input()returns whatever the user typed, as astr, even if it looks like a number. Convert it on purpose:int(text)orfloat(text).- A conversion that cannot work raises a
ValueError:int("4.5")fails because4.5is not a whole number. Usefloat("4.5")for decimals.
coffees = int(input("Coffees? ")) # 6 -> the int 6
print(coffees * 3.50) # 21.0
A user types 4.5 at n = int(input("Amount? ")). What happens?
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
int() only accepts text that is a whole number. Use float() for 4.5, or int(float(text)) if you really want to cut off the decimals.
Floats are close, not exact
- Computers store
floatvalues in binary, so many decimals are rounded a tiny amount.0.1 + 0.2prints0.30000000000000004, and0.1 + 0.2 == 0.3isFalse. - Compare floats with a tolerance instead:
abs(x - y) < 1e-9, ormath.isclose(x, y). For money, many programs count whole pence asint.
In Python, 0.1 + 0.2 == 0.3 is True.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
0.1 + 0.2 is stored as 0.30000000000000004, so the test is False. Compare with math.isclose or a small tolerance.
Format at the edge, not in the middle
- An f-string 格式化字符串 puts values into text:
f"Total: £{total:.2f}"shows two decimal places. - Formatting changes only what is printed. The stored value is unchanged, so keep calculating with the number and format once, when you display it.
- The till charges £3.50 a coffee. Read the count and print the total.
coffees = int(input("Coffees? "))turns the typed"6"into theint6.total = coffees * 3.50gives thefloat21.0(aninttimes afloatis afloat).print(f"Total: £{total:.2f}")printsTotal: £21.00. Predict each type before you run the line.
What does print(f"{2/3:.2f}") show?
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
.2f rounds the displayed value to two decimal places. The value 2/3 itself is unchanged.
Marks that slip away
- Forgetting to convert
input.input()is always a string;"6" * 2is"66". - Mixing
strandintwith+."Sold " + 5raises aTypeError. Write"Sold " + str(5), orprint("Sold", 5). - Testing floats with
==. Use a tolerance. =and==.=binds a name;==asks whether two values are equal.
Put the lines of the café till program in order.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Read, convert, calculate, then format. Each line needs the value the line before it made.
You've got it
- Every value has a type:
int,float,str,bool; the type decides what operators do. - A variable is a name tag;
=moves the tag.input()always returns astr, so convert withint()orfloat(). /gives afloat,//the whole part,%the remainder; compare floats with a tolerance; format with f-strings at output.