Lesson01 Introduction
Lesson01 Introduction
The current version of Python (3.x) was released in 2008. This version is not
compatible with the prior version (2.x). This course will be based on the syntax
of Python version 3.x.
Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
A. Irungu
Scalable: Python provides a better structure and support for large
programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good
features. A few are listed below-
Installing Python
Discuss in groups and find out the best way / form of python installation
Starting Python
1. Interactive Interpreter
You can start Python from a command-line interpreter or shell window. This is
achieved by executing the python command from a shell prompt. Then you
can start coding right away using the interactive interpreter.
While on Linux /Unix platforms always ensure the file permission mode allows
execution.
You can run Python from a Graphical User Interface (GUI) environment as well,
if you have a GUI application on your system that supports Python. Examples
are PyCharm, Visual Studio Code, Sublime Text, Vim, Atom, Jupyter
Notebook, Eclipse + PyDev + LiClipse, GNU Emacs, Spyder, Thonny.
A. Irungu
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). Punctuation characters such as @, $, and % are not allowed within
identifiers. Python is a case sensitive programming language.
• 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
and, as, assert, break, class, continue, def, del
elif, else, except, exec, finally, for, from, global,
if, import, in, is, lambda, not, or, pass,
print, raise, return, try, while, with, yield
Indentation / Blocks
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.
gender="Male"
if gender=="Male":
print ("Gender is Male")
print ("do something …")
else:
print ("Not Male")
print ("do a different …")
Quotations
Single, double, and triple (''' or """) quotes are used to denote string literals,
as long as the same type of quote starts and ends the string. Eg.
word = 'word'
A. Irungu
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made up of multiple lines and
sentences."""
Comments
A hash sign (#) that is not inside a string literal is the beginning of a comment.
All characters after the #, up to the end of the physical line, are part of the
comment and the Python interpreter ignores them.
Statements
Statements in Python typically end with a new line. The semicolon (;) allows
multiple statements on a single line. The line continuation character (\) allows
one statement to break to the next line. However, statements contained within
the [], {}, or () brackets do not need to use the line continuation character.
A. Irungu