Year 7 Programming_Python 1
Year 7 Programming_Python 1
Learning Objectives:
Python Libraries
Program libraries contain pre-written and tested modules, or self-contained programs, that can be called
by another program.
Program libraries save programmers time in writing and testing newly created functions.
Libraries also allow less experienced programmers to include complex things that they may not yet be able
to create for themselves. Therefore, using libraries in this way can simplify the whole programming
process.
How to import the library into a program
By using ‘import’ keyword for example : import math
Example of Math Library
1. import math
radius = 10
circleArea = math.pi * radius * radius
circleCircumference = math.pi * radius * 2
print(circleArea)
print(circleCircumference)
2. import math
fa=math.factorial(46)
p=math.pow(2,3)
print(fa)
print(p)
In programming, data type is an important concept. Variables can store data of different types, and
different types can do different things. Python has the many data types built-in by default but we will be
covering the following:
• int
• float
• String
• Character – Note: In python there is no character data type, a character is a string of length one
Example
• x = 1 # int
y = 2.8 # float
z = “Hello 123” # string
a= ‘G’ #character
Integers: Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Example
x = 90
y = 5547711
z = -3255522
Float : Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
Example
x = 2.40
y = 4.0
z = -23.49
Strings
A string is a collection of one or more characters put in a single quote, double-quote. It stores characters
that can include letters, symbols and numbers that cannot be used mathematically. In python there is no
character data type, a character is a string of length one. Strings in python are surrounded by either single
quotation marks, or double quotation marks.
1. Ask the user to enter True or False. Output a different message depending on their answer.
Answer:
entered = input("Enter True or False")
if entered == "True":
result = True
else:
result = False
or
if result == True:
print("Yes you entered True")
else:
print("Oh no, you did not enter True")
Example:
Note:
Python Operators
Python Operators in general are used to perform operations on values and variables. These are standard
symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different
types of Python operators.
• OPERATORS: Are the special symbols. Eg- + , * , /, etc.
In the example below, we use the + operator to add together two values:
print(10 + 20)
1. if statement
2. if-else
3. if-elif-else
4. nested if-else
If statement in Python : In control statements, The if statement is the simplest form. It takes a condition
and evaluates to either True or False. If the condition is True, then the True block of code will be executed,
and if the condition is False, then the block of code is skipped
Example of the if statement. In this example, we will calculate the square of a number if it greater than 5
number = 6
if number > 5:
# Calculate square
print(number * number)
print(“Try again”)
If – else statement
The if-else statement checks the condition and executes the if block of code when the condition is True,
and if the condition is False, it will execute the else block of code.
Example
if password == "abc789":
print("Correct password")
else:
print("Incorrect Password")
Chain multiple if statement in Python
In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions one after
another. This is useful when you need to check multiple conditions. With the help of if-elif-else we can
make a tricky decision. The elif statement checks multiple conditions one by one and if the condition
fulfills, then executes that code.
Example
def user_check(choice):
if choice == 1:
print("Admin")
elif choice == 2:
print("Editor")
elif choice == 3:
print("Guest")
else:
print("Wrong entry")
user_check(1)
user_check(2)
user_check(3)
user_check(4)
In Python, the nested if-else statement is an if statement inside another if-else statement. It is allowed in
Python to put any number of if statements in another if statement.
Indentation is the only way to differentiate the level of nesting. The nested if-else is useful when we want
to make a series of decisions.
Answer: a diamond
And:
The and keyword is a logical operator, and is used to combine conditional statements:
Example
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or:
Example
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")