0% found this document useful (0 votes)
1 views13 pages

Grade 7 Python programming

The document provides an overview of Python programming, including its features, programming modes, identifiers, operators, variables, comments, input/output functions, decision statements, and looping constructs. It also includes example programs and exercises to reinforce learning. Additionally, it discusses procedures in Python and MIT App Inventor, along with a series of questions to test understanding.

Uploaded by

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

Grade 7 Python programming

The document provides an overview of Python programming, including its features, programming modes, identifiers, operators, variables, comments, input/output functions, decision statements, and looping constructs. It also includes example programs and exercises to reinforce learning. Additionally, it discusses procedures in Python and MIT App Inventor, along with a series of questions to test understanding.

Uploaded by

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

Kids Gurukul International School, Jalgaon

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:

3. Select Run | Run Module F5 to execute the python script.

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:

Python Arithmetic Operators

Operator Name Example

+ Addition 10 + 20 = 30

- Subtraction 20 – 10 = 10

* Multiplication 10 * 20 = 200

/ Division 20 / 10 = 2

% Modulus 22 % 10 = 2

** Exponent 4**2 = 16

// Floor Division 9//2 = 4

Python Comparison Operators

Python comparison operators compare the values on either sides of them and decide the relation
among them. They are also called relational operators.

Operator Name Example

== Equal 4 == 5 is not true.

!= Not Equal 4 != 5 is true.

> Greater Than 4 > 5 is not true.

< Less Than 4 < 5 is true.


>= Greater than or Equal to 4 >= 5 is not true.

<= Less than or Equal to 4 <= 5 is true.

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.

counter = 100 # Creates an integer variable


miles = 1000.0 # Creates a floating point variable
name = "Zara Ali" # Creates a string variable
Python Comments:

 Python comments are programmer-readable explanation


 They are added with the purpose of making the source code easier for humans to
understand, and are ignored by Python interpreter.
 Comments enhance the readability of the code and help the programmers to understand
the code very carefully.

Single Line Comments

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.

# This is a single line comment in python

print ("Hello, World!")

Multiline Comments:
'''
This is a multiline
comment.
'''
print ("Hello, World!")

Input & Output Function in Python:

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.

The Message to be displayed is enclosed either in Single or double quotes.

print(‘Hello’)

or

print(“Hello”)

The contents of the variable can also be displayed as follows:

x = 10

print(x)

name = ”Kids”

print(name)

a = 10

b = 20

print("a = ",a, "b = ",b)

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.

name = input("Enter your Name")


print("Hello ", name)

The output displayed is –

=== RESTART: C:/Users/kidsguru5/AppData/Local/Programs/Python/Python311/p1.py ==

Enter your Name SHC

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.

#To accept an integer

x =int( input(“Enter any 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.

#To accept a floating point number

x = float( input(“Enter any Number:”))

print(x)

str() Function:
This function converts the integer or floating point value to a string.

Decision or Conditional Statements in Python:


If Statement:

a = 33

b = 200

if b > a:

print("b is greater than a")

If .. Else Statement:

a = 333

b = 200

if b > a:

print("b is greater than a")

else:

print("b is not greater than a")

Python Programs:

1) Program to accept two numbers and display the sum.


a = int(input("Enter First No."))
b = int(input("Enter Second No."))
c = a+b
print("Sum is ",c)
2) Program to accept two numbers and display the maximum.
a = int(input("Enter First No."))
b = int(input("Enter Second No."))
if a > b:
print("Max is ",a)
else:
print("Max is ",b)

3) Program to accept a number and display whether it is Odd or Even Number.

a = int(input("Enter Any No."))


if a % 2 == 0:
print("No. is Even")
else:
print("No. is Odd")

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:

 A function is a block of code which only runs when it is called.


 You can pass data, known as parameters, into a function.
 A function can return data as a result.

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 in MIT App Inventor:


Procedure:
 A procedure is a sequence of blocks or code that is stored under a name.
 The procedure when called executes the block i.e. the sequence of blocks to run.
 In computer science, a procedure also might be called a function or a method.
 It permits reusability of code.
 A procedure once defined can be called any number of times.

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

It is same as a procedure do block, but calling this procedure returns a result.

Q1. Select the most appropriate option:

1. A variable helps us _________ and can be ________


a) store information b) overwritten c) used only once in the program
2. Two examples of data types used in Python are ______ and _____.
a) integer b) decimal c) float
3. If b = 33.33, then the data type of b is ____________.
a) type casting b) float c) assignment
4. In the python code x = 15, the ‘=’ sign is called a/an ______ operator.
a) type casting b) float c) assignment
5. __________ is the process of converting the data type of a variable or data.
a) type casting b) float c) assignment
6. Which of the following is the correct to get ‘Hello World’ as the output?
a) Output(“Hello World”) b) show(“Hello World”) c) print(“Hello World”)
7. Which is the correct way to create a variable with numeric value 10?
a) x = 10 b) x = int(10) c) Both a and b
8. Which of the following is the file extension for python files?
a) pyt b) py c) python
9. Which function do we use to convert a ‘string‘into a number?
a) str() b) int() c) upper() d) float()
10. Which function do we use to convert integer into a decimal point number?
a) int() b) decpoint() c) float() d) ()
11. What would be the output if we convert ‘4.9’ into float?
a) ‘4.9’ b) 4.9 c) 4 d) 9
12. What would be the output of the given Python code?
y = int(2.8)
print(y)
a) 2.8 b) 2.6 c) 2 d) 3
13. What would be the output of the given Python code?
w = float(“4.2”)
print(w)
a) 4.2 b) 4.5 c) 4 d) four
14. What would be the output of the given Python code?
z = str(“s1”)
a) s1 b) 1 c) “s1” d) s

Q2. State True or False

1. A conditional statement can have only two possible results. True


2. Functions allow us a call a set of instructions by different names. False
3. The index of the first letter in the string “LEADS” is 0. True
4. upper() is used to convert text to uppercase in Python. True
5. The ** operator returns the multiplication of two numbers. False

Q3. Fill in the blanks

sensor, arguments, procedure, string, len(), str(), clock

1. ________ values in Python are surrounded by single or double quotation marks.


2. The _________ function is used to find the length of a string.
3. A ________ is a component used for detecting changes in an environment and converting
them into electronic signals.
4. We can use a sequence of blocks repeatedly by calling the ________
5. Procedures may need inputs to return results. Such inputs are called _______

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy