Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

11.

Write a program to check whether the given number is ‘Positive’ or ‘Negative’


(Use input function to take values).

Here's a simple Python program that checks whether a given number is positive or negative using the
input function to take the number as input:

python
Copy code

# Input function to take a number as input


num = float(input("Enter a number: "))

# Check if the number is positive, negative, or zero


if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")

This program takes a number as input, converts it to a float (to handle decimal numbers as well), and
then checks if it's positive, negative, or zero, printing the appropriate message.

12.
Write a python code snippet to give a list of odd numbers between any 2 positive
numbers

You can use a simple loop to generate a list of odd numbers between any two
positive numbers. Here's a Python code snippet that does that:

python
Copy code

# Input two positive numbers


start_num = int(input("Enter the starting number: "))
end_num = int(input("Enter the ending number: "))

# Ensure start_num is less than end_num


if start_num >= end_num:
print("Invalid input. Please ensure the starting number is less than the ending
number.")
else:
# Generate a list of odd numbers between start_num and end_num
odd_numbers = [num for num in range(start_num, end_num + 1) if num % 2 != 0]

# Print the list of odd numbers


print("Odd numbers between", start_num, "and", end_num, "are:", odd_numbers)
This code snippet takes two positive numbers as input, ensures that the starting
number is less than the ending number, generates a list of odd numbers between
the two input numbers, and then prints the list.

13 Write a program to print first n natural numbers using for loop (Take input from
user).

Here's a Python program that prints the first n natural numbers using a for loop,
where n is taken as input from the user:

python
Copy code
# Input the value of n
n = int(input("Enter the value of n: "))

# Using a for loop to print the first n natural numbers


print("First", n, "natural numbers are:")
for i in range(1, n + 1):
print(i)

This program takes an integer n as input from the user and then uses a for loop to
iterate from 1 to n (inclusive) and print each number.

14) Using if-else conditional statements check if the number is ‘Positive’, ’Negative’
or ‘Zero’.

You can modify the previous program to include if-else conditional statements to
check if the number is positive, negative, or zero. Here's the updated program:

python
Copy code
# Input the number
num = float(input("Enter a number: "))

# Check if the number is positive, negative, or zero


if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
This program takes a number as input and then uses if-else statements to check if
the number is positive, negative, or zero, and prints the corresponding message.
15.. Print the greatest number from 2 numbers entered by user.

You can use a simple if-else statement to compare the two numbers entered by the
user and print the greatest number. Here's how you can do it:

python
Copy code
# Input two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Compare the two numbers and print the greatest number


if num1 > num2:
print("The greatest number is:", num1)
elif num2 > num1:
print("The greatest number is:", num2)
else:
print("Both numbers are equal")
This program takes two numbers as input from the user, compares them using if-else
statements, and prints the greatest number. If both numbers are equal, it prints a
message indicating that they are equal.

16.Write the output of the code given below, for i in range(12,23,43,54,68): print(i)

The code you provided has an incorrect usage of the range function. The range
function in Python takes one, two, or three arguments, not multiple arguments as
shown in your code. The correct usage is range(start, stop, step), where start is the
starting value, stop is the ending value (exclusive), and step is the step value.

17. . Give proper syntaxes and indentations for ‘for loop’, ‘while loop’, ‘if –else’,
‘elifstaement’.

the proper syntaxes and indentations for for loop, while loop, if-else statement, and
elif statement in Python:

For Loop:

python
Copy code
for item in iterable:
# Code block to be executed for each iteration
While Loop:

python
Copy code
while condition:
# Code block to be executed as long as the condition is true
If-Else Statement:

python
Copy code
if condition:
# Code block to be executed if the condition is true
else:
# Code block to be executed if the condition is false

Elif Statement:

python
Copy code
if condition1:
# Code block to be executed if condition1 is true
elif condition2:
# Code block to be executed if condition1 is false and condition2 is true
else:
# Code block to be executed if both condition1 and condition2 are false
Make sure to replace iterable, condition, condition1, condition2, and the code blocks
with your actual code. Also, note the use of indentation in Python to define the
scope of each block of code.

18. Give examples of type casting.

Type casting is the process of converting one data type to another. Here are examples of type casting in
Python:

.
Integer to Float:
.
.
pythonCopy code
.
.
num_int = 10
num_float = float(num_int)print(num_float) # Output: 10.0
.
.
.
Float to Integer:
.
.
pythonCopy code
.
.
num_float = 10.5
num_int = int(num_float)print(num_int) # Output: 10
.
.
.
Integer to String:
.
.
pythonCopy code
.
.
num_int = 10
num_str = str(num_int)print(num_str) # Output: '10'
.
.
.
String to Integer:
.
.
pythonCopy code
.
.
num_str = '10'
num_int = int(num_str)print(num_int) # Output: 10
.
.
.
String to Float:
.
.
pythonCopy code
.
.
num_str = '10.5'
num_float = float(num_str)print(num_float) # Output: 10.5
.
.
.
Float to String:
.
.
pythonCopy code
.
.
num_float = 10.5
num_str = str(num_float)print(num_str) # Output: '10.5'
.
.
.
String to List:
.
.
pythonCopy code
.
.
str_val = '1 2 3 4 5'
list_val = str_val.split()print(list_val) # Output: ['1', '2', '3', '4', '5']
.
.
.
List to String:
.
.
pythonCopy code
.
.
list_val = ['1', '2', '3', '4', '5']
str_val = ' '.join(list_val)print(str_val) # Output: '1 2 3 4 5'
.
.

These examples demonstrate how to convert between different data types using type casting in Python

19. Give uses and examples of the following functions


a. range()
b. max()
c. sum()

the uses and examples of the range() , max(), and sum() functions in Python:

a. range():

 Use: The range() function generates a sequence of numbers.


 Syntax: range(start, stop[, step])
 start: Starting value of the sequence (default is 0).
 stop: Ending value of the sequence (exclusive).
 step: Step value of the sequence (default is 1).
 Example:

pythonCopy code


for i in range(5):
print(i)# Output: 0, 1, 2, 3, 4

b. max():

 Use: The max() function returns the largest item in an iterable or the largest of two or more arguments.
 Syntax: max(iterable, *iterables[, key, default])
 iterable: Iterable object (e.g., list, tuple, string) from which the maximum value will be selected.
 key: Function that will be applied to each item before comparison (optional).
 default: Default value to return if the iterable is empty (optional).
 Example:

pythonCopy code


numbers = [10, 20, 30, 40, 50]print(max(numbers)) # Output: 50
# Using key function to find the item with maximum length
words = ['apple', 'banana', 'kiwi', 'strawberry']print(max(words, key=len)) # Output: 'strawberry'

c. sum():

 Use: The sum() function adds up all the items in an iterable.


 Syntax: sum(iterable, start=0)
 iterable: Iterable object (e.g., list, tuple, string) whose items will be summed.
 start: Starting value for the sum (default is 0).
 Example:

pythonCopy code


numbers = [1, 2, 3, 4, 5]print(sum(numbers)) # Output: 15
# Using start parameter to start sum from 10print(sum(numbers, start=10)) # Output: 25

These examples demonstrate the use of range() , max(), and sum() functions in Python for generating
sequences, finding the maximum value, and summing up elements in an iterable, respectively.

20. . What are dictionaries give 2 examples.

Dictionaries in Python are data structures that store key-value pairs. They are mutable, unordered, and
indexed. Each key in a dictionary must be unique and immutable (such as a string, number, or tuple), while
the values can be of any data type and can be duplicated. Here are two examples of dictionaries:

.
Example 1: Student Information
.
.
pythonCopy code
.
.
student = {
'name': 'Alice',
'age': 20,
'grade': 'A'
}
.
.
.
Example 2: Fruit Prices
.
.
.
pythonCopy code
.
fruit_prices = {
'apple': 1.00,
'banana': 0.75,
'orange': 1.20,
'kiwi': 1.50
}
.
.

In these examples, the first dictionary stores information about a student, including their name, age, and
grade. The second dictionary stores the prices of different fruits. In both cases, each key (e.g., 'name', 'age',
'grade', 'apple', 'banana', etc.) is associated with a corresponding value (e.g., 'Alice', 20, 'A', 1.00, 0.75, etc.)

You might also like