Grade 7 Python programming
Grade 7 Python programming
Python:
It is a programming language.
It is a higher level language. (Program written in English like language)
It is an interpreted language.
It was developed by Guido van Rossum in 1991.
It is interactive and object-oriented language.
Note:
Python Once installed. Type IDLE (Integrated Development and Learning Environment)
in the windows search box.
Programming Modes:
1) Interactive Mode:
The user types Python commands at the command prompt as shown in the following screens –
Python immediately displays the result of the command. The python command prompt
can also be used as a calculator.
The problem with this mode is one needs to type the command every time to get the
result.
2) Scripting Mode:
A script is a small program. It is saved as a file on secondary storage with an extension
(.py). The steps are as follows:
1. Select File | New File
2. Type the required python code and Select Save option from File Menu option as
shown below:
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object.
An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers.
Types of Python Operators:
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
Python comparison operators compare the values on either sides of them and decide the relation
among them. They are also called relational operators.
Python Variables:
Python variables are the reserved memory locations used to store values with in a Python
Program.
Based on the data type of a variable, Python interpreter allocates memory and decides what can
be stored in the reserved memory. Therefore, by assigning different data types to Python
variables, one can store integers, decimals or characters in these variables.
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and
up to the end of the physical line are part of the comment and the Python interpreter ignores
them.
Multiline Comments:
'''
This is a multiline
comment.
'''
print ("Hello, World!")
Output Operation:
The print() function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before
written to the screen.
print(‘Hello’)
or
print(“Hello”)
x = 10
print(x)
name = ”Kids”
print(name)
a = 10
b = 20
Input Operation:
The input() function is used to accept a single input from the user. The accepted data can be
stored in a variable and can use it in the program wherever we need it. The following code
demonstrates it.
Hello SHC
Note:
The input() function by default returns the accepted data as a string. However to convert string,
python provides built-in functions to convert string into integer and float values.
int() Function: The int() function converts the specified value into an integer number.
print(x)
float() Function:
Floating point numbers are decimal values or fractional numbers like 133.5, 2897.11, and
3571.213, whereas real numbers like 56, 2, and 33 are called integers.
The float() function converts the specified value into a floating point number.
print(x)
str() Function:
This function converts the integer or floating point value to a string.
a = 33
b = 200
if b > a:
If .. Else Statement:
a = 333
b = 200
if b > a:
else:
Python Programs:
Looping In Python:
1) While Loop:
With the while loop one can execute a set of statements as long as a condition is true.
Print i as long as i is less than 6:
i=1
while i < 6:
print(i)
i += 1
2) For Loop:
To loop through a set of code a specified number of times, One can use the range()
function,
The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
for x in range(6):
print(x)
Note that range (6) is not the values of 0 to 6, but the values 0 to 5.
Procedure in Python:
Example:
#Defining a Function
def my_function():
print("Hello from a function")
#Calling a Function
my_function()
Arguments:
Information can be passed into functions as arguments.
def my_function(fname):
print( "Hello From " + fname)
my_function("KGIS")
procedure do:
It collects a sequence of blocks together into a group. one can then use the sequence of blocks
repeatedly by calling the procedure. If the procedure has arguments, one can specify the
arguments by using the block’s mutator button.
When you create a procedure, App Inventor automatically generates a call block and places it in
the Procedures drawer.
procedure result