Types and neat output

Part of the free LogicWiz Python course, Module 1: Start coding. The examples and practice run in the app.

Canteen Counter printed Total: 52.49999999999999. Shankar bhaiya wants Total: Rs 52.50. One f-string fixes it.

You'll learn to

  • Know int, float, str, bool and None, and check them with type()
  • Print neat money with f-strings and :.2f
  • Use round() and know how it differs from :.2f
  • Spot valid names, keywords and the other tokens

Learn

Every value has a type

print(type(42))
print(type(4.5))
print(type("hi"))
print(type(True))
print(type(None))
print(type("42"))

type() tells you what kind of value you have. "42" is a str, not an int, because of the quotes.

The five you meet first

Type Example Used for
int 42, -7 counts, marks
float 4.5, 2.0 money, averages
str "chai" text
bool True, False yes/no answers
None None nothing yet

True, False and None start with a capital letter. Python is dynamically typed: the value has the type, not the name. The same name can point to an int now and a string later.

f-strings: values inside text

name = "Meher"
total = 52.5
print(f"Hi {name}, your bill is Rs {total}")
print(f"Total: Rs {total:.2f}")
print(f"One third is {1 / 3:.3f}")
print(f"{'Samosa':<10}{15:>5}")
print(f"{'Chai':<10}{10:>5}")

Put f before the quote, and anything in { } is worked out and dropped in. :.2f means "2 digits after the point". :<10 pads to 10 characters on the left side, :>5 lines numbers up on the right: perfect for receipts.

round()

print(round(52.4567, 2))
print(round(52.5, 2))
print(round(7.6))
print(round(2.5), round(3.5))

round(x, 2) gives a number with at most 2 decimals, so 52.5 stays 52.5. With no second argument you get an int. A value exactly halfway goes to the even neighbour: 2.5 → 2, 3.5 → 4.

round() changes the value. :.2f only changes how it looks. For money on screen use f"{amount:.2f}": it always shows two digits, so ₹52.5 appears as 52.50.

Rules for names (identifiers)

A name can use letters, digits and _.

  • It cannot start with a digit.
  • No spaces and no symbols like - or @.
  • Case matters: Total and total are different names.
  • It cannot be a keyword: words Python keeps for itself, such as if, for, class, True, None, and.

Valid: total_bill, _count, marks2. Invalid: 2marks, total-bill, my name, class.

Tokens: the pieces of a program

import keyword
print(len(keyword.kwlist))
print(keyword.iskeyword("class"), keyword.iskeyword("Class"))
print(keyword.iskeyword("print"))

Python reads a line as tokens: keywords (if, True), identifiers (names like price), literals (15, "chai"), operators (+, ==) and punctuators (( ) : ,). Python 3.12 has 35 keywords. print is not one: it is a built-in function name.

CBSE Class 11 regularly asks: "Which of these are valid identifiers?", "Name the token type of 15, if, +, total", "What does type(10/2) return?" and "What is dynamic typing?" Each is 1-2 easy marks if you know the rules on this page.

In one line

Values have types (int, float, str, bool, None); print money with f"{x:.2f}"; names cannot start with a digit or be a keyword.