Ass 1 Ans
Ass 1 Ans
Ass 1 Ans
Ans Anything that is written in a single line after '#' is considered as a comment. The syntax for writing
single-line comments is: # comments here. There are two ways of using single-line comments in Python.
You can use it before the code or next to the code.
Ans x = 10
y = 50
temp = x
x=y
y = temp
print("Value of x:", x)
print("Value of y:", y)
3) What is literal?
Ans literals are a data type and can hold any value type, such as strings, numbers, and more.
a)Indentation
Ans White spaces at the beginning of line called indentation. In python indentation is used to associate
and group statement.(:)
a=20
b=10
if(a==b):
print("Equal“)
else:
print("Not equal")
if(a>b):
print("A is big")
else:
print("B is big")
b) Reserved Words
• Ans Reserved words (also called keywords) are defined with predefined meaning an syntax in
the language.
• These keywords have to be used to develop programming instructions.
• Reserved words can’t be used as identifiers for other programming elements like name of
variable, function etc.
• Reserved words: Total -35
c) comment
Ans Comments in Python are identified with a hash symbol, #, and extend to the end of the line.
• In Python the input() function is used to take input from the user.
• To take input from users, Python makes use of input() function. The input() prompts the user to
provide some information on which the program can work and give the result.
• However, we must always remember that the input() function takes input as a string. So
whether you input a number or a string, it is treated as a string only.
OUTPUT
• In Python, we can simply use the print() function to print output.
8) Explain features of python programming language.
Ans
• Simple – Simple and small language, Feel like reading English.
• Easy to learn- Structure of program is simple, uses few keywords and clearly defined syntax.
• Free and open source- Anyone freely distribute it, read the source code, edit, and use the code
to write new program
• High-level language- Programmer does not need to worry about low level details like managing
memory.
• Interactive- Programmer can easily interact with interpreter or compiler directly at python
prompt to write their program
• Portable or Cross platform- Program behaves same on the wide variety of hardware platform,
Program work on any of the operating system like Windows, Linux, Solaris etc
operands
Multiplication: multiplies
* x*y
two operands
b) Logical Operators
In Python, Logical operators are used on conditional statements (either True or False). They
perform Logical AND, Logical OR, and Logical NOT operations.
Operator Description Syntax Example
Returns True if
and both the operands x and y x>7 and x>10
are true
Returns True if
or either of the x or y x<7 or x>15
operands is true
c) Membership operators
The Python membership operators test for the membership of an object in a sequence, such as
strings, lists, or tuples. Python offers two membership operators to check or validate the
membership of a value. They are as follows:
Membership
Operator Description Syntax
d) Relational operators
The different types of Comparison Operators in Python are as follows:
Operator Description Syntax
Python Equality
== Equal to: True if both operands are equal a == b
Operators
i=0
while i<5:
i+=1
print("i =",i)
else:
print("else block is executed")
Output i = 1
i=2
i=3
i=4
i=5
else block is executed
# condition 2
if i < 0:
print("Negative")
else:
print("Zero")
output
zero
Programs
Q.1) Write a python program to check whether number is positive,
negative or zero?
# Python program to check whether
# the number is positive, negative
# or equal to zero
def check(n):
# Driver Code
check(5)
check(0)
check(-5)
# Driver Code
num = 5
print("Factorial of",num,"is",factorial(num))
return largest
# Call the function to find the maximum and then print the result
print("The maximum of", a, ",", b, "and", c, "is", maximum(a, b, c))
# Subtraction
subtraction = num1 - num2
print("Subtraction:", num1, "-", num2, "=", subtraction)
# Multiplication
multiplication = num1 * num2
print("Multiplication:", num1, "*", num2, "=", multiplication)
# Division
if num2 != 0: # Check if the second number is not zero to avoid division by zero error
division = num1 / num2
print("Division:", num1, "/", num2, "=", division)
else:
print("Division by zero is not allowed")
if c > greatest:
greatest = c
return greatest
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
print("The greatest number among", num1, ",", num2, ", and", num3, "is", greatest_num)
**
***
****
*****
def print_pattern(rows):
for i in range(1, rows + 1):
for j in range(i):
print("*", end=" ")
print()
a) Accessing value
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
b) Display values
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
c)Delete values
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
# Try to display the tuple after deletion (this will raise an error because the tuple no longer exists)
print("Tuple after deletion:", my_tuple) # This will raise a NameError
d)len() function
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
tuple_length = len(my_tuple)
e)max() function
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
max_value = max(my_tuple)
f) min() function
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
min_value = min(my_tuple)
1) Repetition (Multiplication):
You can use the repetition operator `*` to repeat a tuple a certain number of times.
# Define a tuple
my_tuple = (1, 2, 3)
2) Concatenation:
You can use the concatenation operator `+` to concatenate two or more tuples.
# Define two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
Output:
Concatenated tuple: (1, 2, 3, 4, 5, 6)
3) Membership:
You can use the membership operator `in` to check if an element exists in a tuple.
# Define a tuple
my_tuple = (1, 2, 3, 4, 5)
Output:
3 exists in the tuple
4) Length:
You can use the `len()` function to find the length of a tuple.
# Define a tuple
my_tuple = (1, 2, 3, 4, 5)
Output -- 50
b) Display list
# Creating a list
my_list = [10, 20, 30, 40, 50]
# Method 2: Using the dict() constructor and passing key-value pairs as arguments
my_dict = dict(key1='value1', key2='value2', key3='value3')
# Method 3: Using a list of tuples where each tuple represents a key-value pair
my_dict = dict([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])
# Method 4: Using a list of two-element lists where each list represents a key-value
pair
my_dict = dict([['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']])
b) Access elements
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
value = my_dict['key2']
print(value)
Output: value2
c) Update dictionary
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
my_dict['key2'] = 'new_value'
print(my_dict)
Output: {'key1': 'value1', 'key2': 'new_value', 'key3': 'value3'}