0% found this document useful (0 votes)
5 views22 pages

3.Phython

Uploaded by

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

3.Phython

Uploaded by

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

Starting Python with IDLE

IDLE appear to be identical

>>> 2 + 2
Type and Check the Result
IDLE editor
Variables
You want to give a value a name

Assign a value to a name using =

 a = 123

 b = 12.34

 c = "Hello"

 d = 'Hello'

 e = True and False, and are case-sensitive.


Reading User Input

Use keyword input.

>>> x = input("Enter Value:")

Enter Value:23

>>> print(x)

23
Arithmetic

Arithmetic operators are +, -, *, and /


ORDER OF OPERATIONS : BODMAS or PEDAS

Write a program to solve the equation


Y=(X + 10 - 3) x 7 / 4

>>> x = input("Enter the value of x=: ")

Enter the value of x=: 10

>>> y = (int(x) +10-3) * 7 /4

>>> print(y)

29.75
Arithmetic

 % (modulo remainder)
 ** (raise to the power of)

>>> 10% 2

>>> 10% 3

>>> 4**2

>>> 9**2
Escape Characters

For tab \t

Newline \n

Name Age

Rama 14

>>> s = “Name\t Age \n Rama \t 14"

>>> print(s)
Running Commands Conditionally

if else and nested elif

EX: Print the message x is big only if x has


a value greater than 100

x=input("enter x value:")

if int (x)>100:

print("X is greater than 100")

else:

print("X is less than 100")


Comparing Values

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

== Exactly equal to

!= Not equal to
Compare the x value with 100 and print
corresponding message.

x = input("enter x value:")

if int (x)>100:

print("X is greater than 100")

elif int(x)==100:

print ("X is = 100")

else :

print("X is less than 100")


Type and Test in Idle

>>> 1 != 2 >>> 10 == 10
True True

>>> 1 != 1 >>> 'aa' < 'ab'


True
False
>>> 'aaa' < 'aa'
>>> 10 >= 10
False
True

>>> 10 >= 11

False
Logical Operators
The logical operators:

Example: you may want to check whether a variable x


has a value between 10 and 20.

x=input("enter x value:")

if int (x)>=10 and int(x)<=20:

print("X is in between 10 and 20")

else :

print("X is not in-between 10 and 20")


Repeating Instructions an Exact
Number of Times

You need to repeat some program code an exact


number of times.

For example: Print BASU 10 times

>>> for i in range(1, 11):

print(‘BASU’)
Repeating Instructions Until Some Condition
Changes

 The while statement repeats its nested commands


until its condition becomes false.
Count=0

while (count<9):

print('the count is:', count)

count = count+1
Breaking Out of a Loop

Use the Python break statement


to exit either a while or for loop.

>>> while True:


answer = input('Enter command:')
if answer == 'X':
break
Breaking Out of a Loop

One more Example to use of Break

var = 10
while var > 0:
print ('Current variable value :', var)
var = var -1
if var == 5:
break
Defining a Function in Python

Why function is required in programming?


 To avoid repeating the same code over and
over in a program
 Function created using the def command

Example
def say_hello():# block belonging to the function
print('hello world') # End of function

say_hello() # call the function


say_hello() # call the function again
Function in Python
def print_max(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')

x = input("enter X value:")
y = input("enter y value)
print_max (x, y) # pass variables as arguments
Function in Python
• The return statement is used to return from a function i.e. break
out of the function.
def print_max (a, b):
if a > b:
return a
elif a == b:
return " x and y are equal"
else:
return b
while True:
x = input("enter X value:”)
y = input ("enter y value")
print(print_max (x, y))
THANK YOU

You might also like