Programming Basics

Anatomy of a Python Program

Each programming language provides a collection of constructs that we use to write programs. There are 2 aspects we need to understand about each construct to use it successfully: its syntax and its semantics. The syntax refers to the grammatical rules that we need to follow, while the semantics refers to what the construct means.

We’ll be diving into Python syntax with some code. At this point, just start to get comfortable looking at code; you will practice more in the next assignment. One important thing to keep in mind is that, in Python, whitespace (spaces, tabs and returns), especially at the beginning of a line, matters!

The most basic constructs in Python are comments, expressions, variables, and statements.

comment

A comment provides information to describe parts of the program to anyone (including ourselves) who is trying to read and understand a program. A comment explains what the programmer’s intent is to another person. These are ignored by the Python interpreter, so as far as Python is concerned, they actually have no semantics! However, to another human, they are VERY IMPORTANT for making your code readable, debuggable and reusable!

There are two syntactic forms for comments. A single line comment is:

  • denoted by # and everything after it is ignored

Example single line comments:

# this is a comment ignored by Python, just for humans!

x = 3 # this is a comment at the end of a line


A multi-line comment is

  • denoted by surrounding the comment with 2 sets of triple quotation marks

Example multi line comment:

""" this is a multi

line comment ignored by Python,

just for humans! """


expression

An expression performs a simple computation, like adding two numbers together. When Python evaluates an expression, it produces a value. There are many different kinds of expressions, which we will see in more detail later.

Example expressions:

4 + 2*6 # This evaluates to 16

"Hello, " + "World!" # This evaluates to "Hello, World!"


variable

A variable is used to give a name to a value. This is done with an assignment statement.

Example assigning to a variable:

x = 4 + 2*6 # This assigns 16 to the variable x.


Variables can be used in expressions. When Python evaluates an expression that contains a variable, it uses the value that the variable has.

Example expression using a variable:

x = 4 + 2*6 # This assigns 16 to the variable x.

y = x + 1 # This assigns 17 to the variable y.


statement

A statement is a single instruction. When executed, a statement does something. We have just seen an assignment statement. An assignment statement evaluates the expression to the right of the = sign, and assigns the resulting value to the variable to the left of the = sign. There are again many different kinds of statements, each with their own semantics, which we will see more of later.

Example statements:

x = 5 # This sets the variable x to the value 5

print( "Hello, " + "World!" ) # This displays on the screen the text Hello, World!

A sample program

birthday = 2802 # set the value of birthday to 2802 (Feb 28)

total = 0 # declare a variable to maintain the sum of the digits


""" add the first digit

note that 1000 is of type int

when dividing by an int in Python 3 with //, truncates

e.g., 2802 / 1000 = 2 not 2.802)"""

total = total + (birthday // 1000)


# add the second digit

# the % (mod) operator gives the remainder after division

# e.g., 10 % 10 = 0, 12 % 10 = 2

total = total + ((birthday // 100)%10)


# add the third digit

total = total + ((birthday // 10)%10)


# add the last digit

total = total + (birthday % 10)


# print it out for the user

print( total )

Next: Variables