0% found this document useful (0 votes)
8 views

Year 7 Programming_Python 1

The document outlines the fundamentals of Python programming, including the use of program libraries, data types, operators, and conditional statements. It emphasizes the importance of libraries for simplifying programming tasks, details various data types like integers and strings, and explains how to use conditional statements to control program flow. Additionally, it covers iterative development, testing algorithms, and the significance of using a range of test data.

Uploaded by

anayadua105
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Year 7 Programming_Python 1

The document outlines the fundamentals of Python programming, including the use of program libraries, data types, operators, and conditional statements. It emphasizes the importance of libraries for simplifying programming tasks, details various data types like integers and strings, and explains how to use conditional statements to control program flow. Additionally, it covers iterative development, testing algorithms, and the significance of using a range of test data.

Uploaded by

anayadua105
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Programming Language

Learning Objectives:

1. Outline the purpose of program libraries.


2. Identify and describe data types in text-based programs, including Integer, Real and Boolean.
3. Identify and know how to use library functions in text-based programs.
4. Know how to develop text-based programs with conditional (selection) statements.
5. Know how to develop text-based programs using data types, including Integer, Real, String and
Boolean.
6. Know how to develop text-based programs which use rules involving AND, OR and NOT.
7. Use an iterative process to develop programs.
8. Know how to develop and apply test plans.
9. Explain the need for using a range of test data.
10. Know how to test algorithms using suitable data.

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

Math Library Function Example of Code using library

math.factorial(n) import math


Return n factorial as an integer fa=math.factorial(4)
print(fa)

math.remainder(x, y) import math


Return the remainder of x with rem=math.remainder(4,2)
respect to y.
print(rem)
math.trunc(x) import math
Return x with the fractional tr=math.trunc(122.477448)
part removed, leaving the
print(tr)
integer part.

math.pow(x, y) import math


Return x raised to the power y power1=math.pow(2,3)
print(power1)

math.sqrt(x) import math


Return the square root of x. sq1=math.sqrt(12)
print(sq1)

1. import math
radius = 10
circleArea = math.pi * radius * radius
circleCircumference = math.pi * radius * 2
print(circleArea)
print(circleCircumference)

Where math.pi returns the value of pi.

2. import math
fa=math.factorial(46)
p=math.pow(2,3)
print(fa)
print(p)

What is a program library?


Answer: A pre-written and pre-tested programs that can be called in another program.
How do you use a program library in Python?
Answer: You import the library and then call the functions.
What are some examples of program libraries in Python?
Answer: random and math.
Why are program libraries useful?
Answer: They save time in writing new functions. They are also pre-tested so should already work.
Note : Explore Math and Random libraries and try the functions in them in your code.
More functions of Math Library https://docs.python.org/3/library/math.html

Python Data Types : Built-in Data Types

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.

You can display a string literal with the print() function:


Example
print("Hello")
print('Hello')
Booleans :
Booleans represent one of two values: True or False. In programming you often need to know if an
expression is True or False. You can evaluate any expression in Python, and get one of two
answers, True or False.
Boolean datatype is used as a ‘flag’. It is used to indicate if something has occurred, or is valid or correct. If
the flag is:

• true then it is positive, meaning that it has occurred, it is valid or it is correct


• false it is negative, meaning that it has not occurred, it is not valid or it is incorrect.

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:

• Any numeric-only values as being Integer or Real.


• Numbers that are not used in calculations, such as telephone numbers or credit card numbers, will
always be a string.
• If the number is never used in a calculation, it is a string.
• Integer data types cannot start with a 0. Any leading 0s will be removed. Therefore, any integer that
needs to have leading 0s, such as an ID number, will need to be stored as a string.
• In this table, if the ID number of the book was stored as an integer, it would be 1182738, which
might be the ID number of a different book.
What is a data type in programming?
Answer: data that is put into categories, so the program knows what is expected
What data types do you already know?
Answer: Integer, Real and String
What is the difference between an Integer and a Real number?
Answer: an Integer is a whole number, a Real number is a decimal number
What is a string?
Answer: one or more characters that can include letters, symbols and numbers that are not used in
mathematics calculations, such as telephone numbers or credit card numbers.
Iterative process to develop programs.
An iterative process is the process of writing part of a program, testing it and editing it, then adding
to the program, testing it and editing it, and then adding to it continually until a final, working,
program is produced.
What is iterative development?
Answer: Repeatedly editing and testing a program until it is complete.
Have you used iterative development before without knowing it?
Did you find iterative development useful?
How could iterative development be useful when you have a very large program to write?
Answer: The program can be split it into smaller parts, with those parts being worked on individually. New
features and parts can then be added to the program throughout the iterative process.
Know how to test algorithms using suitable data. Explain the need for using a range of test data. Know
how to develop and apply test plans
Why do we need to test programs?
Answer: to make sure that they work; to check there are no errors; to check that they give suitable
responses to unexpected input.
How do we test that a program works?
Answer: we use the program, for example by entering data and pressing buttons, then check whether it
does what is expected to meet its criteria.
Example:
a program that only works with some data and not with other data, for example:
number = int(input("Enter a number between 1 and 10 inclusive"))
if number >= 1 and number < 10:
print("Success")
else:
print("Wrong")
Demonstrate that testing this program with:

• 5 will output Success


• 15 will output Wrong
• 10 will output Wrong, so it will not work as expected.
Test plan to check the above program
Note : The second condition is incorrect. It should be number <= 10. If the user inputs 10, the program will
produce the wrong output.
Not every possibility can be tested. Instead, developers need to make a reasonable judgement about when
they can state that their program works.
Sometimes a program can be tested with a wide range of data but can still contain an error that has not
been thought of, such as in a gameplay situation when a character bumps into an object and can no longer
move.

Why did you test your program?


Answer: To make sure it works and to check whether it does what is expected to meet its criteria. Testing
also checks whether the program is able to give a suitable response to an unexpected input.
Why should you test a program using a range of data?
Answer: It may work with one input but not any other inputs.
Testing with a range of data also checks whether the program works in all reasonable cases.

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.

• OPERAND: It is the value on which the operator is applied.

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

print(10 + 20)

Python divides the operators in the following groups:

• Arithmetic operators: +,-,/,%,*


• Assignment operators: =, +=, -=, *=
• Comparison operators : >, <, ==, >=, <=
• Logical operators : AND , OR, NOT
Python Conditions and If statements
In Python, condition statements act depending on whether a given condition is true or false. You can
execute different blocks of codes depending on the outcome of a condition. Condition statements always
evaluate to either True or False.
There are three types of conditional statements.

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

password = input('Enter password ')

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)

Nested if-else statement

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.

Example: Find a greater number between two numbers

num1 = int(input('Enter first number '))


num2 = int(input('Enter second number '))
if num1 >= num2:
if num1 == num2:
print(num1, 'and', num2, 'are equal')
else:
print(num1, 'is greater than', num2)
else:
print(num1, 'is smaller than', num2)

What are the three component parts of a conditional statement?


Answer: the condition; the code to run if true; the code to run if not true
What is a conditional statement?
Answer: A question that has a true or false answer.
What is the symbol for a condition in a flowchart?

Answer: a diamond

What are the conditional operators that we can use?

Answer: <, >, =, !=, <=, >=

And:

The and keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b, AND if c is greater than a:

a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Or:

The or keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b, OR if a is greater than c:

a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")

You might also like