Python Refresher

Basics & Syntax

Python feels close to English, but the computer still needs clear rules. This page teaches the core rules in a simple way so you can write clean code with confidence.


1. Variables: Storing Information

A variable is a named box where you keep a value.

python
1name = "Riya"
2age = 13
3height = 5.1
4is_student = True

What it is: A variable stores data.

How it is used: You save values and reuse them later.

When it is used: Almost everywhere in every program.


2. Data Types You Must Know

TypeExampleMeaning
int42Whole number
float3.14Decimal number
str"hello"Text
boolTrue, FalseYes/No logic
NoneTypeNoneNo value yet

You can check type using:

python
1x = 42
2print(type(x))

3. Input and Output

Output with print()

python
1score = 95
2print("Score:", score)
3print(f"Score is {score}")

Input with input()

python
1user_age = input("Enter age: ")
2user_age = int(user_age)
3print(user_age + 1)

Important: input() gives text by default, so convert with int() or float() when needed.


4. Operators

Math operators

python
1a = 10
2b = 3
3
4print(a + b)
5print(a - b)
6print(a * b)
7print(a / b)
8print(a // b)
9print(a % b)
10print(a ** b)

Comparison operators

python
1print(5 > 2)
2print(5 == 2)
3print(5 != 2)

Logical operators

python
1is_hot = True
2is_humid = False
3
4print(is_hot and is_humid)
5print(is_hot or is_humid)
6print(not is_hot)

5. Indentation: Python's Biggest Rule

In Python, spaces are not decoration. Spaces define code blocks.

python
1temperature = 32
2
3if temperature > 30:
4 print("Too hot")
5 print("Turn on fan")
6
7print("Done")

If indentation is wrong, Python throws IndentationError.

Use 4 spaces

Use 4 spaces for each level. Do not randomly mix tabs and spaces.


6. Conditions (if, elif, else)

Conditions help code make decisions.

python
1marks = 78
2
3if marks >= 90:
4 grade = "A"
5elif marks >= 75:
6 grade = "B"
7else:
8 grade = "C"
9
10print(grade)

What it is: Decision logic.

How it is used: Run one path if condition is true, another path otherwise.

When it is used: Validation, filtering, alerts, business rules.


7. Loops

for loop (known range or collection)

python
1for i in range(5):
2 print(i)

while loop (repeat until condition changes)

python
1count = 0
2while count < 3:
3 print(count)
4 count += 1

What it is: Repetition.

How it is used: Run same action for many values.

When it is used: Reading rows, processing sensor samples, retrying tasks.


8. Strings (Text Handling)

python
1message = "analog data"
2
3print(message.upper())
4print(message.lower())
5print(message.replace("analog", "edge"))
6print(len(message))

Common beginner need: formatting text with variables:

python
1name = "Aarav"
2score = 91
3print(f"{name} scored {score}")

9. Basic Error Handling (try/except)

python
1try:
2 x = int(input("Enter a number: "))
3 print(100 / x)
4except ValueError:
5 print("Please enter a valid integer")
6except ZeroDivisionError:
7 print("Cannot divide by zero")

What it is: A safe way to handle runtime errors.

How it is used: Wrap risky code in try, recover in except.

When it is used: User input, file access, network calls, parsing.


10. Good Habits from Day 1

  • Use meaningful variable names: temperature_c, not t.
  • Keep functions small and focused.
  • Add comments only where logic is not obvious.
  • Print intermediate values while debugging.
  • Run code in small steps, not as one giant file.

Tiny Practice Problems

  1. Ask user for two numbers and print sum, product, and average.
  2. Ask user marks and print grade using if/elif/else.
  3. Print all even numbers from 1 to 50.
  4. Count how many vowels are present in a word.

If you can solve these comfortably, your basics are strong.

Previous
Introduction to Python