Showing posts with label Python Basics. Show all posts
Showing posts with label Python Basics. Show all posts

Sunday, 10 August 2025

LCM of two numbers without built-in functions

LCM of Two Numbers

LCM (Least Common Multiple) of two given numbers without using built-in math functions or user-defined functions.

# LCM of two numbers without use of built-in functions.

# Input two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Store the maximum of num1 and num2
if num1 > num2:
    max_num = num1
else:
    max_num = num2

# Find LCM using loop
while True:
    if (max_num % num1 == 0) and (max_num % num2 == 0):
        lcm = max_num
        break
    max_num += 1

# Display the result
print("LCM of", num1, "and", num2, "is", lcm)

Output of program

Enter first number: 5

Enter second number: 7

LCM of 5 and 7 is 35


Sunday, 18 August 2024

Python Nested If Examples

Nested-If Statements in Python

When we write if statements inside another if statement, it is called Nested If Statements. These can be very useful to make complex decisions in your program. They can be several levels deep. They are also a type of decision making statement in python. 

Followings are some popular examples related to usage of the python nested-if statements.

Example 1: Python program to print student's grade.

# Python program to print student's grade

score = int(input("Enter your marks:"))

if score >= 90:
    grade = 'A'
else:
    if score >= 80:
        grade = 'B'
    else:
        if score >= 70:
            grade = 'C'
        else:
            grade = 'F'

print("Grade:", grade)


Output

Enter your marks:85 Grade: B


Example 2: Python program to check weather conditions. 

# Checking Weather Conditions
temperature = 30
raining = False

if temperature > 25:
    if raining:
        print("It's warm but raining. Bring an umbrella!")
    else:
        print("It's warm and sunny. Enjoy the weather!")
else:
    if raining:
        print("It's cold and raining. Better stay indoors!")
    else:
        print("It's cold but dry. Dress warmly!")

Output

It's warm and sunny. Enjoy the weather!


Example 3: Python program to check user authentication (username and password).
 
# User Authentication
username = "admin"
password = "12345"

if username == "admin":
    if password == "12345":
        print("Access granted")
    else:
        print("Incorrect password")
else:
    print("Username not found")

Output
Access granted


Example 4: Python program to check eligibility for a loan based on given Age and Income.

# We assume that a person is eligible for loan if age is
# above 18 years and income greater than or equal to 40000.

age = int(input("Enter your age in years:"))
income = int(input("Enter your income in Rs:"))

if age > 18:
    if income > 40000:
        print("You are eligible for the loan")
    else:
        print("You are not eligible for the loan due to low income")
else:
    print("You are not eligible for the loan due to age restriction")

Output
Enter your age in years:30 Enter your income in Rs:50000 You are eligible for the loan


Example 5:  Python program to check Positive, Negative or Zero number.

# Checking for Positive, Negative, or Zero Number
number = int(input("Enter one number:"))
if number >= 0:
    if number == 0:
        print("The number is zero")
    else:
        print("The number is positive")
else:
    print("The number is negative")

Output
Enter a number:5 The number is positive

* * *

Monday, 27 November 2023

Python Basics MCQs Set 2

MCQs based on Python Basics (Set 2)


10 important MCQs related to Python basic syntax, along with their answers:


1. Which of the following is the correct syntax for assigning a value to a variable in Python?

(a) my_variable := 10

(b) my_variable = 10

(c) my_variable = "10"

(d) my_variable '10'


2. What is the output of the following Python code?

print("Hello, World!)


(a) Hello, World!

(b) "Hello, World!

(c) Syntax Error

(d) Import Error


3. What is the data type of the variable my_number in the following Python code?

my_number = 10


(a) int

(b) float

(c) string

(d) boolean


4. Which of the following is the correct syntax for an if statement in Python?

(a) if condition:

      body

(b) if condition;

      body

(c) if condition: {

      body }

(d) if condition; {

      body }


5. What is the difference between the + and = operators in Python?

(a) The + operator is used for addition of numbers, while the = operator is used for assignment.

(b) The + operator is used for concatenation of numbers, while the = operator is used for assignment.

(c) The + operator is used for assignment while the = operator is used for addition.

(d) None of these


6. What is the purpose of the print() function in Python?

(a) To print output to the console

(b) To read input from the user

(c) To convert data types

(d) To check if a string is empty


7. What is the purpose of the indentation in Python code?

(a) To make the code more readable

(b) To define code blocks

(c) To prevent errors

(d) All of the above


8. What is the purpose of the for loop in Python?

(a) To prevent errors from occurring

(b) To check if a condition is true

(c) To handle errors in code

(d) To iterate over a sequence of items


9. Which of the following is not a valid Python identifier?

(a) my_variable

(b) 1st_name

(c) while1

(d) _my_variable


10. What is the purpose of the input() function in Python?

(a) To convert data types

(b) To print output to the console

(c) To read input from the user

(d) To check if a string is empty



Answers

1. (b) my_variable = 10

2. (c) Syntax Error

3. (a) int

4. (a) if condition:

         body

5. (a) The + operator is used for addition of numbers, while the = operator is used for assignment. 

6. (a) To print output to the console

7. (b) To define code blocks

8. (d) To iterate over a sequence of items

9. (b) 1st_name

10. (c) To read input from the user


< Back to Python MCQs >


Friday, 1 January 2021

Python program to print 10 to 1 using while

 Following Python program will print 10 to 1 numbers using while loop. 

Expected Output: 10 9 8 7 6 5 4 3 2 1


Program Code:

number = 10

while number >= 1:
  print(number, end=" ")
  number = number - 1


Output of Program:

10 9 8 7 6 5 4 3 2 1


* * * * *


< Back to Python Loops >

< Back to Home >

Python program to print 1 to 10 using while loop

Following Python program will print 1 to 10 numbers using while loop. 

Expected Output: 1 2 3 4 5 6 7 8 9 10


Program Code:

number = 1

while number <= 10:
  print(number, end=" ")
  number = number + 1


Output of Program:

1 2 3 4 5 6 7 8 9 10


* * * * *


< Back to Python Loops >

< Back to Home >



Wednesday, 25 December 2019

Python program to read and print your name and address

Following python program will read and print name, address and mobile number of the user.


Concept to learn

input ( ) : Python input() function can be used to read user inputs. No need to specify variable data type as Python automatically identifies whether entered data is a string or a number.

Source code

# Python program to read and print your name and address using the input() function.
  
name = input("Enter your name: ") 
print(name)

address = input("Enter your address:")
print (address)

mobile = input("Enter your mobile number:")
print(mobile)

Program output

Enter your name: K Patel
K Patel
Enter your address: Ahmedabad, Gujarat, India
Ahmedabad, Gujarat, India
Enter your mobile number: 9898989898
9898989898



* * * * *


Friday, 29 June 2018

Python nested if statement

Python nested-if statement

Nested if statements in any programming language means an if statement inside another if statement. Python also allows us to nest if statements within if statements. i.e, we can write an if statement inside another if statement.

Syntax of nested-if statement:

if (condition 1):
   # Code to be executed when condition 1 is true
   if (condition 2):
      # Code to be executed when condition 2 is true
   # end of inner if Block
# end of outer if Block

Example of Nested-if Statement: Python program to check Positive, Negative or Zero number.

# Checking for Positive, Negative, or Zero Number
number = int(input("Enter one number:"))
if number >= 0:
    if number == 0:
        print("The number is zero")
    else:
        print("The number is positive")
else:
    print("The number is negative")

Output of program

Enter a number:5
The number is positive


if-elif-else ladder

The if statements are executed from the top to the bottom sequentially. When one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the last else statement written will be executed.

Syntax of if-elif-else

if (condition 1):
    statements for condition 1
elif (condition 2):
    statements for condition 2
.
.
else:
    statements for else

Example of Python if-elif-else ladder

number = int(input("Enter number in range of 1 to 5:"))
if (number == 1):
 print ("It’s One")
elif (number == 2):
 print ("It’s Two")
elif (number == 3):
 print ("It’s Three")
elif (number == 4):
 print ("It’s Four")
elif (number == 5):
 print ("It’s Five")
else:
 print ("It’s not in range of 1 to 5")

Output of program

Enter number in range of 1 to 5:3
It’s Three

Suggested Articles for you: