0% found this document useful (0 votes)
7 views

Python 11

Uploaded by

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

Python 11

Uploaded by

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

PYTHON

CLASS- XI- INFORMATION PRACTICES


AND COMPUTER SCIENCE
INPUT()
Python provides us the facility to take input from user using INPUT()
function. This function prompts the user to input value.
FOR EXAMPLE:
Name=input(“enter your name”);
Print(Name);

-Output will be the name as entered by the user.


PYTHON
Let us take the example:

Name=input(“enter your name:”);


Print(“Welcome Mr.”,Name);

Here suppose the user enter the name as “Mayank Grover” so the
variable name will contain Mayank Grover. In such case the output of
the program will be:

OUTPUT: Welcome Mr. Mayank Grover


PYTHON
Note that the INPUT() accepts the input only in string format. So, if any
mathematical calculation is to be performed, we need to convert the
input to integer format. For converting the input to integer format
Python provides us a function called:

Int()

Lets discuss this function:


PYTHON
• PROGRAM WITHOUT INT() FUNCTION: • PROGRAM USING INT() FUNCTION
A=input(“enter 1st number:”); A=int(input(“enter 1st number:”));
B=input(“enter 2nd number:”); B=int(input(“enter 2nd number:”));
C=A+B; C=A+B;
Print(“Addition=“,c); Print(“Addition=“,c);
Note: Note:
In this case if the value of A and B as In this case if the value of A and B given
given by user is 5 and 6 respectively by user is 5 and 6 then the output will
then the output will be 56. As 5 and 6 be 11 as we have converted the input
will be treated as string and not integer from string to integer format
PYTHON OPERATORS
Operators are those who perform specific mathematical or logical
operations on values. The values that the operator work on are called
OPERANDS.
For example: the expression a + b, the “a” and “b” OPERANDS and “+” is
OPERATOR.
The various operators are categories into:
- Arithmetic Operator
- Comparison/Relational Operator
- Logical
- Identity
- Membership
- Bitwise
ARITHMETIC OPERATORS IN
PYTHON
RELATIONAL/COMPARISON
OPERATOR
EXAMPLE
ASSIGNMENT OPERATOR
LOGICAL OPERATOR
LOGICAL OPERATOR

“and” logical operator:


Let a=5, b=10, c=8
And we have to find max between all three. So the logic will be:
• ((a>b) and (a>c)) if it is so then “a” is max.
• ((b>a) and (b>c)) if it is so then “b” is max.
• ((c>a) and (c>b)) if it is so then “c” is max.
LOGICAL OPERATOR
“or” logical operator:
Suppose we have to find whether a time is working hour or not. Then in
this condition:

If, (time<9:30am) or (time<6pm) or (day=“Sunday”)


In such condition, the time wont be working hour otherwise working
hour.
LOGICAL OPERATOR
“not” logical operator:
Not operator just reverse the final output. This means that if the
evaluation of some condition is true and not operator makes its false
and if the final evaluation is false then not operator makes it true.

For example:
Suppose a=5 and b=10;
(a>b)=false
Not(a>b)=true.
PROGRAM ON LOGICAL OPERATOR
Area of triangle using heroes formula:
LOOPING STATEMENT
Looping statement is a control statement which keeps on executing a
single statement or a block of statement “N” times till the condition is
true. Once the condition become false the execution is terminated.

1) While statement 2) For loop


WHILE LOOP
While loop is a control statement which keeps on executing a single
statement or a block or multiple statement “n” times till the condition
is true. Once condition becomes false the execution is terminated
Every looping statement has three parts:
a) Initialization (starting part)
b) Condition (true)
c) Increment/Decrement (increase or decrease)
If any one statement among the above three statement is missing then
the loop will not execute.
CONTROL STATEMENTS IN
PYTHON
STRING HANDLING
• String in python are stored as individual characters i.e. indexing wise. The
index default start with 0.

So, here P will be represented by str[0] or str[-6], Y will be represented by


either str[1] or [-5] and so on.
STRING
NOTE:
String are immutable and hence you can not change the individual
letter at string assignment operator.

For example:
name=“shyam”
name[0]=“A”
//result to an error:
type error: “name” object does not support item assignment
INTIALIZING AND PRINTING A
STRING:
• Example 1:
name=“Shyam”
print(name) output: Shyam

• Example 2:
name=“Shyam”
for i in name:
print (i) OR print(I, end=“”)
STRING OPERATORS
1. CONCATENATION OPERATOR (+)
The + operator concatenation two different strings.
Like “Every”+ “day” turns to Everyday
X=“every”+”day”
Print(x)
Output:everyday
2. Replication Operator (*)
The * operator requires two types of operands – a string and a number. It replicates
the given string to given number of times.
3*”hello”
Outpot: hellohellohello
3. Membership Operator (in/not in)
The syntax is: <string> in <string>
<string> not in <string>
Example: “a” in “ram” =will output true
“a” not in “ram” =will output false ASCII- AMERICAN STANDARD CODE
FOR INFORMATION INTERCHANGE
4. Comparison Operators
A=65 a=97
All relational operators (>,<,>=,<=,==,!=) apply to stings also. B=66 b=98
For example: NOTE: Continue with the serial
numbers in both small and capital
Comparison Output character.
“a”==“a” True
“ram”==“ram” True
“a”!=“ram” True
“a”!=“A” True
“ram”==“Ram” False
“ram”==“RAM” False
STRING SLICING
• String slicing returns the characters falling between indices n and m:
• Staring at n, n+1, n+2…. Till m-1. The syntax is:
String[start:end:step_value]
Example:
1. a=“hello world”
print(a[4:-2])
Output: “o war”
2. a="hello world"
print(a[6:],a[:6])
Output: world hello
STRING SLICING

WORL

WORLD

LO_WOR

HELLO
TUPLE
A tuple is a collection which is ordered and unchangeable. In python
tuples are written with round brackets. Tuple in python are very similar
to list which contain different types of element with the following
major differences

It is declared as
tuple1=(1,2,3,”ram”,”shyam”)
• Difference 1
The list is created using square brackets whereas tuples are
created using round brackets.

list1=[1,2,3,”ram”,”shyam”]
tuple1=[1,2,3,”ram”,”shyam”]
• Difference 2
The list is mutable whereas tuples are immutable.
list1=[1,2,3,”ram”,”shyam”]
list[2]=10
print(list1)
output: 1,2,10,ram,shyam
tuple1=(1,2,3,”ram”,”shyam”)
tuple[2]=10
print(tuple1)
output: error
• Difference 3
List occupies more memory space as compared that tuple.
Program Implementation:
Difference 4
List takes more time to execute as compared to tuple.
DICTIONARY
A dictionary is a collection which is unordered, changeable and
indexed. In python dictionary are written in curly brackets, and they
have keys and values. Means the dictionary contains two thing first is
the key and the second is the value.

Create and print a dictionary:


dict1={“company”:Suzuki”,”model”:dzire”,”year”:2020}
print(dict1)
Accessing items
• You can access the items of a dictionary by referring to its key name,
inside square brackets.

dict1={“company”:Suzuki”,”model”:dzire”,”year”:2020}
print(dict1)
x=dict1[“model”]
print(x)
dict1={“company”:Suzuki”,”model”:dzire”,”year”:2020}
for x in dict1:
print(dict1[x])
This will print the values

We can use values() function for print the values

You might also like