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
 * * * * * 
 * * * * * 
 * * * * * 
 * * * * * 
 * * * * *