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

Python-U-2-ONE-SHOT-Notes

The document outlines the syllabus for Unit 2 of a Python programming course, focusing on flow control, conditional statements, and loops. It includes a comprehensive list of important questions and programming tasks related to if-else statements, loops, and dictionary manipulations. Additionally, it provides examples and exercises for students to practice their Python skills.
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)
8 views

Python-U-2-ONE-SHOT-Notes

The document outlines the syllabus for Unit 2 of a Python programming course, focusing on flow control, conditional statements, and loops. It includes a comprehensive list of important questions and programming tasks related to if-else statements, loops, and dictionary manipulations. Additionally, it provides examples and exercises for students to practice their Python skills.
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
You are on page 1/ 80

AKTU B.

Tech III-Sem

Python
Crash Course
Unit-2 in One Shot Pragya Ma'am
SYLLABUS OF UNIT 2

Python Program Flow Control Conditional blocks: if, else and else if, Simple for loops
in python, For loop using ranges, string, list and dictionaries. Use of while loops in
python, Loop manipulation using pass, continue, break and else. Programming using
Python conditional and loop blocks

1
IMPORTANT QUESTION OF UNIT 2
1. Explain all the conditional statement(if if else etc)
2. Write a program to find out whether a year is leap year or not
3. write a program to find out the whether a number is palindrome or not
4. write a program to find out the fibonnaci series upto n term
5. write a program to convert time from 12 hour to 24 hour format
6 write a program to check whether a number is prime or not
7 Explain break, continue, pass statement with example with example
8. Any patter question check the notes.
9. Write a Python program to sum and multiply all the items in a dictionary
10. Write a Python program to get the maximum and minimum values of a dictionary.
11. Write a Python program to get the key, value and item in a dictionary.
12. Write a Python program to removeduplicates from a list.
13. Write a Python program to find the list words that are longer than n from a given list of words.
14. Write a Python program to print the numbers of a specified list after removing even
numbers from it
2
IMPORTANT QUESTION OF UNIT 2
15. Write a Python program to check if each number is prime in a given list of numbers. Return True if all
numbers are prime otherwise False.
16. Python program to find the second smallest/largest number in a list:
17. Write a Python program to get the frequency of elements in a list
18 write a program to check whether entered number is Fibonacci number or not
19 Difference between for and while
20 write a program to check whether entered number is Fibonacci number or not in range

3
PYTHON PROGRAMMING
(BCC302 / BCC402/ BCC302H / BCC402H)
SYLLABUS
UNIT 2 :Python Program Flow Control Conditional blocks

if, else and else if, Simple for loops in python, For loop using ranges, string, list and
dictionaries. Use of while loops in python, Loop manipulation using pass, continue,
break and else. Programming using Python conditional and loop blocks

1
PYTHON PROGRAMMING
UNIT 2
Python Program Flow Control Conditional blocks
LEC-1
By
Pragya Rajvanshi
M.Tech(CSE)

2
If-else statement Write a program to find out whether a number
Syntax
is even or odd
if condition:
#block of statements
else:
#another block of statements (else-block)

3
 elif statement Write a program to find out the largest number
among three
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statement
else:
#statement

4
Write a program to simulate the calculator

5
 Nested if else Write a program to find out the largest number among
if condition 1:
three using nested –if else
if condition2:
#nested if code
else:
#nested else code
else:
#else code

6
Write a program to find out whether a year is Write a program to find out the roots of
leap year or not
quadratic equation

7
SOME BASIC QUESTIONS
Write a Python program that accepts the user's first
and last name and prints them in reverse order with a
space between them

8
Write a Python program that accepts a Write a Python program to display the first and
sequence of comma-separated numbers from last colors and middle element from the list
the user and generates a list and a tuple of
those numbers.

9
Write a Python program that Write a Python program to count the number 4 in a
accepts an integer (n) and given list.
computes the value of
n+nn+nnn.

Write a Python program to calculate the number of days


between two dates. Sample dates : (2014, 7, 2), (2014, 7, 11)

10
Python program that takes a Write a Python program to test whether a passed letter is a
string and a non-negative integer vowel or not
n, then returns n copies of the
first two characters of the given
string:

Write a Python program that concatenates all elements


in a list into a string and returns it.

11
Write a Python program to sum three given integers. Write a Python program to sum two
However, if two values are equal, the sum will be given integers. However, if the sum is
zero between 15 and 20 it will return 20

12
Write a Python program to add two objects if both Write a Python program to sum two
objects are integers. given integers. However, if the sum is
between 15 and 20 it will return 20

13
Write a Python program to parse a string to float or Write a Python program to count the number
integer of occurrences of a specific character in a
string.

Calculate the hypotenuse of a right angled triangle

14
Write a Python program to remove the Write a Python program to test whether all
first item from a specified list numbers in a list are greater than a certain number.

15
Write a Python program to sum all the Write a Python program to multiply all the items in a
items in a list list.

Write a Python program to get the largest number


from a list

16
Write a Python program to count the Write a Python program to get the smallest number
number of strings from a given list of from a list
strings. The string length is 2 or more
and the first and last characters are the
same

17
PYTHON PROGRAMMING
UNIT 2
Python Program Flow Control Conditional blocks
LEC-2
By
Pragya Rajvanshi
M.Tech(CSE)

2
LOOPS 2 For loop
 Loops is used to execute a block of Syntax
statements repeatedly until a given for var in iterable:
condition is satisfied. # statements

1 while loop
Syntax
while expression:
statement(s)

3
 Range () 2.range (start, stop)
 The Python range() function returns a sequence of
numbers, in a given range.
1. range (stop)

3.range (start, stop, step)

4
Write a for loop that prints number from 0 to 57 write a program to check whether a
using range function
number is Armstrong or not

5
write a program to find out the whether a number write a program to find out the fibonnaci
is palindrome or not
series upto n term

6
Write a program to find out the factorial of a write a program to check whether a number
number
is prime or not

7
write a program to print the reverse of a number What will be the output?

8
write a program to convert time from 12 hour to 24 hour format

9
break statement
The break statement is used to terminate the loop
immediately when it is encountered.
The syntax of the break statement is:
break

10
continue statement
The continue statement is used to skip the current
iteration of the loop and the control flow of the
program goes to the next iteration.
The syntax Continue;

11
Continue Example Break example

12
Pass example
What is pass statement in Python?
 python pass statement is a null statement. But  When the user does not know what code to
the difference between pass and comment is write, So user simply places a pass at that line.
that comment is ignored by the interpreter  Sometimes, the pass is used when the user
whereas pass is not ignored. doesn’t want any code to execute.
 SYNTAX pass
 So users can simply place a pass where empty
code is not allowed, like in loops, function
definitions, class definitions, or in if statements.
So using a pass statement user avoids this error.

13
14
Write a program to print the prime number in range

15
PYTHON PROGRAMMING
UNIT 2
Python Program Flow Control Conditional blocks
LEC-3
By
Pragya rajvanshi

1
Pattern printing-1 Pattern printing-2

2
Pattern printing-3 Pattern printing-4

3
Pattern printing-5 Pattern printing-6

4
Pattern printing-7 Pattern printing-8

5
1. Write a Python script to add a key to a 2. Write a Python script to concatenate/merge the
dictionary following dictionaries to create a new one.

6
3. Write a Python script to check 4.Write a Python program to iterate over dictionaries
whether a given key already exists in a using for loops
dictionary.

7
5. Write a Python script to generate and print a dictionary that contains
a number (between 1 and n) in the form (x, x*x).

6.Write a Python script to print a dictionary where the keys are numbers
between 1 and 15 (both included) and the values are the square of the keys.

8
7. Write a Python program to sum 8.Write a Python program to Multiply all the items in a
all the items in a dictionary dictionary

9. Write a Python program to remove a key from a dictionary.

9
10.Write a Python program to map two lists into a
dictionary.

12. Write a Python program to get the maximum and


minimum values of a dictionary.

11 Write a Python program to sort a given


dictionary by key.

10
13. Write a Python program to remove
duplicates from the dictionary.

15. Write a Python program to combine two


dictionary by adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c':
300})
14 Write a Python program to check if a
dictionary is empty or not.

11
16. Write a Python program
to print all distinct values in
a dictionary.
Sample Data : [{"V":"S001"},
{"V": "S002"}, {"VI": "S001"},
{"VI": "S005"},
{"VII":"S005"},
{"V":"S009"},{"VIII":"S007"}]
Expected Output : Unique
Values: {'S005', 'S002',
'S007', 'S001', 'S009'}

12
17. Write a Python program to create and display 18. Write a Python program to create a dictionary
all combinations of letters, selecting each letter from a string.
from a different key in a dictionary. Note: Track the count of the letters from the string
Sample data : {'1':['a','b'], '2':['c','d']}

13
19. Write a Python program to remove spaces from dictionary keys

20. Write a Python program to check if multiple keys exist in a dictionary.

14
21. Write a Python program to get the key, value and item in a dictionary.

22 Write a Python program to print a dictionary line by line.

23. Write a Python program to replace dictionary values with their sums.

15
Dictionary
24.Write a Python program to create a dictionary
of keys x, y, and z where each key has as value a list
from 11-20, 21-30, and 31-40 respectively. Access
the fifth value of each key from the dictionary.
{'x': [11, 12, 13, 14, 15, 16, 17, 18, 19],
'y': [21, 22, 23, 24, 25, 26, 27, 28, 29],
'z': [31, 32, 33, 34, 35, 36, 37, 38, 39]}
15
25
35
x has value [11, 12, 13, 14, 15, 16, 17, 18, 19]
y has value [21, 22, 23, 24, 25, 26, 27, 28, 29]
z has value [31, 32, 33, 34, 35, 36, 37, 38, 39]

16
25. Write a Python program to
match key values in two
dictionaries.
Sample dictionary: {'key1': 1,
'key2': 3, 'key3': 2},
{'key1': 1, 'key2': 2}
Expected output: key1: 1 is
present in both x and y

17
26.Write a Python program to
find the key of the maximum
value in a dictionary.
Sample Output:
Original dictionary elements:
{'Theodore': 19, 'Roxanne': 22,
'Mathew': 21, 'Betty': 20}
Finds the key of the maximum
and minimum value of the
said dictionary:
('Roxanne', 'Theodore')

18
PYTHON PROGRAMMING
UNIT 2
Python Program Flow Control Conditional blocks
By
Pragya rajvanshi
LEC-4(list)

1
1. Write a Python program to remove 3. Write a Python program to clone or copy a list
duplicates from a list.

2. Write a Python program to check if a list is


empty or not. 4.Write a Python program to find the list of
words that are longer than n from a given list of
words.

2
5. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']

6. Write a Python program to print the numbers of a specified list after removing even
numbers from it

3
7. Write a Python program to shuffle and print a specified list

8. Whose number square is less than equal to 30 range is taken between 1 to 30

4
9. Write a Python program to check if each 10.Write a Python program to calculate the
number is prime in a given list of numbers. difference between the two lists.
Return True if all numbers are prime otherwise
False.

5
11. Write a Python program to access the 13. Write a Python program to find the index of an
index of a list. item in a specified list.

12. Write a Python program to convert a list of 14. Write a Python program to append a list to the
characters into a string. second list.

6
15. Python program to find the second smallest 16 . Python program to find the second largest
number in a list: number in a list:

7
17. Write a Python program to get unique values from a list.

18.Write a Python program to get the frequency of elements in a list

8
PYTHON PROGRAMMING
UNIT 2
Python Program Flow Control Conditional Blocks
By
LEC-5(list)
Pragya Rajvanshi

1
1. Write a Python program to count the number of elements in a list within a specified range.

2. Write a Python program to check whether a list contains a sub list.

2
3. Write a Python program to find common items in two lists

4. Write a Python program to convert a list of multiple integers into a single integer.

3
5. Write a Python program to split a list based on the first character of a word.

6. Write a Python program to create multiple lists.

4
7. Write a Python program to split a list into different variables.

8. Write a Python program to insert an element before each element of a list.

5
9. Write a Python program to split a list every Nth element.
Sample list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
Expected Output: [['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']]

6
10. Write a Python program to concatenate elements of a list.

11.. Write a Python program to convert a string to a list.

12.. Write a Python program to check if all items in a given list of strings are equal to a given
string.

7
PYTHON PROGRAMMING
UNIT 2
Python Program Flow Control Conditional blocks
By
Pragya rajvanshi
LEC-6(some mixed questions)

1
1. Write a Python program to replace the last element in a list with another list. Sample data : [1, 3,
5, 7, 9, 10], [2, 4, 6, 8] Expected Output: [1, 3, 5, 7, 9, 2, 4, 6, 8]

2. Write a Python program to check whether the n-th element exists in a given list.

3. Write a program to create a list of empty dictionary

2
4. Write a Python program to reverse a given list of lists

5. Write a Python program to remove specific words from a given list.


Original list:
['red', 'green', 'blue', 'white', 'black', 'orange']
Remove words:
['white', 'orange']
After removing the specified words from the said list:
['red', 'green', 'blue', 'black']

3
6 . Write a Python program to combine two lists into another list randomly.

4
Difference between while and for loop

For loop While loop

For loop is used to iterate over a sequence of While loop is used to repeatedly execute a block of
items. statements while a condition is true.

While loop is used when the number of iterations is


For loops are designed for iterating over a
not known in advance or when we want to repeat a
sequence of items. Eg. list, tuple, etc.
block of code until a certain condition is met.

While the loop requires an initial condition that is


For loop require a sequence to iterate over.
tested at the beginning of the loop.

For loop is typically used for iterating over a While loop is used for more complex control flow
fixed sequence of items situations.

For loop is more efficient than a while loop


While a loop may be more efficient in certain
when iterating over sequences, since the
situations where the condition being tested can be
number of iterations is predetermined and
evaluated quickly.
the loop can be optimized accordingly.
5
7.write a program to check whether entered number is Fibonacci number or not

6
8.write a program to check whether entered number is Fibonacci number or not in range

7
Use of else with loops
Yes, you can use else with loop

8
Conditional statement:
 If statement
 If-else statement
 Nested-if else
 Elif statement
Purpose of loops
 Loops are used in programming to execute a set of instructions repeatedly until a
certain condition is met. They help automate repetitive tasks, reducing the amount of
code needed and improving efficiency

9
AKTU QUESTIONS

Q.1 What are conditional statement in python using small code AKTU 2019-
example 20

Q.2 Write a python program to check if the input year is a leap year or AKTU 2021-
not 22
Q.3 Write a program to produce the Fibonacci series in python AKTU 2021-
22
Q.4 Define loops. Explain different types of loops in python AKTU 2023-
Or 24
Difference between while and for loop with example, syntax and
flow diagram
Q.5 ANY pattern question AKTU 2021-
22
AKTU 2022-
23 10
AKTU QUESTIONS
Q.6 Explain the the working and purpose of loops. Discuss break and AKTU 2019-
continue with example .Write a python program to convert 20
time from 12 hour format to 24 hour format
Q.7 Explain the use of break , pass and continue with the suitable AKTU 2021-
example 22
Q.8 Write a program to check an input number is prime or not AKTU 2021-
22
Q.9 Write a program to calculate the reverse of a number AKTU 2022-
23
Q.10 Write a program to check whether a number is Fibonacci AKTU 2019-
number or not 20
Q.11 Write a program to convert time format from 12 hour to 24 AKTU 2019-
hour format 20

11
12

You might also like