0% found this document useful (0 votes)
295 views19 pages

Python Notes 1 To 3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

Pakistan International School Jeddah – English Section

International Computing – Stage 8

Unit 8.4 High Level Programming Language


Skills Covered: Python Notes
Name: ___________________________________________ Grade: Y8 __

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:

 web development (server-side),


 software development,
 mathematics,
 system scripting.

What can Python do?


 Python can be used on a server to create web applications.
 Python can be used alongside software to create workflows.
 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.

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.

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.

Example
print("Hello, World!")

Python Comments
Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing 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.

Let's begin by printing the Hello, World! example.


print("Hello, World!")

Hello, World!

Parenthesis is a must else it will raise a syntax error


let's say you want to add a new line or a vertical space between the below
two outputs, to achieve this you can simply call the print() function without
passing any argument in it.
Let's say you have two strings and you want to join them together with
space, all you would need to do is in the print function of the first
string str1 add end argument with quotes, and you can expect the two
strings to be joined by a space.
Let's now see how you can make use of the print function to take input from
the user. For this, you will use python's built-in function input().

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”

Data Types Examples

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

You might also like