0% found this document useful (0 votes)
4 views24 pages

Language Component & Loop

The document provides an overview of various programming concepts in Python, including arithmetic operations, operator precedence, and control flow structures like if statements and loops. It covers the use of logical, comparison, and assignment operators, as well as the implementation of functions and modules for mathematical tasks. Additionally, it includes examples for while loops, for loops, and nested loops, demonstrating how to iterate over sequences and perform calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Language Component & Loop

The document provides an overview of various programming concepts in Python, including arithmetic operations, operator precedence, and control flow structures like if statements and loops. It covers the use of logical, comparison, and assignment operators, as well as the implementation of functions and modules for mathematical tasks. Additionally, it includes examples for while loops, for loops, and nested loops, demonstrating how to iterate over sequences and perform calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Language Component & Loop

Agenda
Arithmetic Operations
Operator Precedence
Math Functions
Indentation
If Statements
Logical Operators
Comparison/Relational/Conditional Operators
Assignment Operators
Ternary Operators
While Loops
Break & Continue Statement
Sum of n Numbers Program
For Loops
For-While Comparison
For with Range Function
Nested Loops
Arithmetic Operations
Arithmetic Operations

Example

val1 = 3
val2 = 2

print(val1 + val2)
print(val1 - val2)
print(val1 * val2)
print(val1 / val2)
print(val1 // val2)
print(val1 % val2)
print(val1 ** val2)
Operator Precedence

Operators Meaning

() Parentheses
** Exponent

Multiplication, Division,
*, /, //, %
Floor division, Modulus
+, - Addition, Subtraction

print(10+3*2**2+45)
Math Functions/Module
Python math Functions
Python Math functions is one of the most used functions in Python Programming. In python there are
different built-in math functions. Beside there is also a math module in python.

x=2.9
print(round(x))
print(abs(-2.9))

Python math Module


Python has a built-in module that you can use for mathematical tasks. The math module has a set of
methods and constants.

import math

x=2.9
print([Link](x))
print([Link](x))

[Link]
Python Indentation

Python Indentation
Indentation refers to the spaces at the beginning of a code line.

if 5 > 2:
print("Five is greater than two!")
If Statements
if statement
if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of
statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

if If……else If……elif……else
If Statements
Example

if If……else If……elif……else
i = 10 i = 20 i = 20
if i < 15: if i == 10:
if i > 15: print("i is smaller than 15") print("i is 10")
print("10 is less than 15") print("i'm in if Block") elif i == 15:
print("I am Not in if") else: print("i is 15")
print("i is greater than 15") elif i == 20:
print("i'm in else Block") print("i is 20")
print("i'm not in if and not in else Block") else:
print("i is not present")
Comparison/Relational/Conditional Operators
Comparison/Relational/Conditional Operators

Example:
a=9
b=5

print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
Logical Operators

and
a = 10
b = 10
c = -10

if a > 0 and b > 0:


print("The numbers are greater than 0")

if a > 0 and b > 0 and c > 0:


print("The numbers are greater than 0")
else:
print("Atleast one number is not greater than 0")
Logical Operators

or not
a = 10 a = 10
b = -10
c=0 if not a%3 == 0 or a%5 == 0:
print("10 is not divisible by either 3 or 5")
if a > 0 or b > 0: else:
print("Either of the number is greater than 0") print("10 is divisible by either 3 or 5")
else:
print("No number is greater than 0")

if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
Assignment Operators
Ternary Operators

num1=20
num2=15

if num1>num2:
print(num1)
else:
print(num2)

print(num1 if num1>num2 else num2)


Numeric Data Types
Python Number Types: int, float, complex
Complex
A complex number is a number with real and imaginary components. For example, 5 + 6j is a complex number
where 5 is the real component and 6 multiplied by j is an imaginary component.
Complex data types is used while developing scientific applications where complex mathematical operation is
required.

a=6+7j
print(a)
print([Link])
print([Link])
print(type(a))

b=5+5j
print(a+b)
While Loops
A while loop statement in Python programming language repeatedly executes a target statement as
long as a given condition is true.

count = 0
while count < 9:
print('The count is:', count)
count = count + 1

print("Good bye!")
Break & Continue Statement

Break Continue
i=1 i=0
while i < 6: while i < 6:
print(i) i += 1
if i == 3: if i == 3:
break continue
i += 1 print(i)
Sum of n Numbers Program

n = int(input("Enter the n Number:"))


sum = 0
i=1

while i <= n:
sum = sum + i
i = i +1

print(sum)
For Loops
For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
For Loops

#Looping Through a String #Looping Through a list #The break Statement


#Exit the loop when x is "banana"
for x in "banana": fruits = ["apple", "banana", "cherry"]
print(x) for x in fruits: fruits = ["apple", "banana", "cherry"]
print(x) for x in fruits:
print(x)
if x == "banana":
break
#The continue Statement
#Do not print banana:

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
continue
print(x)
For-While Comparison
while for
num = [10, 20, 30, 40, 50] num = [10, 20, 30, 40, 50]
index = 0 for x in num:
n = len(num) print(x)
while index < n:
print(num[index])
index = index+1
For with Range Function
The range() Function
To loop through a set of code a specified number of times, we can use the range() function,

#Using the range() function: #Using the start parameter: #Increment the sequence with 3 (default is 1):

for x in range(6): for x in range(2, 6): for x in range(2, 30, 3):


print(x) print(x) print(x)
Nested Loops

adj = ["red", "big", "tasty"]


fruits = ["apple", "banana", "cherry"]

for x in adj:
for y in fruits:
print(x, y)

You might also like