Unit 5 Introduction to python pdf
Unit 5 Introduction to python pdf
Introduction to Python
1. Instructions written in a source code that are executed by a Python interpreter are
called program or statements.
2.Identifiers are the user defined names of variables, list, dictionary, tuples, classes etc.
3.Keywords are words that have special meaning and are reserved by Python for special
purposes and are not allowed to be used as identifiers.
4.Variables is a name given to a memory location to hold a specific value.
5. Writing one if statement within another if is called Nested if statement.
Short Question and Answers:
1. What is Python?
An ordered set of instructions to be executed by a computer to carry out a specific task is
called a program, and the language used to specify this set of instructions to the
computer is called a programming language.
Python is one of the programming language to solve the problem.
2. What are the different modes for coding in python?
Python shell can be used in two ways, viz., interactive mode and script mode.
✓ Interactive Mode, allows us to interact with OS.
✓ Interactive mode of working means you type the command – one command at a
time and the python executes the given command there and then gives you
output.
✓ script mode lets us create and edit Python source file.
✓ Python instructions are stored in a file generally with .py extension and are
executed together in one go as a unit and get complete output.
3. What are comments in python? List down the various types of comments
A comment is text that doesn't affect the outcome of a code, it is just a piece of text to
let someone know what you have done in a program or what is being done in a block of
code.
Single line comment : We use the hash (#) symbol to start writing a comment.
Eg. #Define a variable
Multiline comment: These comment lines started with “ “ “ and ended with “ “ “ .
“””This is a program
To solve factorial of two numbers”””
4. What is a variable? Give example
A variable is a named location used to store data in the memory. It is helpful to think of
variables as a container that holds data which can be changed later throughout
programming. For example,
X=42
Y=42
These declarations make sure that the program reserves memory for two variables with
the names x and y. The variable names stand for the memory location.
Long Question and Answers:
1. What are the key features of python? Mention various applications along with it
Eg.
5. What is type conversion? Explain the types of type conversion with the help of an
example.
The process of converting the value of one data type (integer, string, float, etc.) to
another data type is called type conversion. Python has two types of type conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion
Implicit Type Conversion:
In implicit type conversion, Python automatically converts one data type to another data
type. This process doesn't need any user involvement.
Q1) Explain different types of 'If' statements with the help of a flowchart.
If Statement:
Decision making statements in programming languages decide the direction of flow of
program execution. Decision making statements available in Python are:
● If statement
● if else statements
● if-elif ladder
If Statement:
The if statement is used to check a condition: if the condition is true, we run a block of
statements (called the if-block).
Syntax:
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the
text expression is True. If the text expression is False, the statement(s)is not executed.
Example:
if num>0:
print (num, “is a positive number.”)
print (“this is always printed”)
Q2) What is the difference between 'for' loop and 'while' loop. Explain with a help of a
flowchart?
For Loop: For … In statement is another looping statement which iterates over as
sequence of objects. i.e. go through each item in a sequence.
Syntax:
for val in sequence:
Body of for
Here, val is the variable that takes the value of the item inside the sequence one each
iteration. Loop continues until were ach the last item in the sequence.
Numbers=[1,2,3,4,5]
Sum=0
for val in Numbers:
Sum = Sum+Numbers
print (“The sum is:”, Sum)
While Statement:
The while statement allows us to repeatedly execute a block of statements if a condition
is true.
A while statement is an example of looping statement.
A while statement can have an optional else clause.
Syntax:
while test expression:
Body of while
In while loop, test expression is checked first.
The body of the loop is entered only if the test_expression evaluates to True.
After one iteration, the test expression is checked again.
This process continues until the test_expression evaluates to False.
Flowchart:
Output: Enter n: 10
The sum is 55
Q3) What are python nested if statements? Explain with example.
Q4) Write a program to find numbers which are divisible by 7 and multiple of 5 between
1200and 2200.
# Take input for minimum and maximum values
min_val = 1200
max_val = 2200
# Check each number within the range
for num in range(min_val, max_val+1):
if num % 7 == 0 and num % 5 == 0:
print(num, "is divisible by 7 and 5.")
Q5) Write a program to find whether a number is prime or not using 'while' loop
num = int(input("Enter a number ( greater than 1)"))
f=0
i=2
while i <= num / 2:
if num % i == 0:
f=1
break
i=i+1
if f==0:
print("The entered number is a PRIME number")
else:
print("The entered number is not a PRIME number")
Q1) Explain the different ways of slicing a list with a help of an example.
Slicing of a List
In Python List, there are multiple ways to print the whole List with all the elements, but
to print a specific range of elements from the list, we use Slice operation.
Slice operation is performed on Lists with the use of colon (:).
➢ To print elements from beginning to a range use [: Index],
➢ to print elements from end use [: -Index],
➢ to print elements from specific Index till the end use [Index:],
➢ to print elements within a range, use [Start Index: End Index] and
➢ to print whole List with the use of slicing operation, use [:].
➢ to print whole List in reverse order, use [:: -1].
Example 1:
Example 2:
>>> list1 =['Red','Green','Blue','Cyan','Magenta','Yellow','Black']
>>> list1[2:6]
['Blue', 'Cyan', 'Magenta', 'Yellow']
>>> list1[7:2] #first index > second index
[] #results in an empty list
#Return sublist from index 0 to 4
>>> list1[:5] #first index missing.
['Red','Green','Blue','Cyan','Magenta']