grade 7 python
grade 7 python
More to Python
Variables:
Variables are containers for storing data values.
A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type and can even change type after
they have been set.
REGISTER WORK:
a) Create a variable called age and set its value to 10. Print the value of age.
b) Create two variables, x and y, and assign the values 5 and 8 to them, respectively. Add
the values of x and y together and print the result.
2
Data Types in Python:
Data types in Python refer to classifying or categorizing data objects based on their characteristics
and behavior. They determine the type of values variables can hold and specify the operations that
can be performed on those values. For instance, Python has several built-in data types, including
numeric types (int, float, complex), string (str), boolean (bool), and collection types (list, tuple,
dict, set).
a) 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.
b) 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:
num1 = 5 5 is of type <class 'int'>
print(num1, 'is of type', type(num1)) 2.0 is of type <class 'float'>
(1+2j) is of type <class 'complex'>
num2 = 2.0
print(num2, 'is of type', type(num2))
num3 = 1+2j
print(num3, 'is of type', type(num3))
3
2. Sequence Data Types in Python
The sequence Data Type in Python is the ordered collection of similar or different Python data
types. Sequences allow storing of multiple values in an organized and efficient fashion. There
are several sequence data types of Python:
a) String
A string is a sequence of characters. Python treats anything inside quotes as a string. This
includes letters, numbers, and symbols.
print(message)
b) List:
c) Tuple
In many ways, a tuple is like a list. Tuples, like lists, also contain a collection of items from
various data types. A parenthetical space () separates the tuple's components from one another.
4
3. Boolean
True and False are the two default values for the Boolean type. These qualities are utilized to decide
the given assertion valid or misleading. The class book indicates this. False can be represented by
the 0 or the letter "F," while true can be represented by any value that is not zero.
4. Set
The data type's unordered collection is Python Set. It is iterable, mutable(can change after creation),
and has remarkable components. Either a sequence of elements is passed through the curly braces
and separated by a comma to create the set or the built-in function set() is used to create the set. It
can contain different kinds of values.
5. Dictionary
Python dictionary is an ordered collection of items. It stores elements in key/value pairs.
5
How to declare the data type:
Operators in Python
Operators are the symbols used to perform a specific operation on different values and variables.
These values and variables are considered as the Operands, on which the operator is applied.
Types of Operators:
Same as other languages, Python also has some operators, and these are given below -
1. Arithmetic Operators
2. Comparison Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Let us now discuss these operators used in Python in the following sections.
6
Arithmetic Operators
Python Arithmetic Operators are used on two operands to perform basic mathematical operators like
addition, subtraction, multiplication, and division. There are different types of arithmetic operators
available in Python including the '+' operator for addition, '-' operator for subtraction, '*' for multiplication,
'/' for division, '%' for modulus, '**' for exponent and '//' for floor division.
Let us consider the following table of arithmetic operators for a detailed explanation.
+ (Addition) r=a+b This operator is used to add two operands. For example, if a =
15, b = 10 => a + b = 15 + 10 = 25
- r=a-b This operator is used to subtract the second operand from the
(Subtraction) first operand. If the first operand is less than the second
operand, the value results negative. For example, if a = 20, b =
5 => a - b = 20 – 5 = 15
/ (divide) r=a/b This operator returns the quotient after dividing the first
operand by the second operand. For example, if a = 15, b = 4
=> a / b = 15 / 4 = 3.75
* r=a*b This operator is used to multiply one operand with the other.
(Multiplicatio For example, if a = 20, b = 4 => a * b = 20 * 4 = 80
n)
% (reminder) r=a%b This operator returns the reminder after dividing the first
operand by the second operand. For example, if a = 20, b = 10
=> a % b = 20 % 10 =
7
Example:
a=7 Sum: 9
b=2 Subtraction: 5
print ('Sum: ', a + b) # addition Multiplication: 14
Division: 3.5
print ('Subtraction: ', a - b) # subtraction Floor Division: 3
Modulo: 1
print ('Multiplication: ', a * b) # multiplication Power: 49
REGISTER WORK:
a) Write a Python program that takes input from user and divides two numbers and gives the
quotient (without the remainder) using the floor division operator.
Comparison Operators
Python Comparison operators are mainly used for the purpose of comparing two values or variables
(operands) and return a Boolean value as either True or False accordingly. There are various types of
comparison operators available in Python including the '==', '!=', '<=', '>=', '<', and '>'.
Let us consider the following table of comparison operators for a detailed explanation.
== a == b Equal to: If the value of two operands is equal, then the condition
becomes true.
!= a != b Not Equal to: If the value of two operands is not equal, then the
condition becomes true.
<= a <= b Less than or Equal to: The condition is met if the first operand is
smaller than or equal to the second operand.
>= a >= b Greater than or Equal to: The condition is met if the first operand is
greater than or equal to the second operand.
> a>b Greater than: If the first operand is greater than the second operand,
then the condition becomes true.
< a<b Less than: If the first operand is less than the second operand, then the
condition becomes true.
8
Example:
a = 10 False
b = 20 True
# Equal to (==) False
print(a == b) # False, because 10 is not equal to 20 True
False
# Not equal to (!=) True
print(a != b) # True, because 10 is not equal to 20
Assignment operator:
Assignment operator that helps to assign values or expressions to the left-hand-side variable. The
assignment operator is represented as the "=" symbol used in assignment expressions. In the assignment
operator, the right-hand side value or operand is assigned to the left-hand operand.
= x=y+z Assign value of the right side of expression to the left side operand
Add AND: Add right-side operand with left-side operand and then
+= a+=b a=a+b
assign to left operand
Subtract AND: Subtract right operand from left operand and then
-= a-=b a=a-b
assign to left operand
Multiply AND: Multiply right operand with left operand and then
*= a*=b a=a*b
assign to left operand
9
Operator Syntax Description
Divide AND: Divide left operand with right operand and then assign
/= a/=b a=a/b
to left operand
Modulus AND: Takes modulus using left and right operands and
%= a%=b a=a%b
assign the result to left operand
Divide(floor) AND: Divide left operand with right operand and then
//= a//=b a=a//b
assign the value(floor) to left operand
Example:
x = 10 y = 10
# Assignment (=) x after += 5: 15
y = x # Assign the value of x to y x after -= 3: 12
print("y =", y) x after *= 2: 24
x after /= 4: 6.0
# Subtract and assign (-=) x after %= 4: 2.0
x -= 3 # Same as x = x - 3 x after //= 2: 1.0
print("x after -= 3:", x) x after **= 3: 1.0
# Multiply and assign (*=)
x *= 2 # Same as x = x * 2
print("x after *= 2:", x)
# Divide and assign (/=)
x /= 4 # Same as x = x / 4
print("x after /= 4:", x)
# Modulo and assign (%=)
x %= 4 # Same as x = x % 4
print("x after %= 4:", x)
# Floor divide and assign (//=)
x //= 2 # Same as x = x // 2
print("x after //= 2:", x)
# Exponentiation and assign (**=)
x **= 3 # Same as x = x ** 3
print("x after **= 3:", x)
10
Logical Operators
The assessment of expressions to make decisions typically uses logical operators. Python offers different
types of logical operators such as and, or, and not.
and x and y Logical AND: True if both the operands are true
Example:
a = True False
b = False True
print(a and b) False
print(a or b)
print(not a)
if Statement
if statement is the most simple decision-making statement. If the condition evaluates to True, the
block of code inside the if statement is executed.
11
Example:
i = 10 I am Not in if
# Checking if i is greater than 15
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
if….else Statement
if…else statement is a control statement that helps in decision-making based on specific conditions.
When the if condition is False. If the condition in the if statement is not true, the else block will be
executed.
i = 20 i is positive
# Checking if i is greater than 0
if (i > 0):
print("i is positive")
else:
print("i is 0 or Negative")
12
Using Logical Operators with If..Else:
We can combine multiple conditions using logical operators such as and, or, and not.
age = 25 Eligible.
experience = 10
# Using > & 'and' with IF..ELSE
if age > 23 and experience > 8:
print("Eligible.")
else:
print("Not eligible.")
Worksheet
Write a Python program that checks if two numbers are both greater than 10 using the and logical
operator.
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
13
Write a python program to check if a given year is a leap year.
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
Write a python program taking user input to calculate simple interest using the given formula
𝑷𝒙𝑹𝒙𝑻
𝒔𝒊𝒎𝒑𝒍𝒆 𝒊𝒏𝒕𝒆𝒓𝒆𝒔𝒕 =
𝟏𝟎𝟎
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
14