0% found this document useful (0 votes)
29 views7 pages

Python Data Types and Patterns Guide

The document contains Python programs demonstrating various number data types, including integers, floats, complex numbers, and booleans, along with type conversions and arithmetic operations. It also includes multiple methods to find the largest of three numbers using if-else statements, nested ifs, functions, the max() function, lists, sorted(), and ternary operators. Additionally, the document provides functions to print different patterns such as pyramids and right-angled triangles using nested loops.

Uploaded by

shankar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

Python Data Types and Patterns Guide

The document contains Python programs demonstrating various number data types, including integers, floats, complex numbers, and booleans, along with type conversions and arithmetic operations. It also includes multiple methods to find the largest of three numbers using if-else statements, nested ifs, functions, the max() function, lists, sorted(), and ternary operators. Additionally, the document provides functions to print different patterns such as pyramids and right-angled triangles using nested loops.

Uploaded by

shankar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Program to demonstrate different number datatypes in python.

# Assigning values (Python automatically determines the type)


num1 = 10 # Integer
num2 = 10.5 # Float
num3 = 3 + 4j # Complex
num4 = True # Boolean

# Printing values and their types


print("Value:", num1, " Type:", type(num1))
print("Value:", num2, " Type:", type(num2))
print("Value:", num3, " Type:", type(num3))
print("Value:", num4, " Type:", type(num4))

# Boolean in arithmetic operations


print("Boolean as Integer:", num4 + 5) # True is treated as 1 → Output: 6

# Type Conversions
print("\nType Conversions:")
print("Integer to Float:", float(num1))
print("Float to Integer:", int(num2))
print("Integer to Complex:", complex(num1))
print("Boolean to Integer:", int(num4))
print("Boolean to Float:", float(num4))

OUTPUT:
# Printing values and their types
Value: 10 Type: <class 'int'>
Value: 10.5 Type: <class 'float'>
Value: (3+4j) Type: <class 'complex'>
Value: True Type: <class 'bool'>

# Boolean in arithmetic operations


Boolean as Integer: 6

#Type Conversions:
Integer to Float: 10.0
Float to Integer: 10
Integer to Complex: (10+0j)
Boolean to Integer: 1
Boolean to Float: 1.0
5. Write a python program to find largest of three numbers
Using If-else:
# Get input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Find the largest number using if-else statements


if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3

# Display the result


print(f"The largest number is: {largest}")

Using nested if:


# Get input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Find the largest using nested if statements


if num1 > num2:
if num1 > num3:
largest = num1
else:
largest = num3
else:
if num2 > num3:
largest = num2
else:
largest = num3
# Display the result
print(f"The largest number is: {largest}")
Using Functions:
def find_largest(a, b, c):
"""Find the largest of three numbers."""
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c

# Get input from the user


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Find the largest number


largest = find_largest(num1, num2, num3)

# Display the result


print(f"The largest number is: {largest}")

Using the max() function:


# Get input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Find the largest using max() function


largest = max(num1, num2, num3)

# Display the result


print(f"The largest number is: {largest}")

Using a list and the max() function:


# Get input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Create a list and find the maximum


numbers = [num1, num2, num3]
largest = max(numbers)

# Display the result


print(f"The largest number is: {largest}")

Using sorted() function:


# Get input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Sort the numbers and get the last (largest) one


largest = sorted([num1, num2, num3])[-1]

# Display the result


print(f"The largest number is: {largest}")

Using ternary operators (conditional expressions):


# Get input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Find largest using nested ternary operators


largest = num1 if (num1 >= num2 and num1 >= num3) else (num2 if num2 >=
num3 else num3)

# Display the result


print(f"The largest number is: {largest}")

OUTPUT:
Enter first number: 3
Enter second number: 5
Enter third number: 8
The largest number is: 8.0
7. a) Write a python program to construct the following patterns using
nested for loop

•••
•••••
•••••••
def print_pyramid(): ##This defines a function named print_pyramid that does
not take any parameters.
"""
Prints a pyramid pattern as shown in the image:

•••
•••••
•••••••
"""

rows = 4 #A variable rows is set to 4, meaning the pyramid will have 4 rows.

for i in range(rows): # Outer loop. This for loop iterates from i = 0 to i = 3 (since range(4) generates 0,
1, 2, 3). Each iteration represents a new row in the pyramid.
for j in range(rows - i - 1): # Inner loop1This loop prints spaces to align the dots.
print(" ", end="") # The end="" argument prevents the cursor from moving to a new line
after printing spaces.

for k in range(2*i + 1): # Inner loop2 prints • The number of dots in each row follows the
formula 2*i + 1
print("•", end="") # ensures that dots are printed on the same line

# Move to the next line


print() #After printing spaces and dots for a row, this print() statement moves to the next
line.

# Call the function to display the pyramid


print_pyramid() # This calls the print_pyramid() function to execute the logic.
7. b) Reverse pyramid:
•••••••
•••••
•••

def print_reverse_pyramid():
"""
Prints a reverse pyramid pattern:
•••••••
•••••
•••

"""
# Number of rows in the reverse pyramid
rows = 4
for i in range(rows):
# Print spaces before the dots (increasing with each row)
for j in range(i):
print(" ", end="")

# Print the dots (decreasing with each row using formula: 2*(rows-i)-1)
for k in range(2*(rows-i)-1):
print("•", end="")

# Move to the next line


print()

# Call the function to display the reverse pyramid


print_reverse_pyramid()
7. c) Right angled triangle:

••
•••
••••
def print_right_angle_triangle():
"""
Prints a right angle triangle pattern:

••
•••
••••
"""
# Number of rows in the triangle
rows = 4

for i in range(rows):
# Print the dots (i + 1 dots per row)
for k in range(i + 1):
print("•", end="")

# Move to the next line


print()

# Call the function to display the right angle triangle


print_right_angle_triangle()

You might also like