Professional Documents
Culture Documents
Python 11-20docx
Python 11-20docx
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
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
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: "))
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: "))
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: "))
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.
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
the uses and examples of the range() , max(), and sum() functions in Python:
a. range():
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():
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.
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.)