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
119
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
Enter an integer number: 54
Enter another integer number: 65
5465
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
Enter an integer number: 54
Enter another integer number: 65
119
Now we get the correct answer.
Operators in Python:
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
- ‘
Example:
a = 5
b = 4
print(a & b)
OUTPUT
4
In binary form:
a = 0101
b = 0100
After AND operation, the output is 0100
, which is decimal 4.
- Membership Operators:
- ‘
in
‘ and ‘not in
‘
- ‘
Example:
str = "data"
print('a' in str)
OUTPUT
True
Artificial Intelligence in the Aviation and Airlines Sector
AI is transforming the aviation industry, enhancing safety, operational efficiency, and customer experience. Sazit Suvo Designer & Editor Aviation Sector and…
Who Is an AI Engineer? Career, Work, Challenges, and Required
AI Engineer is one of the most in-demand careers today. An AI Engineer designs, develops, and implements artificial intelligence (AI)-based…
Top SQL Keywords and Functions: Master 80% of Data Engineering
SQL is a fundamental skill for data engineering and analytics. With the right understanding of a few key concepts, you…
Statistics and Machine Learning: Relationship and Importance
Statistics and Machine Learning Although statistics and machine learning may sound different, they are deeply related. On one hand, statistics is…
Machine Learning and Deep Learning :Step-by-Step Learning Guide.
Machine Learning (ML) and Deep Learning (DL) Step-by-Step Learning Guide and Job Preparation Are you interested in learning Machine Learning or…
Do You Need to Learn Math First to Start Data
Should You Learn Math First or Alongside ML? “Do I need to learn Math first to start Data Science or Machine…