0% found this document useful (0 votes)
19 views13 pages

Mid Questions With Answers

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

Mid Questions With Answers

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

1.

Illustrate various conditional statements used in


Python programming.
Decision making is the most important aspect of
almost all the programming languages. As the
name implies, decision making allows us to run a
particular block of code for a particular decision.
Here, the decisions are made on the validity of the
particular conditions. Condition checking is the
backbone of decision making.
In python, decision making is performed by the
following statements.
Statement Description

If The if statement is used to test a specific


Statement condition. If the condition is true, a
block of code (if-block) will be executed.
If - else The if-else statement is similar to if
Statement statement except the fact that, it also
provides the block of the code for the
false case of the condition to be checked.
If the condition provided in the if
statement is false, then the else
statement will be executed.
Nested if Nested if statements enable us to use if ?
Statement else statement inside an outer if
statement.

The if statement
The if statement is used to test a particular
condition and if the condition is true, it executes a
block of code known as if-block. The condition of if
statement can be any valid logical expression which
can be either evaluated to true or false.
Syntax:
1.if expression:
2. statement
Ex: Program to print the given number is even or
odd

num = int(input("enter the number?"))


1.if num%2 == 0:
2. print("Number is even")

The if-else statement


The if-else statement provides an else block
combined with the if statement which is executed
in the false case of the condition.
If the condition is true, then the if-block is
executed. Otherwise, the else-block is executed.
syntax :
1.if condition:
2. #block of statements
3.else:
4. #another block of statements (else-block)
5.
Ex: Program to check which is the big number in
given two numbers.
1.Var1 = int (input("Enter your number "))
2.Var2= int (input("Enter your number "))
3.
4.if var1>var2:
5. print("Big is var1");
6.else:
7. print("Big is var2");

The elif statement


The elif statement enables us to check multiple
conditions and execute the specific block of
statements depending upon the true condition
among them. We can have any number of elif
statements in our program depending upon our
need. However, using elif is optional.
The elif statement works like an if-else-if ladder
statement in C. It must be succeeded by an if
statement.
syntax :
1.if expression 1:
2. # block of statements
3.
4.elif expression 2:
5. # block of statements
6.
7.elif expression 3:
8. # block of statements
9.
else:
# block of statements
Ex: Write a program to print which is equal to
given number

1.number = int(input("Enter the number?"))


2.if number==10:
3. print("number is equals to 10")
4.elif number==50:
5. print("number is equal to 50");
6.elif number==100:
7. print("number is equal to 100");
8.else:
print("number is not equal to 10, 50 or 100
");
Nested If

You can have if statements inside if statements, this


is called nested if statements.

Example
x = 41

if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")

2. Is String a mutable data type? Also explain the


string operations length and slicing in detail with
an appropriate example.
Ans: String is not a mutable data type. It is
immutable data type only. Immutable means we
can’t changes its values at any time. Once you
store any value in the string we can’t change that
value. That’s why we can say string is immutable
data type.
Operations of Strings:
a) Length(): It is a built-in Function. It is used
to find the length of the string.
Syntax: len(String name)

Ex:

Str = ”Rama Rao”


Print(len(str))

b) Slicing: It is an operation. It is used to get


the substring of a given string.

Syntax: String variable/string name[value]

Ex:

Str=”ramarao”
Print(str[1])

3. Explain about String and its Methods

String is nothing but a data structure, and it


is groups of characters or it is a bunch of
characters or it is a sequence of characters.
It is a combined unit and it contains small
pieces of data.

So many string methods are available.


1.Capitalize()
It converts the first character to upper
case in a given string

Syntax: string.capitalize()

Ex:

txt=”hello world”
x=txt.capitalize()
print(x)

2.Casefold()

It converts the string into lower case

Syntax: String.casefold()

Ex:
txt=”Hello world”
x=txt.casefold()
print(x)

3.Count()

It returns the number of times a specified


value occurs in a given string
Syntax: String.count(substring,start,end)

Ex:

Txt=”apples are good for health, Apples


colour in Red”
X=txt.count(“apple”)
Print(x)

4.Upper()

It returns a copy of a letter or a word


converted to upper case

Syntax: String.upper(“string_name”)

Ex:

txt=”apples are good for health”


x=txt.upper()
print(x)

5.Lower()
It returns a copy of a letter or a word
convert to lower case

Syntax:String.lower(“string_name”)
Ex:

txt=”APPLES ARE GOOD FOR


HEALTH”
x=txt.lower()
print(x)

4. Demonstrate the use of break and continue


keywords in looping structure using a code snippet.
Ans: Break and continue keywords are jump
statements. Basically we have three jump
statements,
i.e., Break, continue and pass.
Break: It is used to terminate the current
loop and resumes execute at the next statement
Ex:

i=1
while i < 6:
print(i)
break
i=i+1
Continue: It is used to skip all the
remaining statements in the current iteration of the
loop
and moves the control back to the
top of the loop
Ex:
i=1
while i < 6:
print(i)
continue
i=i+1

5. Dictionaries and various example


A dictionary is a collection which is ordered,
changeable and do not allow duplicates.

As of Python Version 3.7, dictionaries are ordered. In


Python 3.6 and earlier dictionaries are unordered.

Dictionaries are written with curly brackets, and have


keys and values.

Dictionary items are presented in key:value pairs, and


can be referred to by using the key name.

Create and Print a Dictionary:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)

Dictionary Items

Dictionary items are ordered, changeable, and does


not allow duplicates.

Dictionary items are presented in key:value pairs,


and can be referred to by using the key name.

Example

Print the "brand" value of the dictionary:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])

Ordered or Unordered

When we say that dictionaries are ordered, it


means that the items have a defined order, and that
order will not change.
Unordered means that the items does not have a
defined order, you cannot refer to an item by using
an index.

Changeable

Dictionaries are changeable, meaning that we can


change, add or remove items after the dictionary
has been created.

Duplicates Not Allowed

Dictionaries cannot have two items with the same


key:

Example

Duplicate values will overwrite existing values:


thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)

You might also like