Python 3 beginner course · Chapter 2 of 10
This chapter explains Basics: Syntax, Print, Variables, Types and Operators in plain language. Read the concept, run the example, check the result and change one part to learn by doing. Optional packages are identified in their sections.
Python syntax and indentation
A colon starts a block after if, for, while or def. Indented lines belong to that block. Four spaces is a common style. Names are case-sensitive, so score and Score are different.
Python example
score = 85
if score >= 50:
print("Pass")Expected result: Pass
Practice: Predict the result before running the example. Change one value or line and explain the new result.
print() and readable output
print() writes values to the terminal. Separate values with commas for a space, or use an f-string to place variables inside text. Printing is useful while learning, but a function should usually return a value to its caller.
Python example
name = "Asha"
score = 85
print(f"{name} scored {score}")Expected result: Asha scored 85
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Variables and assignment
A variable name refers to a value. Python does not require a type declaration first. Choose meaningful names and use = to assign; use == to compare. Reassigning a name can change the value it refers to.
Python example
count = 2
count = count + 1
print(count == 3)Expected result: True
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Basic data types
int stores whole numbers, float stores decimal approximations, str stores text, and bool stores True or False. None means no value. Check a value with type(), but usually choose operations based on what the value means.
Python example
age = 20
price = 19.5
name = "Asha"
print(type(age).__name__, type(price).__name__)Expected result: int float
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Operators and comparisons
Use +, -, *, / and ** for arithmetic. Comparisons such as >= produce Boolean values. and, or and not combine conditions. Parentheses make a complex expression easier to read.
Python example
price = 20
quantity = 3
print(price * quantity, quantity >= 2 and price < 25)Expected result: 60 True
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Floor division and remainder
// rounds a division result down toward negative infinity; % gives the remainder. For positive counts, they help split items into full groups and leftovers. For negative values, test the result because floor is not the same as truncating toward zero.
Python example
print(17 // 5, 17 % 5)
print(-7 // 3)Expected result: 3 2 on the first line, then -3.
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Quick review
Explain the main idea to a friend in one sentence. Then recreate one example without looking at the code. If it fails, read the error message and compare the indentation and names.
Continue through the complete Python learning path. These are original explanations and diagrams; the topic checklist was compared with the w3resource Python index and the official Python tutorial.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.