Learn Python 3 Basics and its Benefits:
Hello readers, welcome back to Learn Python 3 Basics and its benefits. This article strives the Basic concepts in Python and where it really benefits. Python programming is easy to learn and powerful language having good features.
As already discussed Python scripting in the previous article as it serves 3 different ways of programming with high-level data structures.
This article provides the definition and usage of Top 5 Python 3 Basic concepts in the real-time projects.
Top 5 Python Basics:
1. Syntax and Semantics
2. Python Interpreter
3. Data Types and Variable Declarations
4. Decision Making and Control Flow Tools
5. Exceptional Handling
Syntax and Semantics:
1. Python programming follows simple syntax and semantics as other programming languages have.
2. As it is powerful and general purpose language it follows indentation rules with a simple syntax.
3. Python is a dynamically typed language which used to write direct scripting and allows to develop Applications in different Platforms.
4. You can write the automated scripts using python easily and automate the task.
5. Python offers more support for huge programs and structure than shell scripts or batch scripts.
Let us see the small example for ‘Hello World’ program. The syntax of Python 3+:
Save this file as ‘MyFirstScript.py’ and write the above statement. OR Directly type in Python Interpreter.
1. It is the runtime environment and execution interface which implicitly comes with the Python installation setup.
2. Python Interpreter supports the direct scripting and functions to write and execute. It is easy to use and handles the exceptions with error messages.
3. Python Interpreter is extended with all new functions and it’s data types and libraries which is implemented by other programming languages.
4. Python Syntax and semantics follows C or C++ language mostly. If you are already a C or C++ programmer Python is easy to learn.
5. Python source files are processed as encoded in UTF-8 standards by default.
If you are a newbie don’t worry just go through some C Basics and I assure you will learn Python easily in no time and work on real-time Projects
Python programs are much shorter than other programming languages and easily readable.
Data Types and Variable Declarations:
1. Python supports few data types like Numbers, String, and Lists(This is included in Python Data Structure).
2. By default, Python supports int, float and str data types.
3. It automatically identifies the data type which you have declared on the right-side using assignment variable.
4. In advance, Python supports other types of numbers like Decimal values, Fractional values, and Complex Numbers.
Arithmetic Operators, Relational Operators, and Assignment Operators
The ‘list’ also comes into Data Types but it is in Data Structures. This will be explained in the next upcoming article.
Decision Making and Control Flow Tools:
1. Python has very good decision making statements, where it is better than other programming languages.
2. Python decision making statements can check the data types and conditions on Arithmetic Operators, Bitwise operators and lot more.
3. It also checks the non-assigned variable or NaN values or variables. Even a lot more can do using Python decision making statements.
1. if statements: Checks the only True or False condition with variables or constants.
a=2 if a == 2: print('The True condition is executed')
2. if else statements: Check the True and False conditions with the variables or constants
if a!=2: print('Variable a is not equal to 2') else: print('Variable a is equal to 2')
3. if elif statements: This statement allows to check more than one condition with the variables or constants
if a!=2: print('Variable a is not equal to 2') elif a > 2: print('Variable a is greater than 2') else: print('Variable a is equal to 2')
4. You can also use nested if statements, this mainly depends on the inner loops and inner conditions.
Will update this context in the next article ‘Decision Making Statements’ in Python.
Python has very good control flow loops which allow to iterate the list, set, dictionaries and more on numbers.
Here are few strong loops in Python, they are For loop and While loop.
The syntax of For Loop:
num = 1 for num in range(11): print(num)
The syntax for While Loop:
num = 2 while num in range(10): print(num) num += 1
Here ‘range’ is a built-in function which is used for iteration. For iterating the values Iterators and Generator are both built-in functions and used in loops.
Sometimes we often get errors even after the syntax and semantics are correct when we execute. The errors raised at run-time are called exceptions, to handle those errors python has good exception handling functions.
1. Syntax errors are first most errors in Python, and the interpreter raises the error message even without using exceptions.
You can write the program to handle those exceptions. Please look at the exceptions.
The program which may raise errors is written in ‘try’ block and errors are handled in ‘except’ block.
def divide(a, b): try: value = a / b except ZeroDivisionError: print("division by zero Error!") else: print("The Value is", value) finally: print("executing finally clause") divide(10,2)
First ‘try’ block executes, if you get zero division error then it executes ‘except’ block.
If you don’t get any zero division error, else clause executes and ‘finally’ block executes.
You can write the ‘except’ block in this way with arguments.
except (RuntimeError, TypeError, NameError): print(RuntimeError, TypeError, NameError)
Click here for the PEP 8 Rules
Click here for the more info about Python 3 Basics