Introduction to Python
Grade 7
Topics to be covered in this chapter
Part A – Starting and Installing
python and Introduction to modes
of Python.
Part B – Syntax, Data types and
Elements of Python.
Part C – Operators in Python
Part D – Program practice.
Part A – Starting and Installing
python and Introduction to
modes of Python.
What is Python?
Python is a popular programming
language. It was created by Guido
van Rossum, and released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
What can Python do?
Python can be used on a server to
create web applications.
Python can be used alongside software
to create workflows.
Python can connect to database
systems. It can also read and modify
files.
Python can be used to handle big data
and perform complex mathematics.
Python can be used for rapid
prototyping, or for production-ready
software development.
Starting Python
IDLE Python
(INTEGRATED DEVELOPMENT
AND LEARNING ENVIRONMENT)
Python IDLE
There are two modes to work in
Python IDLE:
◦ Interactive mode
◦ Script mode
Modes of Python
Interactive Mode
Script Mode
Modes of Python
Script Mode
Part B – Syntax, Data types and
Elements of Python.
SYNTAX TO PRINT
print("Hello, World!")
OUTPUT
Hello, World!
How to run the program
Steps to Save a file
Do it yourself
1) Program to print your Name in Python
2) Program to print your Name and grade
in python.
DATA TYPES IN PYTHON
Numeric Types:
Integers – This value is represented by int
class. It contains positive or negative whole
numbers (without fractions or decimals). In
Python, there is no limit to how long an
integer value can be.
Float – This value is represented by the
float class. It is a real number with a
floating-point representation. It is specified
by a decimal point. Optionally, the
character e or E followed by a positive or
negative integer may be appended to
specify scientific notation.
Example of Numeric type
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
String Data Type
A string is a collection of one or more
characters put in a single quote,
double-quote.
Example
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
print(type(String1))
List Data Type
Lists are just like arrays, declared in other
languages which is an ordered collection of
data. It is very flexible as the items in a list
do not need to be of the same type.
Example
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple
values: ")
print(List[0])
print(List[2])
Keywords in Python
Every programming language has special
reserved words, or keywords, that have specific
meanings and restrictions around how they
should be used. Python is no different. Python
keywords are the fundamental building blocks of
any Python program.
Python Variables
Python Variable is containers that store
values.We do not need to declare
variables before using them or declare
their type.
A variable is created the moment we first
assign a value to it.
A Python variable is a name given to a
memory location. It is the basic unit of
storage in a program.
Rules for Python variables
A Python variable name must start with a letter
or the underscore character.
A Python variable name cannot start with a
number.
A Python variable name can only contain alpha-
numeric characters and underscores (A-z, 0-9,
and _ ).
Variable in Python names are case-sensitive
(name, Name, and NAME are three different
variables).
The reserved words(keywords) in Python cannot
be used to name the variable in Python.
Comments in Python
Comments can be used to explain Python code.
Comments can be used to make the code more
readable.
Comments can be used to prevent execution
when testing code.
Comments starts with a #, and Python will
ignore them.
Example
#This is a comment
print("Hello, World!")
Assignment Operator in Python
Assignment operators are used to
assign values to variables.
Example
x=5
x *= 3
print(x)
Programs Practice
Write a program that generates the
following output :
5
10
9
Assign value 5 to a variable using
assignment operator (=) Multiply it with 2
to generate 10 and subtract 1 to generate
9.
Solution:
a = 5 print(a)
a = a * 2 print(a)
a = a - 1 print(a)
Write the program with maximum
three lines of code and that assigns first
5 multiples of a number to 5 variables
and then print them.
Solution:
a = int(input("Enter a number: "))
b, c, d, e = a * 2, a * 3, a * 4, a * 5
print(a, b, c, d, e)
Write a Python program that accepts
radius of a circle and prints its area.
Solution
r = float(input("Enter radius of circle: "))
a = 3.14159 * r * r
print("Area of circle =", a)
Part C – Operators in Python
and Conditional Statement.
OPERATORS IN PYTHON
Arithmetic Operator
Relational or comparison Operator
Part D –
Program practice
Write Python program that accepts marks in 5
subjects and outputs average marks.
Solution
m1 = int(input("Enter first subject marks: "))
m2 = int(input("Enter second subject marks: "))
m3 = int(input("Enter third subject marks: "))
m4 = int(input("Enter fourth subject marks: "))
m5 = int(input("Enter fifth subject marks: "))
avg = (m1 + m2+ m3+ m4 + m5) / 5;
print("Average Marks =", avg)
Write a program to read a number n and print
n2, n3 and n4.
Solution
n = int(input("Enter n: "))
n2, n3, n4 = n ** 2, n ** 3, n ** 4
print("n =", n)
print("n^2 =", n2)
print("n^3 =", n3)
print("n^4 =", n4)
Write a program to find area of a triangle.
Solution
h = float(input("Enter height of the triangle: "))
b = float(input("Enter base of the triangle: "))
area = 0.5 * b * h
print("Area of triangle = ", area)
Write a program to input a number and print
its first five multiples.
Solution
n = int(input("Enter number: "))
print("First five multiples of", n, "are")
print(n, n * 2, n * 3, n * 4, n * 5)
Write a program to read details like name, class,
age of a student and then print the details firstly in
same line and then in separate lines. Make sure to
have two blank lines in these two different types
of prints.
Solution
n = input("Enter name of student: ")
c = int(input("Enter class of student: "))
a = int(input("Enter age of student: "))
print("Name:", n, "Class:", c, "Age:", a)
print("Name:", n)
print("Class:", c)
print("Age:", a)