0% found this document useful (0 votes)
2 views7 pages

Python Cheet Sheets

This handout serves as a reference for Year 8 students learning Python programming, covering key elements such as variables, input/output, libraries, selection, and iteration. It includes syntax, examples, and notes for each topic to aid understanding. Resources are regularly updated and available online under an open government license.

Uploaded by

bb mg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Python Cheet Sheets

This handout serves as a reference for Year 8 students learning Python programming, covering key elements such as variables, input/output, libraries, selection, and iteration. It includes syntax, examples, and notes for each topic to aid understanding. Resources are regularly updated and available online under an open government license.

Uploaded by

bb mg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Year 8 – Intro to Python programming Handout

Handout: Python cheat sheets


Introduction
This is a reference handout for the Python elements covered in this unit. The sheets include short
explanations, brief notes, syntax, and selected examples.

The content has been grouped into categories:


● Variables, assignments, operators, and expressions
● Output and input
● Libraries: randomness and time
● Selection
● Iteration

Resources are updated regularly — the latest version is available at: ncce.io/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this licence, see ncce.io/ogl.
Output
The print function displays literals (e.g. numbers, text) and
the values of variables and expressions.

Syntax
print(comma-separated literals, variables, expressions)

Examples
Display the string literal
print("Hello world") "Hello world"

Display a string literal and the value of


print("Hello", user) the user variable

Display, among others, the value of the


print(x, "times two is”, 2*x) expression 2*x

Input
The input function reads a line of text from the keyboard and
returns it.

Syntax
input()

Notes
Assign the value returned by input to a variable, if you need to
refer to that value later in your program.
Use the int function to convert the text returned by input to an
integer.
Use the float function to convert the text returned by input to a
floating-point number.

Examples
Read text from the keyboard and
name = input() assign it to the name variable

Read text from the keyboard, convert it


years = int(input()) to an integer, and assign it to the years
variable

Read text from the keyboard and


input() discard it (useful for pausing execution
until Enter is pressed)
Assignment
An assignment statement evaluates an expression and
associates its value with the name of a variable (an identifier).

Syntax
variable name = expression

Notes
Do not interpret the = sign as an equation. Assignments are actions
to be performed.
Read assignments from right to left, i.e. evaluate the expression and
then assign the value to the variable.
A variable name can only refer to a single value. A new assignment
to a variable replaces the previous value of the variable.

Examples
Assign the string literal "Ada" to the
name = "Ada" name variable

Evaluate the expression 365*years and


days = 365*years assign the value to the days variable

Call the randint function and assign the


dice = randint(1,6) value it returns to the dice variable

Evaluate the expression count+1 and


count = count+1 assign the value to count,
i.e. increase count by 1

Evaluate the expression 2*a and assign


a = 2*a the value to a,
i.e. double the value of a
Operators and expressions

Arithmetic Relational (comparisons)


Perform calculations with numbers. The Compare the values of expressions. The
result of these operations is also a number. result of these operations is either True or
False (so relational operators form logical
Addition: + expressions).
Subtraction: -
Multiplication:* Equal to: ==
Division: / Not equal to: !=
Integer division: // Less than: <
Remainder: % Less than or equal to: <=
Exponent: ** Greater than: >
Greater than or equal to: >=

Notes Logical
Logical expressions evaluate to either True or Negate or combine logical expressions. The
False. result of these operations is either True or
‘Logical expression’ is a synonym for False.
condition. To evaluate a logical expression is to
check a condition. Negation: not
Conjunction: and
Disjunction: or

Examples
An arithmetic expression involving
3 + 13 * 3 operators and literals

An arithmetic expression involving


2**8 - letters - numbers - symbols operators, literals, and variables

A logical expression, comparing the


applications <= positions values of two variables

A logical expression, checking if the


a + b == c - d values of two expressions are equal

A logical expression, which is the


user != "Ada" and logins < 3 conjunction of two simpler logical
expressions
Modules
Modules are libraries of existing code.
They extend the functionality of the language by offering
components (such as functions) that can be imported and used
in programs.

Syntax
from variable import component

Note
It is standard practice that you place all import statements at the
beginning of the program.

Examples

The random module


docs.python.org/3/library/random.html

Provides functionality for generating random numbers

Call the randint function to generate a


from random import randint random integer from
dice = randint(1,6) 1 to 6 and assign the value that it
returns to the dice variable

Call the randint function to generate a


from random import randint random integer from
coin = randint(0,1) 0 to 1 and assign the value that it
returns to the coin variable

The time module


https://docs.python.org/3/library/time.html

Provides functionality for time and date handling

from time import sleep Call the sleep function to pause


sleep(3) program execution for 3 seconds

Use the localtime function to retrieve


from time import localtime
the current year and assign it to the
year = locatime().tm_year year variable
Selection
The if statement creates branches in the flow of program
execution.
At runtime, a condition or a sequence of conditions are
checked, to select which one of the possible branches will be
followed.

Syntax
if condition:
block of statements
(the if block)
elif condition:
block of statements
(an elif block — optional, there may be many)
else:
block of statements
(the else block — optional)

Notes
Out of the different blocks of statements contained in a selection
structure, at most one block will be executed at runtime.
The blocks of statements can contain nested if and while
statements.

Examples

if dice1 == dice2: Check if the values of the dice1 and


dice2 variables are equal and perform
print("A double roll") the appropriate actions, depending on
total = 4*sum the outcome
else:
There are two possible, mutually
total = sum exclusive branches.

if temperature < 4: Check the range in which the value of


the temperature variable lies and print
print("Freezing") an appropriate message, depending on
elif temperature < 18: the outcome
print("Tolerable")
There are three possible, mutually
else: exclusive branches.
print("Nice and warm")

Compute max, the greatest value


max = x
among x, y, and z
if y > max:
max = y These if statements compare y and z to
the current max and raise max, if
if z > max: necessary.
max = z
Without an elif, the two if statements
are not mutually exclusive.
Iteration
The while statement creates a loop in the flow of program
execution.
At runtime, a set of actions is repeated and a condition is
checked to determine if the loop should continue.

Syntax
while condition:
block of statements
(the while block)

Notes
The block of statements in the iterative structure may be executed
many times, once, or even not executed at all (if the while
condition is False when it is first checked).
The block of statements can contain nested if and while statements.

Examples

# display a count from 1 to 10


count = 1 Repeat the indented block of
while count <= 10: statements while count does not exceed
10
print(count)
count = count+1

print("What is your name?")


name = input()
# only take "Ada" for an answer Repeat the indented block of
statements while name does not equal
while name != "Ada":
"Ada"
print("I was expecting Ada")
print("What is your name?")
name = input()
# end of loop, welcome user
print("Welcome")

non_zero = True
while non_zero == True: Repeat the indented block of
statements while the Boolean flag
a = int(input()) non_zero remains True
if a != 0:
# display inverse of a
print(1/a)
else:
non_zero = False

You might also like