Saturday 26 December 2020

Python program to swap two variables

Python program to swap two variables

Python Code:

# Read inputs from the user
x = input('Enter value of x: ')
y = input('Enter value of y: ')

print('Value of x before swapping: ', x)
print('Value of y before swapping: ', y)

# using temporary variable to swap values
temp = x
x = y
y = temp

print('Value of x after swapping: ', x)
print('Value of y after swapping: ', y)

Output of program:

Enter value of x: 10 Enter value of y: 20 Value of x before swapping: 10 Value of y before swapping: 20 Value of x after swapping: 20 Value of y after swapping: 10



Friday 25 December 2020

Check whether given character is a vowel or not

Python program to check whether given character is a vowel or not.

Python Code:

myChar = input("Enter a character: ")

if(myChar=='A' or myChar=='E' or myChar=='I' or myChar=='O' or myChar=='U' 
   or myChar=='a' or myChar =='e' or myChar=='i' or myChar=='o' or myChar=='u'):
    print("'",  myChar, "' is a Vowel.")
else:
    print("'",  myChar, "'is a Consonant.")

Output of program

Enter a character: A
' A ' is a Vowel.



Pattern based on given value of N

Python while loop

Pattern based on given value of N.

Sample output for N=5

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Python Code:

number = int(input("Enter number of lines to be printed: "))
count = 0
while count < number:
  print(" * * * * * ")
  count = count + 1

Output of program

Enter number of lines to be printed: 5
 * * * * * 
 * * * * * 
 * * * * * 
 * * * * * 
 * * * * * 



Sunday 12 April 2020

Python if else MCQs Set 1 Answer

Python if else MCQs Set 1 Answer




Question 1. If the Boolean expression of if statement evaluates to ________, then the block of code inside the if statement will be ignored.
A. True
B. False
C. both True and False
D. none of these
ANSWER: B. False 

Question 2. Predict output of the following python program.
a, b, c = 5, 1, 15

if (a < b) and (a < c):
  print("A is minimum")
elif (b < a) and (b < c):
  print("B is minimum")
else:
  print("C is minimum")
A. A is minimum
B. B is minimum
C. C is minimum
D. none of these
ANSWER: B. B is minimum 

Question 3. Predict output of following python code:
if(4-6):
   print("Android")
else:
   print("Linux")


A. Android
B. Linux
C. Error
D. none of these
ANSWER: A. Android 

Question 4. What will be the output of the following python program?

    if False:
       print ("AAA")
    elif True:
       print ("BBB")
    else:
       print ("CCC")

A. AAA
B. BBB
C. CCC
D. none of these
ANSWER: B. BBB  

Question 5. What will be the output of the following python program?


    result = 50
    if(result != 50):
       flag=0
    else:
       flag=1
    if(flag==0):
       print("Pass")
    else:
       print("Fail")

A. Pass
B. Fail
C. Syntax Error
D. none of these
ANSWER: B. Fail   


Python Basics MCQs - Set 1 Answer

Python Basics MCQs Set-1 Answers



Top of Form
Question 1. The _______ are names given to some data or information that we want to store in a Python programs.
A. variables
B. tokens
C. keywords
D. none of these

ANSWER: A. variables



Question 2. Which of the following character can be used to specify comments in Python?
A. #
B. $
C. //
D. none of these

ANSWER: A. #



Question 3. The _______are used to describe the code and are not interpreted by Python.
A. variables
B. comments
C. tokens
D. loops

ANSWER: B. comments



Question 4. A variable name cannot start with a __________.
A. number
B. letter
C. underscore
D. none of these

ANSWER: A. number



Question 5. Which of the following is invalid identifier name?
A. _num
B. Num
C. 101_
D. Syntax

ANSWER: C. 101_



Back to Python MCQs >

Saturday 14 March 2020

MCQs Python while loop Set 1

MCQs based on Python while loop




Question 1. What will be the output of following python while loop code?
num = 1
while num < 5:
  print(num, end=" ")
  num = num + 1

A. 1 2 3 4
B. 1 2 3 4 5
C. 1 3 5
D. none of these

Question 2. What is the output of following python while loop code?
    num = 1
    while num < 5:
      print(num, end=" ")
      if num == 2:
        break
      num += 1
A. 1 3 4
B. 1 2
C. 1 2 3 4 5
D. none of these

Question 3. What is the output of following python while loop code?

    num = 1
    while num < 5:
      num += 1
      if num == 2:
        continue
      print(num, end=" ")
A. 1 2 3 4
B. 1 2
C. 3 4 5
D. Syntax error

Question 4. Predict output of the following python while code.

    num = 1
    while num < 5:
        print(num, end=" ")
        num = num + 1
    else:
        print("'num' is now out of range.")
A. 1 2 3 4 'num' is now out of range.
B. 1 2 3 4
C. 'num' is now out of range.
D. none of these

Question 5. Predict output of the following python while code.

    flag = 1

    while flag == 1:
        print(flag + 1)

    print("1 2 3 4 5")
A. infinite loop
B. 1 2 3 4 5
C. 2 3 4 5
D. Syntax error


   


Score out of 5 =  

Percentage =


Back to Python MCQs >











Friday 13 March 2020

Python for loop MCQs Set 1

MCQs based on Python for loop



Question 1. Predict output of following python code:


    str = 'pythonpracticals'
    for i in str:
        print(i.upper(), end="")
A. PYTHONPRACTICALS
B. python
C. pythonpracticals
D. none of these

Question 2. What is the output of the following python code?

    str = 'pythonpracticals'
    for i in str[0:6]:
        print(i, end="")
A. pythonpracticals
B. python
C. practicals
D. error

Question 3. What is the output of the following?

    number = 5
    sum=0
    for i in range (1,5):
        sum=sum+i
    print(sum)
A. 1
B. 5
C. 10
D. error

Question 4. Predict output of following python code:

    dict1 = {0: 'a', 1: 'b', 2: 'c', 3:'d'}
    for i in dict1.values():
        print(i, end="") 
A. abcd
B. 0123
C. 0a1b2c3d
D. error

Question 5. Predict output of following python code:

    print("Hello")
    for i in range(5, 1):
       print (i)
A. Hello5 4 3 2 1
B. Hello1 2 3 4 5
C. Hello5 4 3 2
D. Hello


   


Score out of 5 =  

Percentage =


Back to Python MCQs >











Thursday 12 March 2020

MCQs based on Python if else set 1

MCQs based on Python if else




Question 1. If the Boolean expression of if statement evaluates to ________, then the block of code inside the if statement will be ignored.
A. True
B. False
C. both True and False
D. none of these

Question 2. Predict output of the following python program.
a, b, c = 5, 1, 15

if (a < b) and (a < c):
  print("A is minimum")
elif (b < a) and (b < c):
  print("B is minimum")
else:
  print("C is minimum")
A. A is minimum
B. B is minimum
C. C is minimum
D. none of these

Question 3. Predict output of following python code:
if(4-6):
   print("Android")
else:
   print("Linux")


A. Android
B. Linux
C. Error
D. none of these

Question 4. What will be the output of the following python program?

    if False:
       print ("AAA")
    elif True:
       print ("BBB")
    else:
       print ("CCC")

A. AAA
B. BBB
C. CCC
D. none of these

Question 5. What will be the output of the following python program?


    result = 50
    if(result != 50):
       flag=0
    else:
       flag=1
    if(flag==0):
       print("Pass")
    else:
       print("Fail")

A. Pass
B. Fail
C. Syntax Error
D. none of these



Score out of 5 =

Score in percentage =

Check Your Answer Here






Tuesday 10 March 2020

MCQs based on Python Basics - Set 1

MCQs based on Python Basics - Set 1




Question 1. The _______ are names given to some data or information that we want to store in a Python programs.
A. variables
B. tokens
C. keywords
D. none of these

Question 2. Which of the following character can be used to specify comments in Python?
A. #
B. $
C. //
D. none of these

Question 3. The _______are used to describe the code and are not interpreted by Python.
A. variables
B. comments
C. tokens
D. loops

Question 4. A variable name cannot start with a __________.
A. number
B. letter
C. underscore
D. none of these

Question 5. Which of the following is invalid identifier name?
A. _num
B. Num
C. 101_
D. Syntax


   


Score out of 5 =  

Percentage =

Check Your Answer Here

Back to Python MCQs >











Monday 17 February 2020

Python program for Simple Calculator

Python program to demonstrate simple calculator.


print("=== Python Simple Calculator ===")

print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")

choice = input("\nEnter your choice: \n")

num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

print("\nAnswer for Given Inputs:")

if choice == '1':
   print(num1, "+", num2, "=", num1 + num2)

elif choice == '2':
   print(num1, "-", num2, "=", num1 - num2)

elif choice == '3':
   print(num1, "*", num2,"=", num1*num2)

elif choice == '4':
   print(num1, "/", num2, "=", num1/num2)
else:
   print("Invalid input.")

Output of program

=== Python Simple Calculator ===
1. Addition
2. Subtraction
3. Multiplication
4. Division

Enter your choice: 
1
Enter number 1: 5
Enter number 2: 6

Answer for Given Inputs:
5 + 6 = 11

Tuesday 4 February 2020

Python String Methods ( Functions )

Popular Python String Methods ( Functions )


        Method         Description
  1. count() It returns the number of times a specified value occurs in a string
  2. capitalize() It converts the first character of a string into upper case
  3. casefold()   Converts string into lower case
  4. endswith() Returns true if the string ends with the specified value
  5. find() Searches the string for a specified value and returns the position of where it was found
  6. index() Searches the string for a specified value and returns the position of where it was found
  7. isdigit()        Returns True if all characters in the string are digits
  8. islower()      Returns True if all characters in the string are lower case
  9. isalnum()     Returns True if all characters in the string are alphanumeric
  10. isalpha()      Returns True if all characters in the string are in the alphabet
  11. isdecimal() Returns True if all characters in the string are decimals
  12. isspace()      Returns True if all characters in the string are whitespaces
  13. istitle()        Returns True if the string follows the rules of a title
  14. isupper()      Returns True if all characters in the string are upper case
  15. join() Joins the elements of an iterable to the end of the string
  16. ljust() Returns a left justified version of the string
  17. lower() Converts a string into lower case
  18. upper() Converts a string into upper case
  19. lstrip()         Returns a left trim version of the string
  20. replace()      Returns a string where a specified value is replaced with a specified value
  21. rsplit()         Splits the string at the specified separator, and returns a list
  22. rstrip()         Returns a right trim version of the string
  23. startswith() Returns true if the string starts with the specified value
  24. swapcase() Swaps cases, lower case becomes upper case and vice versa
  25. title() Converts the first character of each word to upper case
  26. strip() Returns a trimmed version of the string



Monday 27 January 2020

Python program to find simple interest

Python program to find simple interest

Concept to learn: Python Basics


# Python program to find simple interest for the given principal amount, rate of interest and time in number of years.

# Inputs for P, R, N
P = 1000
R = 12
N = 1

# Calculating simple interest(SI)
SI = (P * R * N) / 100

# Print SI for the given values
print("Simple interest = ", SI)

Output of Program

Simple interest = 120.0