In the last article we have gone through the definition of programming languages, have discussed the Python programming language along with it’s basic syntax, benefits, usage and some concepts which Python follows. In today’s article we will have a look at the python programming structure, some more statements, and other fundamental python concepts.
Python terminologies | Python programming structure
Python follows the PEDMAS(Parentheses, Exponent, Multiplication, Division, Addition & Subtraction) precedence for handling operators/expressions to be evaluated. To remember this abbreviation sometimes people use the mnemonic ““Please Excuse My Dear Aunt Sally” . Let us follow this precedence to evaluate an expression.
((((12+6)*7)-20)/2)-8*2 To solve this expression we follow the PEDMAS pattern, hence: Step 1: (12+6) = 18 Step 2: (18*7) = 126 Step 3: (126-20) = 106 Step 4: (106/2) = 53 Step 5: 53 - 8*2 = 53 -16 Step 6: 53-16 = 37
Keywords in python are the reserved words which have been given a definite meaning in order to perform a specific task. Since these words have already been utilized in the programming language, variables in python should ideally not be defined with the same name as the keywords. Some of the keywords in the python programming language are: for, while, if, else, del, and, or, not. In the latest version of Python(3.7) there are 33 reserved keywords.
These are the mainly the user defined variables, their purpose is to store some data or values. These variables are stored in some memory location and retrieved when the variable name is specified. Variable names are case-sensitive. Certain rules exist for naming variables in Python.
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) .
- Variable names are case-sensitive (age, Age and AGE are three different variables).
- As discussed earlier, variable names cannot be the same as keywords.
To avoid repetition/rewriting of the same line of statements multiple times in a program, several programming languages have functions or procedures. Functions are a reusable block of statements. In python the ‘def‘ keyword is used to define a function. To call/use the function we mention the function name followed by closed parenthesis.
def new_function(): print("New Function") new_function()
We can also modify the function execution by passing arguments/parameters to a function.
Classes & Objects
Python is an object-oriented programming language, in OOP the program is mainly implemented through the use of objects which interact with each other. Classes are mainly blueprints which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values.
#Python class definition class MyClass: x = 5 #Creation of python object p1 p1 = MyClass() print(p1.x)
Modules & Packages
Modules behave similarly to the libraries found in other languages and are utilized to extend or implement certain pre-defined functions into the Python program .Modular programming refers to the process of breaking a longer, complex programming task into separate, smaller set of tasks. Individual modules can then be cobbled together like building blocks to create a larger application.
A package is a hierarchical directory structure that defines a application environment that consists of modules and sub packages, and other required files. To import modules in Python we use the ‘import’ keyword, suppose we want to import a module which is part of a package, we use the import keyword followed by the package name, a . symbol and then specify the module name at the end of the statement. Let us look at an example:
#Importing separate module import mod #Importing modules which are within a package import pkg.mod1, pkg.mod2
In the next article we will have a look at downloading/installing python, configuration options, and how python handles the code execution or translates the code into machine understandable format.