Python Input Function & Operator

Python Input Function & Operator

 

In the previous article, we discussed the introduction of Python and learned about the print function to display output results. We also learned about variables, how to create them, and their syntax.

In this article, we will discuss some important topics of the Python programming language.

Input Function

When we want to get any value from the user, we use the input function. This is a predefined function in Python. When our variable value is not given, we ask the user to input a value. When the user inputs an integer or string value, it will be stored in our variable. After that, we can use this value for our functional, logical, or arithmetic operations.

When we want to add two or more numbers:

a = 54
b = 65
sum = a + b
print(sum)

OUTPUT

Here we can see that both variables a and b have values. But what if the values of a and b are not given and depend on the user’s input? In this case, we use the input function to get a value from the user:

a = input("Enter an integer number: ")
b = input("Enter another integer number: ")
sum = a + b
print(sum)

OUTPUT

Here, we get the values of variables a and b from the user using the input function. But notice one thing: our values are stored in a and b, and our addition result is stored in the sum variable. However, our sum is not correct because of the data type. What is a data type? Basically, a data type is the type of value we input, whether it is a number or a character. There are various kinds of data types such as:

  • Integer (int): Numerical format (e.g., 8, 23, 2004)
  • Float: Decimal or fractional values (e.g., 8.29, 23.22, 87.5)
  • String: One or more characters together (e.g., “hello”, “123”)

When we get any value from the user using the input function, it is treated as a string. Anything you enter here is converted to a string value. We need to convert this string input value to an integer or float value. This conversion process is called typecasting, where data is converted from one type to another.

Now we use the typecasting method for our input value:

a = int(input("Enter an integer number: "))
b = int(input("Enter another integer number: "))
sum = a + b
print(sum)

OUTPUT

We will discuss mostly used operators in Python:

  • Arithmetic Operators:
    • +‘ for addition of numbers
    • -‘ for subtraction of numbers
    • *‘ for multiplication of numbers
    • /‘ for division of numbers
    • % ‘for modulus
    • **‘ for exponentiation (to the power of)
  • Relational & Assignment Operators:
    • ==‘ equal
    • <‘ less than
    • >‘ greater than
    • <=‘ less than or equal
    • >=‘ greater than or equal
    • =‘ assignment operator (to assign a value to a variable)
  • Logical Operators:
    • and‘ logical AND operation
    • or‘ logical OR operation
    • not‘ logical NOT operation
  • Bitwise Operators:
    • &‘ AND operation
    • |‘ OR operation
    • ^‘ XOR operation
    • ~‘ NOT operation’~ NOT operation
    • <<‘ left shift
    • >>‘ right shift
a = 5
b = 4
print(a & b)

OUTPUT

a = 0101
b = 0100
  • Membership Operators:
    • in‘ and ‘not in
str = "data"
print('a' in str)

OUTPUT

administrator

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *