Computing: Introduction to Python
Lesson One
Learning Objective
• To use a textual programming language to solve a variety of
computational problems.
Success Criteria
• To know how to start Python IDLE and run a Python program.
• To use Python to perform calculations.
• To write simple programs that respond to user input.
What Is Python?
Python is a programming language.
It was invented in the 1980s.
It was named after comedy group ‘Monty Python’.
Python is used by:
• Yahoo (maps);
Python is easy
• Google (search engine);
to learn.
• Nokia;
• Facebook, and many more.
Python is used in many universities to teach programming.
How to install Python
[Link]
Starting Python
We can use Python IDLE to create and run our Python programs.
IDLE is an acronym of Integrated Development Environment.
An Integrated Development Environment contains all the tools needed to
write, test and run computer programs.
• The window that first
appears when you
start Python IDLE is
called the Interactive
mode window.
Starting Python
• From the Interactive mode window, we can click on the File menu item,
and then select New File.
• This opens a new Script mode window.
Starting Python
• Good practice is to have instances of both Interactive and Script mode
windows open side-by-side, something like this:
Interactive Script
On the left is the
On the right is
Interactive mode
the Script mode
window.
window.
Starting Python
• The Interactive mode window is easy to identify.
• You will always see >>> at the start of each new line.
Starting Python
Key Term
An Integrated Development Environment contains all the tools
needed to write, test and run computer programs. In Python
we use the acronym IDLE. Other programming languages
sometimes use the shorter acronym IDE for Integrated
Development Environment.
Python IDLE has two modes of running.
The Interactive mode gives immediate feedback to every line of code that is
entered, and is useful for testing ideas and small snippets of code.
The Script mode allows a programmer to type in many lines of Python
code, and then save the code as a single program with a file name. This
file can be saved and run at any time. However, any inputs or outputs of
Python programs will always be shown in the Interactive mode.
Hello World
• Let’s stick with the Interactive mode window for now,
and learn our first Python command: print
• print does exactly what it says: it prints information onto the screen.
• It does not print anything onto a printer.
• There is a long established tradition in computer programming.
The first program that most programmers write is often a
“Hello World” program.
• In the Python Interactive window, type: print(“Hello World”)
and then press the Enter key on your keyboard.
Hello World
• Some important things to notice:
Always remember to use the (brackets) with print.
• We also used “quotes” because we were printing text.
Did you notice the different colours?
• print is purple because IDLE recognises print as a Python command.
• “Hello World” is green because it is a string of text (more on this later).
• The printed output is usually blue.
Try the print command again, but this
time type your own sentences.
For example: print(“Goodbye Monty”)
Output variables
✔ The Python print() function is often used to output variables.
Example
x = "Python is awesome"
print(x)
✔ In the print() function, you output multiple variables, separated by a comma:
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
You can also use the + operator to output multiple variables:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
For numbers, the + character works as a mathematical operator:
x = 5
y = 10
print(x + y)
In the print() function, when you try to combine a string and a number with
the + operator, Python will give you an error:
Example
x = 5
y = "John"
print(x + y)
The best way to output multiple variables in the print() function is to separate
them with commas, which even support different data types:
Example
x = 5
y = "John"
print(x, y)
Creating a comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
Example
print("Hello, World!") #This is a comment
Input
The input() function allows user input.
Example:
Use the prompt parameter to write a message before the input:
x = input('Enter your name:')
print('Hello, ' + x)
Example:
name = input("Enter your name: ") # input always returns a string
age = input("Enter your age: ") # convert the input to an integer
print("Hello,", name, "you are", age, "years old.")
Variables
• Variables do not need to be declared with any particular type, and can
even change type after they have been set.
• Variable names are case-sensitive.
• A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
• Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different
variables)
• A variable name cannot be any of the Python keywords.
myvar = "John"
my_var = "John"
_my_var = "John" 2myvar = "John"
myVar = "John" my-var = "John"
MYVAR = "John" my var = "John“
myvar2 = "John"
Camel Case
Each word, except the first, starts with a capital
letter:
myVariableName = "John"
Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"
Snake Case
Each word is separated by an underscore
character:
my_variable_name = "John"
Assign multiple values
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z) Note: Make sure the number of variables
matches the number of values, or else you will
get an error.
Specify a variable type
Casting in python :
•int() - constructs an integer number from an integer literal, a float literal (by removing
all decimals), or a string literal (providing the string represents a whole number)
•float() - constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
•str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
ExampleG((type_cast.py)
#Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
print(“X value is”,x)
print(“Y value is”,y)
#Floats:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
#Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'