What is a program?
A program is a list of instructions for a computer, followed exactly, in order, from top to bottom. Python is a language for writing those instructions, and most AI work today is written in it because it reads almost like English.
print: make the computer speak
print("Namaste!")
print("I am learning Python.")
print(2 + 3)Output
Namaste! I am learning Python. 5
- print(...) shows whatever is inside the brackets.
- Text goes inside quotes. Without quotes, Python thinks it's a name or a command.
- Numbers need no quotes, and Python does the sum for you.
Variables: labelled boxes
A variable is a labelled box that holds a value. The = sign means “put this into the box”. It does not mean “equals” as in maths.
name = "Riya"
age = 14
print(name)
print(age)
age = age + 1 # Take out 14, add 1, put 15 back
print(age)Output
Riya 14 15
Rules for names
| Name | Allowed? | Why |
|---|---|---|
| total_marks | Yes | Letters and underscores: this is the usual style |
| marks2 | Yes | Numbers are fine after the first letter |
| 2marks | No | Can't start with a number |
| total marks | No | No spaces allowed |
| Age and age | Different boxes | Capital letters matter |
Numbers and text are different things
| Type | Name in Python | Example |
|---|---|---|
| Whole number | int | 42 |
| Decimal number | float | 3.14 |
| Text | str | "hello" |
| True or false | bool | True |
print(type(42))
print(type(3.14))
print(type("hello"))
# The same symbol does different things
print(2 + 3) # Numbers: add
print("2" + "3") # Text: join togetherOutput
<class 'int'> <class 'float'> <class 'str'> 5 23
Python as a calculator
| Symbol | Meaning | Example | Answer |
|---|---|---|---|
| + | Add | 7 + 2 | 9 |
| - | Subtract | 7 - 2 | 5 |
| * | Multiply | 7 * 2 | 14 |
| / | Divide | 7 / 2 | 3.5 |
| // | Divide, drop the decimal | 7 // 2 | 3 |
| % | Remainder | 7 % 2 | 1 |
| ** | Power | 7 ** 2 | 49 |
Mixing text and values: f-strings
Put an f before the quotes, and anything in curly brackets is filled in.
name = "Aman"
marks = 86
print(f"{name} scored {marks} marks.")
print(f"Next year {name} wants {marks + 10}.")Output
Aman scored 86 marks. Next year Aman wants 96.
Errors are normal: how to read them
Every programmer sees errors daily. An error isn't a judgement of you. It's Python telling you what confused it. Read the last line first.
| You see | What it means | Usual fix |
|---|---|---|
| NameError: name 'nme' is not defined | Python has no box with that name | Check the spelling, and that you created it earlier |
| SyntaxError | The grammar is wrong | Look for a missing quote or bracket |
| TypeError: can only concatenate str | You mixed text and a number with + | Use an f-string |
| IndentationError | The spaces at the start of a line are wrong | Remove the extra spaces |
Here is a bug to fix. Run it, read the last line of the error, then correct it.
name = "Riya"
print(nme)Your first small project: a marks calculator
Everything so far, together. Change the marks to your own.
# Marks out of 100 in each subject
maths = 78
science = 85
english = 90
total = maths + science + english
percentage = total / 300 * 100
print(f"Total: {total} out of 300")
print(f"Percentage: {percentage:.1f}%")Output
Total: 253 out of 300 Percentage: 84.3%
Practice
- Print your name, and your age next year, using an f-string.
- A pen costs ₹12. Work out the price of 7 pens in a variable, then print it.
- Add a fourth subject to the marks calculator. What else must you change?
- What does 17 % 5 give? Guess first, then run it.
Finished reading?
Comments: notes for humans
Anything after a # is ignored by Python. Use it to leave yourself a note.
Output