q
Problem 1: Python Variables
Problem Statement:
Declare and print variables of different data types.
Examples:
In this example, we have declared variables of different data types (integer_variable, float_variable, string_variable, Boolean_variable) and assigned them values of respective data types. Then, we simply print these variables to the console.
Solution Approach:
Declare variables of different data types and print them using the print function.
Code Implementation:
Interger_var = 4
Float_var = 2.1
Boolean_var = True
String_var = “hello America!”
Print (“Interger variable:”, Interger_var)
Print (“Float variable:”, Float_var)
Print (“Boolean variable:”, Boolean_var)
Print (“String variable:”, String_var)
Screenshot:
Problem 2: Swap Two Variables
Problem Statement:
Write a Python program to swap two variables without using a temporary variable.
Example:
This example demonstrates swapping the values of two variables (a and b) without using a temporary variable. The swap is achieved using a tuple assignment in Python. By assigning a to b and b to a simultaneously, the values are effectively swapped.
Solution Approach:
Swap the values of two variables directly without using a temporary variable.
Code Implementation:
a = 4
b = 2
a, b = b, a
print (“x=“ a)
print (“y=” b)
Screenshot:
Problem 3: Python For Loops
Problem Statement:
a. Write a Python program to print numbers from 1 to 10 using a for loop.
b. Write a Python program to find the factorial of a number using a for loop.
Example:
a. Printing numbers from 1 to 10: We use a for loop with the range () function to iterate over numbers from 1 to 10 and print each number.
b. Finding factorial of a number: We calculate the factorial of a given number (number = 5) using a for loop. The factorial is computed by multiplying each integer from 1 to the given number.
Solution Approach:
a. Iterate through numbers 1 to 10 using a for loop and print each number.
b. Calculate the factorial of a given number using a for loop.
Code Implementation:
#Printing a number from 1 to 10
For I in range (1,11)
Print(i)
# Finding factorial of a number
number = 5
factorial = 1
for I in range (1, number + 1):
factorial *= i
print (“Factorial of”, number, “is”, factorial)
Screenshot:
Problem 4: Python Functions
Problem Statement:
a. Write a Python program to define a function that takes two arguments and returns their sum.
b. Write a Python program to calculate the area of a circle using a function.
Example:
a. Defining a function to find the sum of two numbers: We define a function sum_of_two_number (a, b) which takes two arguments a and b and returns their sum.
b. Calculating the area of a circle using a function: We define a function area_of_circle(radius) which takes the radius of a circle as an argument and returns its area using the formula πr².
Solution Approach:
a. Define a function that takes two arguments and returns their sum.
b. Define a function to calculate the area of a circle using the formula πr².
Code Implementation:
# Define a function to find the sum of two numbers.
Def sum (a, b)
Return a + b
Result = sum (2,1)
Print (“sum=”, result)
# Define a function to find the area of circle.
Def area_of_circle (radius):
Return 3.14 * radius * radius
Radius = 2
Area = area_of_circle(radius)
Print (“Area of circle is:”, radius, “is”, area)
Screenshot:
Problem 5: Python Lambda
Problem Statement:
a. Write a Python program to create a lambda function that doubles a given number.
b. Write a Python program to filter a list of numbers using a lambda function to select only even numbers.
Example:
a. Creating a lambda function to double a number: We define a lambda function double which takes a single argument x and returns its doubled value.
b. Filtering even numbers from a list using a lambda function: We use the filter () function with a lambda function to filter even numbers from a list of numbers.
Solution Approach:
a. Use lambda function to double a given number.
b. Use lambda function with filter () to select only even numbers from a list.
Code Implementation:
# Create a lambda function to double a number
double = lambda x: x * 2
print (double (5))
# Filter even numbers from a list using lambda function
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list (filter (lambda x: x % 2 == 0, numbers))
print(even_numbers)
Screenshot:
Problem 6: Python Arrays
Problem Statement:
a. Write a Python program to create an array of integers and display them.
b. Write a Python program to perform basic operations (addition, subtraction, multiplication) on arrays.
Example:
a. Creating an array of integers and displaying them: We import the array module and create an array of integers using array.array(). Then, we print the array to display its contents.
b. Performing basic operations on arrays: We perform addition, subtraction, and multiplication operations on arrays using list comprehension and the zip () function to iterate over corresponding elements of two arrays.
Solution Approach:
a. Use the array module to create an array of integers and display them.
b. Perform addition, subtraction, and multiplication operations on arrays using list comprehension.
Code Implementation:
# An array of integers and display them
import array as arr
numbers = arr.array('i', [1, 2, 3, 4, 5])
print(numbers)
# Perform basic operations on arrays
array1 = arr.array('i', [1, 2, 3])
array2 = arr.array('i', [4, 5, 6])
result_addition = [x + y for x, y in zip(array1, array2)]
print ("Addition:", result_addition)
result_subtraction = [x - y for x, y in zip(array1, array2)]
print ("Subtraction:", result_subtraction)
result_multiplication = [x * y for x, y in zip (array1, array2)]
print ("Multiplication:", result_multiplication)
Screenshot: