- Python is a programming language.
- Creator : Guido van Rossum
- First release : 1991
- Python is an easy and powerful programming language with elegant syntax and numerous libraries in various fields which makes python one of the useful and easy programming languages.
- Is Python high-level Programming Language?
Python is a high-level, general-purpose programming language.
- What is a high-level programming language?
- high-level refers to programs that are more independent of a particular type of computer, i.e they are convinient for the programmer to write code.
- low-level refers to as machine languages or assembly languages.
Applications of Python
- Web Development, Frameworks like Django, Flask and more.
- Numerical and Scientific Computations, Packages like numpy, Scipy.
- Applications in Data Science and Machine Learning.
- GUIs (Graphical User Interface) for Desktop.
- For Web Automation, Frameworks such as Selenium.
- Building Chatbots for Online Services and more.
Comments in Python
# Single line comments start with 'number sign' or 'hash'. """ Multi line comments are written using 3 Quotation marks("). Comments are usually used to understand various parts of the code. They are mostly used in documentation section. """
Datatypes are everywhere in programming. They are one of the most important concepts of programming.
Variables are used to store data. We can store different types of data and use them for different operations. In Python, the data type is set when the value is assigned to a variable.
Commonly used Datatypes :
|numbers||int, float and complex|
|Data Structures||lists, tuples, sets, dictionaries etc..|
Open python console and assign any type of data to a variable with any name.
We can get the type of the variable by using type() in python.
In this example, we have assigned "1" to variable "i". Python sets the data type to int by default when you assign an integer value to the variable.
>>> i = 1
>>> type(i) <class 'int'>
The same way we can assign a float and string type data to a variable.
>>> pi = 3.14
>>> type(pi) <class 'float'>
Here the type of variable pi is set to float as we assign a decimal value to the variable.
The type of variable "var" is set to string by default.
>>> var = "Let's learn Python"
>>> type(var) <class 'str'>
Let's try assigning a boolean value to the variable and check the output.
>>> var = True
>>> type(var) <class 'bool'>
The type of variable is now set to boolean.
# Assign any value to two variables >>> a = 100 >>> b = 10 # '+' gives the sum >>> a + b 110 # '-' gives the difference >>> a - b 90 # '*' gives the product >>> a*b 1000 # '/' gives the quotient, the result of division is always float type. >>> a/b 10.0 >>> -a/b -10.0 # Integer Division '//' gives the int output. >>> a//b 10 >>> -a//b -10 # Modulo Operator '%' gives the remainer >>> a%b 0 # Exponential Operator '**' gives a to the bth power >>> a**b 100000000000000000000 # Precedence using Parentheses >>> a + b*2 120 >>> (a + b)*2 220
"and" and "or" are called Boolean Operators, they are case-sensitive
>>> True and True True >>> True and False False >>> True or True True >>> True or False True
How they work?
As you can see "and" gives True when both the given expressions are same.
Where as "or" outputs True when any one of the given expressions is True, and False when both the cases are False.
"True" and "False" are actually "1" and "0" respectively, so we can use "1 instead of True" and "0 instead of False".
For example :
>>> 1 and 1 1 >>> 1 and 0 0 >>> 1 or 1 1 >>> 1 or 0 1 >>> True * 100 100 >>> False + True 1
Comparision operators are useful when using conditional statements and loops.
The "==" is the Equality and "!=" is Inequality, other comparision operators include <, >, >=, <=.
>>> 100 == 100 True >>> -100 == 100 False >>> 22/7 != 3.14 True >>> True == 1 True >>> 2 < 1 False >>> 99 >= 99 True >>> 99 > 98 and 100 > 99 True
Spaces and Tabs at the beginning of a code to determine which statement does the code belong to, is known as Indentation. The number of spaces must be same and shouldn't differ for each line. Usually 4 spaces are considered as an indent in Python.
>>> if 1 == 1: ... print("They are Equal.") ... print("Python will throw an error if the code inside 'if statement' isn't indented.") ... They are Equal. Python will throw an error if the code inside 'if statement' isn't indented.
What happens if we don't use indentation?
IndentationError: expected an indented block
If we try using the print statement without indentation(spaces) inside the if condition, python throws an error that it is expecting an indent before the print statement. It is important to indent any code inside a statement to tell python that which code belongs to which statement or the above is thrown everytime.
Flow Control Statements
What is Flow Control?
Flow Control is the order in which the each statement in the code is executed.
The if, if-else, for, while statements are called the flow control statements.
Generally, we all know that if refers to a condition. It is similar in programming as well.
Let's understand this by suitable example.
>>> x = -10 >>> if x > 0: ... print("x is a positive number.") ... elif x < 0: ... print("x is a negative number.") ... else: ... print("x is equal to zero.") ... x is negative
If the condition passed through the "if statement" is True then the code inside it is executed.
The "elif statement" is short for "else if", we can pass a condition through it and if the "if statement returns False" the code inside the elif statement is executed.
The "else statement" is optional but if all other conditions return False then the code inside the else statement is executed.
In the present case :
The given number (x) is -10 .
It goes to the if condition and checks if the condition (x > 0) is satisfied, if it is True it prints "x is a positive number".
But as the condition is not True here, it goes to the elif statement and the condition here (x < 0) is satisfied as -10 is less than 0, the code inside the elif statement is executed. So it prints "x is a negative number" .
The else part is ignored as the elif condition is satisfied.
for, while, do are generally termed as loops.
A loop is nothing but a piece of code iterating over and over again until a condition is satisfied. It's like doing the same thing again and again till we get a preferred output.
The term iterating means repeating the process continuously.
The function is used to generate a series of numbers. It's important to know the range() function, it comes in handy when we use for loops in python.
>>> for i in range(4): ... print(i) 0 1 2 3
The number passed through the range function i.e 4, won't be part of the output as it generates 4 values starting from 0.
The range by default starts from 0, however we can change the starting point in the range function.
>>> for i in range(2, 4): ... print(i) 2 3
Here the starting point is 2 and ending point is 4 which won't be included in the generated output.
[starting point, ending point)
"starting point is included" and "the ending point is excluded" from the output.
We can also specify the increment or decrement value, i.e we can specify the value to be added or subtracted each time while generating. It is usually called as step.
By default it is incremented by 1.
>>> for i in range(1, 10, 2): ... print(i) 1 3 5 7 9
As you can see it prints the odd numbers between 1 to 10 i.e the number is incremented by 2 everytime.
The range() function is not only limited to for loops but also in other senarios like in print, list, sum and more, which we'll know in upcoming sections.
As for loops can iterate over any sequence, we can use them on strings and lists as well.
>>> for i in [2,3,4,5]: ... print(i) 2 3 4 5
>>> for i in "Bug Routine": ... print(i) B u g R o u t i n e
The work flow statements like if-else, and for, are very useful as they can be used almost in any scenario in programming.
I hope this article was helpful for you, do share it with your friends.