Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

grade 7 python

The document provides an overview of Python programming, covering variables, data types, and operators. It explains how to create variables, the rules for naming them, and various built-in data types such as numeric, string, list, tuple, boolean, set, and dictionary. Additionally, it details different types of operators including arithmetic, comparison, assignment, logical, and their usage with examples.

Uploaded by

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

grade 7 python

The document provides an overview of Python programming, covering variables, data types, and operators. It explains how to create variables, the rules for naming them, and various built-in data types such as numeric, string, list, tuple, boolean, set, and dictionary. Additionally, it details different types of operators including arithmetic, comparison, assignment, logical, and their usage with examples.

Uploaded by

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

1

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.

Rules for Python variables:


a) A variable name must start with a letter or the underscore character
b) A variable name cannot start with a number
c) A Python keyword cannot be used as a variable name.
d) A variable name can only contain alpha-numeric characters and
Underscores (A-z, 0-9, and _)
e) Variable names are case-sensitive (age, Age and AGE are three different variables)
Values are assigned to variables using assignment operator (=). For example, the statement x=25
assigns the value 25 to the variable x.

Observe the following codes to understand the use of assignment operators:


a=5 #Value 5 is assigned to the variable a
b=10 #Value 10 is assigned to the variable b
c=a*b #The numbers are multiplied and the value is assigned to the variable c,
i.e., 50 is assigned to the variable c.

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).

1. Numeric Data Types in Python


The numeric data type in Python represents the data that has a numeric value. A numeric value
can be an integer, a floating number, or even a complex number.

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.

c) Complex Numbers – A complex number is represented by a complex class. It is specified


as (real part) + (imaginary part) j . For example – 2+3j

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.

name = 'Python' Python

print(name) Python for grade 7

message = 'Python for grade 7'

print(message)

b) List:

Lists are used to store multiple items in a single variable

list1 = [1, "hi", "Python", 2] <class 'list'>


#Checking type of given list
print(type(list1)) [1, 'hi', 'Python', 2]
#Printing the list1
print (list1)

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.

tup = ("hi", "Python", 2) <class 'tuple'>


# Checking type of tup ('hi', 'Python', 2)
print (type(tup))
#Printing the tuple
print (tup)

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.

a = True <class 'bool'>


print(type(a))
# Assigning Boolean values Is it raining? True
is_raining = True Is it sunny? False
is_sunny = False

# Directly using Boolean values in print statements


print("Is it raining?", is_raining)
print("Is it sunny?", is_sunny)

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.

# Creating set {2, 3, 'Python', 'James'}


set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)

5. Dictionary
Python dictionary is an ordered collection of items. It stores elements in key/value pairs.

# create a dictionary named capital_city {'Italy': 'Rome', 'England': 'London'}


capital_city = {'Italy': 'Rome', 'England': 'London'}
print(capital_city)

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.

 OPERATORS: These are the special symbols. Eg- + , * , /, etc.


 OPERAND: It is the value 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.

Operator Syntax Description

+ (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 =

** r=a**b As this operator calculates the first operand's power to the


(Exponent) second operand, it is an exponent operator. For example, if a =
2, b = 3 => a**b = 2**3 = 2^3 = 2*2*2 = 8

// (Floor r = a // b This operator provides the quotient's floor value, which is


division) obtained by dividing the two operands. For example, if a = 15,
b = 4 => a // b = 15 // 4 = 3

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

print ('Division: ', a / b) # division

print ('Floor Division: ', a // b) # floor division

print ('Modulo: ', a % b) # modulo

print ('Power: ', a ** b) # a to the power b

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.

Operator Syntax Description

== 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

# Greater than (>)


print(a > b) # False, because 10 is not greater than 20

# Less than (<)


print(a < b) # True, because 10 is less than 20

# Greater than or equal to (>=)


print(a >= b) # False, because 10 is not greater than or equal to 20

# Less than or equal to (<=)


print(a <= b) # True, because 10 is less than or 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.

Operator Syntax Description

= 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

Exponent AND: Calculate exponent(raise power) value using


**= a**=b a=a**b
operands and assign value 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.

Operator Syntax Description

and x and y Logical AND: True if both the operands are true

or x or y Logical OR: True if either of the operands is true

not not x Logical NOT: True if the operand is false

Precedence of Logical Operators in Python

The precedence of Logical Operators in Python is as follows:


1. Logical not
2. logical and
3. logical or

Example:
a = True False
b = False True
print(a and b) False
print(a or b)
print(not a)

Conditional statement in python

In Python, If-Else is a fundamental conditional statement used for decision-making in programming.


If…Else statement allows to execution of specific blocks of code depending on the condition is True
or False.

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

You might also like