As the heading suggests, the objective of this blog is to explain the basics of Python in a very simple way through which you will be easily able to understand this language without any prerequisites. This blog aims to cover some of the very important building blocks of Python language such as simple numerical operations, floats, strings, simple input and output, string operations, type conversions, variables, and in-place operators. So, without any further delay, let’s begin!
The first question that arrives in the mind is, “What is Python?”
Python is a high-level programming language, object-oriented, and with dynamic logic, which makes it enticing for web programming, scripting, and scientific computing. It is designed to be easy to read and implement. It is very popular and used by various organizations like Google, NASA, Disney, etc.
Let’s Get Started
Let’s set out by using a simple print statement that displays “Hello World!”
We use the print statement to get this output:
>>> print(“Hello World!”)
This print statement can even be accustomed to get an output of multiple statements:
>>> print(“Hello World!”)
>>> print(“My Name is”)
My Name is
Simple Operations in Python
Hence, we’ve learned a way to execute the print statements and find the output. Now, we will focus on some easy operations performed in Python. Python has the capability of performing simple calculations. All you would likely need to do is to enter the calculation into the python console directly, and it will provide you with the output.
Python carries out operations like multiplication and division using an asterisk for multiplication and forward slash for division. Python performs operations on negative numbers just as the operations are performed on the positive ones. For example:
>>> print(-4*6 + 7*(-3)*(-5))
Zero Division Error
Division by 0 results in a zero division error:
ZeroDivisionError: division by zero
In the article above, we saw the division of two integers, which results in the formation of float (simply stated, a number with a decimal). Floats can be added to integers as the compiler automatically converts integers (int) to float.
Python and Exponents
Other than addition, multiplication, division, and subtraction, Python can perform exponential tasks, i.e., raising one number to its own or any other. To perform this task, we use double asterisks (**). For example:
Quotient and Remainder
To find the quotient and remainder of the division operation, we use floor division and modulo, respectively. We perform floor division by using double forward slash // and modulo using %. For example:
Getting away from numbers, if you want to use text in Python, you have to use “string.”
>>> print(“Python In Loginworks”)
Python In Loginworks
>>> print(‘Let us learn Python’)
Let us learn Python
By entering text between two double quotation marks, we create a string. Some characters cannot be directly included in the quotation mark. To tackle these types of characters, we place a backslash before them. While we cannot use it to escape the new line (\n). For example:
>>> print(” Python ain\’t that hard, but it ain’\t that easy either “)
Python ain’t that hard, but it ain’t easy either.
Moreover, python provides an easy way to avoid using \n repeatedly for the new line. Instead of \n, we create a string with three sets of quotes in which a new line is created by pressing enter automatically. For example:
>>> print(“””Customer: I need a chocolate
Shopkeeper: Here it is!”””)
Customer: I need a chocolate
Shopkeeper: Here it is!
Hence, the second line of the conversation automatically enters a new line without even using \n; therefore, it is more convenient to use three sets of quotes instead of a single one.
Integers, floats, and strings can also be added through the process of concatenation. The concatenation of two strings does not require whether they’ve been created in a single or double string.
>>> print(“Hello” + “World”)
>>> print(“String 1” + “,” + “String 2”)
String 1,String 2
Note: You cannot concatenate strings with integers, as they are two different entities.
TypeError: unsupported operand type(s) for+: ‘int’ and ‘str’
Strings can also be multiplied, and the result is the repeated version of the original string. As shown in the example below:
Conversion of Type
To accomplish certain tasks, for instance, adding two strings “1” and “2” to get the desired result of “3”, this can only be accomplished by using type conversion. While the addition of these two strings mentioned above would concatenate and result in “12,” but changing the type of these strings to int would produce the desired result. For example:
>>> print(“1” + “2”)
Converting strings to int and giving the print function as input, we get:
>>> print(int(“1”) + int(“2”))
Another example is to convert str to float, but before that, we need to understand what is input() function. This function takes the user’s input and then evaluates the expression and hence, automatically finds out whether it is a string, a list, or a number. To convert str to float all we need to do is-
>>> print(float(input(“x: “)) + float(input(“y: “)))
>>> x: 5
>>> y: 8
Note: Passing float or non-integer values result in an error.
Variables play a significant role in the Python language. A variable allows you to store value by assigning a name to it, and the name assigned can be used to refer to the value later as per the code demands. To use variables we need to use is equal to (=) sign.
Hence, you can use mathematical operation with variables just as you did with numbers and strings. You can assign different values to a single variable as many times as you want.
x= “I am a String”
I am a String
There are certain restrictions you need to keep in mind while using the name of your variable. The only characters that are allowed to be used in a variable are letters, numbers, and underscores; also, they can’t start with numbers.
SyntaxError: invalid syntax
Hence, the result is an error.
We could have had a great shot at Python if we could’ve just picked some of the key sub-topics from above and implemented it. This blog covers some of the deep roots in Python. However, it’s always good to implement a few of them than none. Well, we began with the simple Print command, coming to some mathematical operations and then to exponential functions, finally approaching the strings in Python. From there, we went to conversion types and very important variables. These basics were designed for people with no previous knowledge of any computer language, and we tried our best to explain as easily as possible. But there is always a scope of improvement, and hence, we would love to receive any suggestions and comments from you.
“In What Other Ways Can We Make Python Easier?”
To Know More Visit Loginworks Softwares Inc
- Business Intelligence Vs Data Analytics: What’s the Difference? - December 10, 2020
- Effective Ways Data Analytics Helps Improve Business Growth - July 28, 2020
- How the Automotive Industry is Benefitting From Web Scraping - July 23, 2020