0% found this document useful (0 votes)
4 views5 pages

Python Programs for Beginners

The document contains Python programs for various tasks including checking if a number is odd or even, finding the largest of three numbers, determining if input is a number or character, printing prime numbers less than 20, and checking if a triangle is a right triangle. It also explains the use of if, elif, and else statements, the print() and input() functions, and types of comments in Python. Each section includes code examples and explanations to illustrate the concepts.

Uploaded by

kyadadarshil6
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)
4 views5 pages

Python Programs for Beginners

The document contains Python programs for various tasks including checking if a number is odd or even, finding the largest of three numbers, determining if input is a number or character, printing prime numbers less than 20, and checking if a triangle is a right triangle. It also explains the use of if, elif, and else statements, the print() and input() functions, and types of comments in Python. Each section includes code examples and explanations to illustrate the concepts.

Uploaded by

kyadadarshil6
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. Write a python program to find whether given number is odd or even.

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


if num % 2 == 0:
print("given number is even")
else:
print("Given Number is odd")

2. Write a python program to find largest of three numbers using control


structures.
num1 = int(input("Enter First Number:"))
num2 = int(input("Enter Second Number:"))
num3 = int(input("Enter Third Number:"))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

3. Write a python program to find whether given input is a number or


character.
X=input(“Enter A value”)
If(‘a’<= x <=’z’ or ‘A’ <= x <= ’Z’ )
Print(“Given Value is Char”)
else:
print(“Given Value is Number”)

4. Write a python program that prints prime numbers less than 20.

x=int(input("Enter A number :"))


if x<2:
print("Not A prime number")
elif(x==2 or x==3 or x==5 or x==7 or x==9 or x==11 or x==13):
print("Given Number is Prime")
elif x<20:
print("Not a Prime number")
else:
print("Value is not Valid")

5. Write a python program that accepts the lengths of three sides of a


triangle as inputs. TheProgram output should indicate whether or not
the triangle is a right triangle(Note: In aright triangle, the square of one
side equals to the sum of the squares of other two sides).
1. Explain if, elif, else statements in Python with syntax and examples
In Python, if, elif, and else statements are used for decision-making. These
statements allow the program to execute certain blocks of code based on
conditions.
Syntax:
if condition:
# Code to execute if the condition is True
elif another_condition:
# Code to execute if the previous condition is False and this one is True
else:
# Code to execute if all the above conditions are False
Example:
age = 20
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult.")
else:
print("You are an adult.")
Output:
You are an adult.

2. Explain with example: How print() and input() functions can be used in
Python?
print() Function:
The print() function is used to display output on the screen.
Example:
print("Hello, World!")
print("The sum of 5 and 10 is:", 5 + 10)
Output:
Hello, World!
The sum of 5 and 10 is: 15
input() Function:
The input() function is used to take user input as a string.
Example:
name = input("Enter your name: ")
print("Hello,", name)
Input:
Enter your name: John
Output:
Hello, John

3. Explain types of comments in Python


Comments in Python are used to explain the code and are ignored during
execution. There are three types of comments in Python:
1. Single-line Comments (#)
o Use # before the text to comment a single line.
o Example:
2. # This is a single-line comment
3. print("Hello, World!") # This prints a message
4. Multi-line Comments (Using Triple Quotes ''' or """)
o Multi-line comments are enclosed within triple quotes.
o Example:
5. '''
6. This is a multi-line comment.
7. It spans multiple lines.
8. '''
9. print("Python comments example.")
[Link] (Documentation Strings)
o Used to document functions, classes, or modules.
o Enclosed in triple quotes (""" or ''').
o Example:
[Link] add(a, b):
12. """This function returns the sum of two numbers."""
13. return a + b
14.
[Link](add(3, 5))
These comments help in making the code more readable and
understandable.

You might also like