Syntax

Quickstart

Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.

The way to run a python file is like this on the command line:

C:\Users\pyclubKNUST>python helloworld.py

Where "helloworld.py" is the name of your python file.

Let's write our first Python file, called helloworld.py, which can be done in any text editor.

helloworld.py

print("Hello, World!")

Save your file. Open your command line, navigate to the directory where you saved your file, and run:

C:\Users\pyclubKNUST>python helloworld.py

The output:

Hello, World!

The Command-line

To test a short amount of code in python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself.

Type the following on the Windows, Mac or Linux command line:

"python" command did not work, you can try "py":

Type print("Hello, World!")

Whenever you are done in the python command line, you can simply type the following to quit the python command line interface:

exit()

Congratulations, you have written and executed your first Python program.

Python Identifiers

A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.

Here are naming conventions for Python identifiers-

  • Class names start with an uppercase letter. All other identifiers start with a lowercase letter.

  • Starting an identifier with a single leading underscore indicates that the identifier is private.

  • Starting an identifier with two leading underscores indicates a strong private identifier.

  • If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

Reserved Words

The following list shows the Python keywords. These are reserved words and you cannot use them as constants or variables or any other identifier names. All the Python keywords contain lowercase letters only.

and

exec

Not

as

finally

or

assert

for

pass

break

from

print

class

global

raise

continue

if

return

def

import

try

del

in

while

elif

is

with

else

lambda

yield

except

Lines and Indentation

Python does not use braces({}) to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example-

Good to know: Spaces or Tabs?

The recommended indentation is 4 spaces but tabs or spaces can be used so long as they are consistent. The spacing should be even and uniform throughout.

Do not mix tabs and spaces in Python as this will cause an error in Python 3 and can cause errors in Python 2.

Improper indentation can cause an IndentationError or cause the program to do something unexpected.

Last updated