Lesson 1 - Introduction To Python
Lesson 1 - Introduction To Python
Lesson 1 - Introduction To Python
(Programming in Python)
It is used for:
1
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python can be used for rapid prototyping, or for production-ready software
development.
1.1.2 Why Python?
The most recent major version of Python is Python 3, which we shall be using
in this tutorial. However, Python 2, although not being updated with anything
other than security updates, is still quite popular.
In this tutorial Python will be written in a jupyter notebook. It is possible to
write Python in other Integrated Development Environments, such as Thonny,
Pycharm, Netbeans or Eclipse which are particularly useful when managing
larger collections of Python files.
1.1.3 Python Syntax compared to other programming languages
Python was designed for readability, and has some similarities to the English
language with influence from mathematics.
Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as the
scope of loops, functions and classes. Other programming languages often
use curly-brackets for this purpose.
2
1.2 Python Outputs and Inputs
Results of computations during program execution must be displayed to the
user. These are called program outputs. Similarly, the user may need to supply
certain data to the program to aid it in carrying out its computations. Such data
are called program inputs.
1.2.1 The print() function
The print() function displays output to the user. Output is the information or
result produced by a program. The sep and end options can be used to
customize the output. Table 1.1 shows examples of sep and end. Multiple
values, separated by commas, can be printed in the same statement. By default,
each value is separated by a space character in the output. The sep option can
be used to change this behavior.
By default, the print() function adds a newline character at the end of the
output. A newline character tells
3
1.2.2 The input() function
Computer programs often receive input from the user. Input is what a user enters
into a program. An input statement, variable = input("prompt"), has three parts:
2. The input() function reads one line of input from the user. A function is a named,
reusable block of code that performs a task when called. The input is stored in the
computer's memory and can be accessed later using the variable.
3. A prompt is a short message that indicates the program is waiting for input. In
the statement above, "prompt" can be omitted or replaced with any message.
State your observed difference between the code and the output displayed
underneath.
1.3 Variables
Variables allow programs to refer to values using names rather than memory
locations. Ex: age refers to a person's age, and birth refers to a person's date of birth.
A statement can set a variable to a value using the assignment operator (=). Note that
this is different from the equal sign of mathematics. Ex: age = 6 or birth = "May 15".
The left side of the assignment statement is a variable, and the right side is the value
the variable is assigned.
4
1.3.1 Rules for Naming Variables
A variable name can consist of letters, digits, and underscores and be of any
length.
The name cannot start with a digit. Example: 101class is invalid.
Variable names are case sensitive. Example: Total is different from total.
Variable names must not have spaces between characters. Python's style guide
recommends writing variable names in snake case, which is all lowercase
with underscores in between each word, such as first_name or total_price.
A name should be short and descriptive, so words are preferred over single
characters in programs for readability. Example: A variable named count
indicates the variable's purpose better than a variable named c.
Python has reserved words, known as keywords, which have special
functions and cannot be used as names for variables (or other objects).
Figure 1.1 shows some of the common python keywords that should not be used as
identifiers.
To include a single quote (') in a string, enclose the string with matching double
quotes (").
Example: "Won't this work?"
To include a double quote ("), enclose the string with matching single quotes (').
Example: 'They said "Try it!", so I did'.
Write a program that asks the user to input their first and last name separately. Use
the following prompts (example input in bold):
What is your first name? Alan
What is your last name? Turing
The program should then output the length of each name. Based on the example
input above, the output would be:
Your first name is 4 letters long
Your last name is 6 letters long
age = 20 integer
The type() function is used to find out of what data type is a variable name. Thus,
given the variables in table 1.3 above, the statement print(type(age)) will
return <class ‘int’> indicating that the variable age is of type integer.
8
1.5.2 Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication, and division on numeric data. Table 1.4 shows the
basic arithmetic operators recognized by python:
+ Addition 5+3=8
- Subtraction 5–3=2
* Multiplication 5 * 3 = 15
// Integer division 5 // 3 = 1
** Exponentiation 5 ** 3 = 125
9
Exponentiation and assignment operations are right associative, so
consecutive instances of these are evaluated from right to left.
You can use parentheses to change the order of evaluation.
The caret character (^) shows where Python found the error. Sometimes the error
may be located one or two lines before where the caret symbol is shown because
Python may not have discovered the error until then.
Traceback is a Python report of the location and type of error. The word traceback
suggests a programmer trace back in the code to find the error if the error is not seen
right away.
Learning to read error messages carefully is an important skill. The amount of
technical jargon can be overwhelming at first. But this information can be very
helpful.
11
Table 1.5: Common Errors in Python Programming
12
Activity 1.7: Error Messages 2
This code is based on an earlier example, but the code contains several mistakes.
• One line is missing required punctuation, and another line uses incorrect symbols.
• Run the program to find the first error, and correct the corresponding line of code.
• Keep running and correcting the program until no errors are found.
team1 = "Liverpool"
score1 = "4"
team2 = "Chelsea"
score2 = "3"
In the program segment above, lines 1, 8, and 10 contain comments. Each comment
begins with a hash character (#). All text from the hash character to the end of the
line is ignored when running the program. In contrast, hash characters inside of
strings are treated as regular text. For instance, the string "Item #1: " does not contain
a comment.
13
• Comments should explain the purpose of the code, not just repeat the code itself.
For example, # Get the user's preferences is more descriptive than # Input item1 and
item2.
14