Python 11
Python 11
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:
Int()
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.
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.
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