Python Notes 1 To 3
Python Notes 1 To 3
Python Notes 1 To 3
Python Introduction
What is Python?
Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991.
It is used for:
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc).
Python has a simple syntax similar to the English language.
1
Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be very
quick.
Python can be treated in a procedural way, an object-oriented way or a
functional way.
Example
print("Hello, World!")
Python Comments
Comments can be used to explain Python code.
2
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the rest of
the line:
Example
print("Hello, World!") #This is a comment
Since Python will ignore string literals that are not assigned to a variable, you
can add a multiline string (triple quotes) in your code, and place your comment
inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
3
Python Programs
Program 1:
Program 2:
Program 3:
Program 4:
4
Python Y8
Python Print() Function
The Print function is the most basic and the baby step that you take while
learning a particular language.
Hello, World!
To display a variable's value along with a predefined string, all you need to do
is add a comma in between the two. Here the position of the predefined string
and the variable does not matter.
Different ways to assign value to variables
You can use single quotes or double quotes or triple quotes to create strings. There
is no difference in the functionality of single quote and double quote in python
'''
You can use three single quotes or three double quotes to create a string with
multiple lines
'''
Students’ Tasks
Program 1
Using Print function produce the following output:
Output
Program 2
Using Print function produce the following multiline string output:
Output
Program 3
Write a program using print that, when run, prints out a tic-tac-toe
board.
Output
Program 4
Write a program using input and print functions to display your name
and date of birth:
Y8 Python notes 3
Variables
Variables are used to store data in a program.
A variable is a named location in the computer’s memory which stores data of a
particular type. Data is entered by the user whilst the program is running.
Variables can hold data of different types. In order to store the data you must
decide on a name and a data type for the variable first. For example, to store the
player’s age you will need a variable which can hold a whole number (also known
as an integer)
To store the player’s name you will need a variable which can hold letters – this is
called a string.
The variable names have been chosen as playerage and playername but they
could have been called anything we wanted. Variables can hold different data
types. The table shows some examples of each data type.
Page 1 of 6
Input Statement
To capture data from the user the input function.
The input function captures user input as a string data type.
This contains numbers, letters and symbols. For example ‘Robot’,
‘Password123’ and ‘WWW777’ are all examples of string data type.
Input is a built-in function in Python – it is the part of the Python
language
The input statement prints a question onto the screen and captures
the user’s answer to the question in a variable called
playername…………………………….
playername = input (“What is your name?”)
print(“Welcome “+ playername)
playername is then used in the next line of code and printed onto
the screen as part of a welcome message. Can you guess what will be
printed onto the screen?
Assignment Statement
The value of a variable can also be set by using an assignment
statement. This gives the variable a value.
Page 2 of 6
An assignment statement includes an equals symbol and takes the
form:
name of variable = value
For example:
playerlives = 3
total = score1 + score2
playername = ”George”
Page 3 of 6
Constants
Example of constants:
PI = 3.14
GRAVITY = 9.8
Example Programs:
Page 4 of 6
Python Worksheet 3
Program 1
Write a program to use input function and ask two questions:
1. Person’s name
2. Favorite color
Produce the following output:
Program 2
Write a program that will ask the year when you were born and then it
will calculate and display your age.
Produce the following output:
Program 3
Write a program to input integer marks for three subjects out of 50,
calculate and print the total and average of three subject marks.
Page 5 of 6
Produce the following output:
Page 6 of 6