← All lessons

Lesson 2

Python Fundamentals and Core Programming Logic

Item 1

Data Type

A data type tells Python what kind of value a variable holds. int is a whole number, float has decimal points, str is text and bool is True or False. Python uses dynamic typing: the type is decided when you assign a value. Use type() to check it and int(), float(), str() for type casting.

int — Whole numbers: 0, -10, 42.
float — Decimal numbers: 3.14, 9.5.
str — Text inside quotes: "Hello".
bool — True or False.
Type Casting — Converting a value from one type to another.

Try it — edit the code and press Run:

Item 2

Arithmetic Operators

Arithmetic operators do math: + - * / and also // (floor division), % (modulus = remainder) and ** (power). Order of operations follows BODMAS: brackets, powers, multiply/divide, then add/subtract from left to right.

/ — Division, always gives a float.
// — Floor division, removes the decimal part.
% — Modulus, gives the remainder.
** — Exponent (power).

Try it — edit the code and press Run:

Item 3

Assignment, Comparison & Logical Operators

Assignment operators like = += -= update variables (Python has no x++). Comparison operators (== != > < >= <=) return True or False. Logical operators and, or, not combine conditions.

+= — Add and assign: x += 1 means x = x + 1.
== — Checks if two values are equal.
and / or / not — Logical operators that combine booleans.

Try it — edit the code and press Run:

Quick Quiz

1. What is the data type of the value 3.14 in Python?

2. What is the result of 10 // 3 in Python?

3. Which operator is used to find the remainder of a division?

4. What is the output of 2 ** 4?

5. What is the result of (5 > 3) and (2 < 1)?

6. How do you increment a variable x by 1 in Python?

Score: 0 / 6(Sign in to save your score)

Practice

Auto-checked exercises at three levels. Open hints one by one before the solution.

⭐Basic

Remainder

Print the remainder of 17 divided by 4.

⭐⭐Intermediate

Rectangle area

Set length = 8 and width = 5, then print the area.

⭐⭐⭐Challenge

Scores calculator

math = 90, science = 80, english = 70. Print the total on the first line and the average on the second line.

Smart Assistant Project

Task 2: The Calculation Engine

Goal: Integrate arithmetic logic and process multiple scores.

Requirement: Add input for three subjects (Math, Science, English). Use arithmetic operators to calculate the Total, Average and Percentage.