The first program
Open a file called hello.py and put this in it.
name = input("Your name: ")
print(f"Hello, {name}. You have started.")
Two lines. In C the same program needs a header file, a main function, a char array, scanf, a printf with a format string, and a return 0. Python is not being clever. It is removing things the computer can work out by itself.
Why there are no semicolons
In C, ; tells the compiler "this statement is finished". Python decided that the end of a line already tells you that. So the newline is the statement terminator. You can still write a = 1; b = 2 on one line, but almost nobody does, because it is harder to read.
Why indentation is part of the language
This is the part that surprises everyone coming from C or Java.
marks = 72
if marks >= 40:
print("Pass")
print("Collect your marksheet")
print("Program over")
The two indented lines belong to the if. The third one does not, so it runs either way. In C you would write { and } and also indent, because an unindented block is unreadable. Python noticed that everyone was already indenting correctly, and made the indentation the real thing. The braces were duplicate information.
The practical effect: a Python program that runs is a Python program you can read. There is no such thing as badly indented working Python.
Never mix tabs and spaces in one file. They look identical on screen, but Python treats them as different, and you get TabError: inconsistent use of tabs and spaces in indentation. Set your editor to insert 4 spaces when you press Tab and forget the problem exists. Four spaces is the standard.
Why you do not declare types
In C you write int marks = 72;. You are telling the compiler two things: this box is 4 bytes wide, and treat the bits inside as a signed integer.
In Python you write marks = 72. The value 72 already knows it is an integer. The name marks is just a label tied to that value. Labels are not boxes with a fixed size, so there is nothing to declare.
marks = 72
print(type(marks)) # <class 'int'>
marks = "seventy two"
print(type(marks)) # <class 'str'>
The same name pointed at an int and then at a string. C will not allow that. Python will, and this is called dynamic typing.
Dynamic typing is convenient, not free. C catches int x = "hello"; at compile time. Python only finds out when the line actually runs - possibly in front of your external examiner. Later in this course you will see how to catch these early.
Interpreted, not compiled
gcc prog.c produces a.out, a file full of machine instructions your CPU runs directly. python prog.py does something different: the python program reads your file and carries out each instruction itself.
That gives you two things:
- You can test one line at a time. Type
python3with no filename and you
get a prompt where every line runs immediately. Use it constantly.
- The same
.pyfile runs on Windows, Linux and your college lab machines
without recompiling.
And it costs you one thing: speed. A tight loop in Python is roughly 30 to 100 times slower than the same loop in C. For a data structures assignment that does not matter. For a numerical method running a million iterations, it does. That is why engineering Python uses libraries like NumPy, whose inner loops are written in C.
Comments and the shape of a file
# This is a comment. Python ignores everything after the hash.
def area_of_circle(radius):
"""Return the area of a circle with the given radius."""
pi = 3.14159
return pi * radius * radius
print(area_of_circle(5)) # 78.53975
The triple-quoted line under def is a docstring. It is not a comment - it is stored with the function, and help(area_of_circle) prints it. Get into the habit now. In a viva, a function with a one-line docstring says you know what you wrote.
What to do before the next lesson
Type the three programs above by hand. Do not copy and paste. Then break them on purpose: remove an indent, add a semicolon, misspell print. Read the error message carefully each time. Python's error messages tell you the line number and the problem, and learning to read them is half of learning to program.