Python Basic Learn with easy step

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 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: