Relational Operator in Python

Relational Operator in Python



Symbol
Description
Example 1
Example 2
<
Less than

>>>7<10

True

>>> 7<5

False

>>> 7<10<15

True

>>>7<10 and 10<15

True

>>>"Hello"< "Goodbye"

False

>>>'Goodbye'< 'Hello'

True

>Greater than

>>>7>5

True

>>>10<10

False

>>>„Hello‟> „Goodbye‟

True

>>>'Goodbye'> 'Hello'

False

<=
less than equal to

>>> 2<=5

True

>>> 7<=4

False

>>>"Hello"<= "Goodbye"

False

>>>'Goodbye' <= 'Hello'

True

>=greater than equal to

>>>10>=10

True

>>>10>=12

False

>>>"Hello">= "Goodbye"

True

>>>'Goodbye' >= 'Hello'

False

! =, <>
not equal to

>>>10!=11

True

>>>10!=10

False

>>>‟Hello‟!= "HELLO"

True

>>> "Hello‟ != "Hello‟

False

==
equal to

>>>10==10

True

>>>10==11

False

>>>"Hello" == "Hello"

True

>>>"Hello" == "Good Bye"

False

Example: To test Relational Operators:


#Demo Program to test Relational Operators
a=int (input("Enter a Value for A:"))
b=int (input("Enter a Value for B:"))
print ("A = ",a," and B = ",b)
print ("The a==b = ",a==b)
print ("The a > b = ",a>b)
print ("The a < b = ",a<b)
print ("The a >= b = ",a>=b)
print ("The a <= b = ",a<=0)
print ("The a != b = ",a!=b)
#Program End


Output:


Enter a Value for A:35

Enter a Value for B:56

A = 35 and B = 56

The a==b = False

The a > b = False

The a < b = True

The a >= b = False

The a <= b = False

The a != b = True

Qus. What are operators? What is their function? Give examples of some unary and binary operators.

Ans: “Operators are those symbols used with operands, which tells compiler which operation is to be done on operands.‖ in other words – ―operators are tokens that trigger some computation/action when applied to variables and other objects in an expression.‖

Operators are of following types:

  •  Unary operators like (+) Unary Plus, (-) Unary Minus, not etc.
  •  Binary Operators like (+) addition, (*) multiplication, and etc.

Qus. What is block/code block/suit in Python?

Ans: Sometimes a group of statements is part of another statement of function. Such a group of one or more statements is called block or code-block or suit in python. e.g.


Qus. What is the role of indentation in Python?

Ans: Indentation plays a very important role in Python. Python uses indentation to create blocks of code. Statements at same indentation level are part of same block/suit. You cannot unnecessarily indent a statement; python will raise an error for that.

Qus. How many types of strings are supported by Python?

Ans: Python supports two types of strings:

(i) Single-line string That terminates in single line.

(ii) Multi-line String That stores multiple lines of text.

Qus. What will be the sizes of following constants?

(a) "\a‟ (b) “\a” (c) “Kumar\‟s” (d) "\‟ (e) “it‟s”

Ans: (a) 50 (b) 50 (c) 56 (d) 50 (e) 53

This screenshot is the output of the above question.


Qus. How can you create multi-line strings in Python?

Ans: We can create multi-line string by putting a backslash (\) at the end of line which allows you to continue typing in next line in same string.

Qus. Which of the following are syntactically correct strings? State reason.

(a) ”Python is nice Language”

(b) „He called me “Friend!” when he came‟

(c) “Very Good‟

(d) „This is a good book‟

(e) “Namaste

(f) “I liked „Harry Potter‟ very much”

Ans: (a) Correct (b) Correct (c) Incorrect (d) Correct (e) Incorrect (f) Correct


Qus. What is the error in following Python program with one statement?

print(“My name is : “, name)

suggest a solution

Ans: Error is : ―name 'name' is not defined‖. And the solution is to declare the variable-name before this statement.

Qus. Predict the output of the following:

Ans: Output: 17 5


Qus. What are augmented assignment operators? How are they useful?

Ans: An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + (1). Similar constructions are often available for various binary operators. They are helpful in making the source code small.


Qus. What are loops in Python? How many types of loop are there in Python?

Ans: Loops are iteration constructs in Python. Iteration means repetition of a set of statements depending upon a condition test. Loops has three basic elements within it to repeat the statements –

  • Initialization (Start)
  • Check Condition (Stop)
  • Updation (Step)

Python provide two types of loop

(i) Conditional Loop while( (Condition based loop)

(ii) Counting loop for (loop for a given number of times).


Qus. What is the syntax of if-elif statement in Python?

Ans: The syntax of if-elif statement in python is as follows:

If condition1:

#code-block of statements when condition1 is true

elif condion2:

#code-block of statements when condition2 is true

elif condition3:

#code-block of statements when condition3 is true
.

.

.

else:

#code-block of statements when all above conditions are false.


Qus What are jump statements in Python? Name jump statements with example.

Ans: Python offers two jump statements to be used with in loops to jump out of loop-iterations.

These are break and continue statements.


Programs

Write a Program to obtain temperature in Celsius and convert it into Fahrenheit using formula –

C X 9/5 + 32 = F


View Solution


WAP to read todays date (only date Part) from user. Then display how many days are left in the current month.


View Solution


WAP to print the area of circle when radius of the circle is given by user.


View Solution


WAP to print the volume of a cylinder when radius and height of the cylinder is given by user.


View Solution


WAP that asks your height in centimeters and converts it into foot and inches.


View Solution


WAP to accept 3 side of triangle and find area of a triangle.


View Solution


WAP to input principle amount,rate time and calculate simple interest.


View Solution


WAP to read a number in n and prints n^2, n^3, n^4


View Solution


WAP to calculate compound simple interest after taking the principle, rate and time.


View Solution


WAP to take two numbers and check that the first number is fully divisible by second number or not.


View Solution


WAP to take value of x,y,z from the user and calculate the equation 


View Solution


WAP to take the temperatures of all 7 days of the week and displays the average temperature of that week.


View Solution


WAP to check the given year is leap year or not.


View Solution


WAP that searches for prime numbers from 15 through 25.


View Solution


WAP to test if given number is prime or not.


View Solution


WAP to compute the result when two numbers and one operator is given by user.


View Solution


WAP to calculate the roots of a given quadratic equation.


View Solution


WAP to input a digit and print it in words.


View Solution


WAP to check whether square root of a given number is prime or not.


View Solution


WAP to print first n odd numbers in descending order.


View Solution


WAP to find the average of the list of the numbers entered through keyboard.


View Solution


WAP to find the largest number from the list of the numbers entered through keyboard.


View Solution


WAP to find the sum of n natural numbers.


View Solution


WAP to find the sum of first n even numbers.


View Solution


WAP to find the sum of first n odd numbers.


View Solution


WAP a program to generate a list of elements of Fibonacci Series.


View Solution


Program to find the simple interest based upon number of years. If number of years is more than 12 rate of interest is 10 otherwise 15.


View Solution


Write a program to input any choice and to implement the following.

Choice Find

1. Area of square

2. Area of rectangle

3. Area of triangle


View Solution


Write a program to input any number and to print all natural numbers up to given number.


View Solution


Write a program to input any number and to find sum of all natural numbers up to given number.


View Solution


Write a program to input any number and to find reverse of that number.


View Solution


Write a program to input any string and count number of uppercase and lowercase letters.


View Solution


Python program to obtain three numbers and print their sum.


View Solution


Python program to obtain length and breath of a rectangle and calculate its area


View Solution


Python program to calculate (BMI) body mass index of a person

Body Mass Index is a simple calculation using a person's height and weight. The formula is BMI=kg/m^2 where kg is a person's weight in kilogram and m^2 is the height in meter squared.


View Solution


Write a program to input a number and print its cube.


View Solution


Write a program to input two number and swap them.


View Solution


Write a program to input three numbers and swap them as this : 1st number becomes the 2nd number, 2nd number becomes the 3rd number and 3rd number becomes the first number.


View Solution


Write a program to read three numbers in three variables and swap first two variables with the sums of first and second ,second and third number respectively.


View Solution


Write a program to input a single digit(n) and print a 3 digit number created as <n(n+1)(n+2)> e.g., if you input 7, then it should print 789. Assume that the input digit is in range 1-7.


View Solution