Prerequisites: This is in continuation with the first part Python tutorial for Beginners
In this post we’ll delve deeper into the Python language’s concepts, structure and its syntax. We’ll learn about more complex parts of the language. In the later section, we’ll introduce Python’s most popular Machine Learning libraries. So let’s get started with the portion left from the previous post.
Decision Making
Decision making is predicting the conditions occurring while execution of the program and specifying actions taken according to those conditions. It is useful in situations where you have several inputs or expressions and you want different outputs for those inputs. Decision structures evaluate several expressions (starting with if, elif, else) which produce TRUE or FALSE (boolean value) as outcome. You determine which action to take and which statements to execute based on the outcome.
1 2 3 4 5 6 7 8 9 | #Code a = 10 if a = = 10 : print (a) elif a > 10 : print ( "Greater" ) else : print ( "Smaller" ) |
Output
1 2 | #Print Output 10 |
Note: People coming from languages like C or C++ might find the use of elif unusual. But it’s basically a shorter form of ‘else if’.
Loop Control
As we know code statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. But sometimes a situation may arise, when you need to execute a block of code several number of times before executing the next block.
A loop control instruction allows us to execute a portion of code either a specified number of times or until a particular condition is satisfied. Python language provide us with two methods for repeating a part of a program:
- while loop
1 2 3 4 5 6 | #Loop i = 0 while i< 5 : print (i) i = i + 1 |
Output
1 2 3 4 5 6 | #Print Output 0 1 2 3 4 |
- for loop
1 2 3 | #Loop for i in range ( 0 , 5 ): print (i) |
Output
1 2 3 4 5 6 | #Print Output 0 1 2 3 4 |
Loop control statements[1]
Loop control statements change the normal execution of the loop. We often come across situations where we want to exit the loop instantly before the next iteration or skip some part of the code for particular iteration. Loop control statements come for rescue, in such situations.
Let us go through the loop control statements briefly:
Python Standard Library Methods
Python provide us with a large set of useful library methods which comes in very handy while performing redundant tasks like “String manipulation” or “Complex mathematical calculations”. Let’s go through the most commonly used methods.
Math library methods
1 2 3 4 5 6 7 | #Math Functions import math as m print (m.floor( 5.6 )) 5 print (m.log( 10 )) 2.3025 |
- abs
1 2 3 | #Math Functions abs ( - 4 / 3 ) 1.33333333 |
- min
1 2 3 4 5 6 | #Math Functions min ( 1 , 2 , 3 ) 1 min ( 'b' , 'c' , 'a' ) a |
- max
1 2 3 4 5 6 | #Math Functions max ( 1 , 2 , 3 ) 3 max ( 'b' , 'c' , 'a' ) c |
- sorted
1 2 3 4 5 | #Math Functions a = [ 2 , 10 , 30 ] print ( sorted (a)) [ 2 , 10 , 30 ] |
- sum
1 2 3 4 5 | #Math Functions a = [ 2 , 10 , 30 ] print ( sum (a)) 42 |
String methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #String Manipulation str = "Hello" print ( len ( str )) 6 print ( str .lower()) hello print ( str .upper()) HELLO str = " cricket " print ( str .strip()) #removes white spaces 'cricket' |
Note: You can also concatenate methods
1 2 3 4 5 6 7 8 9 10 11 | #String Manipulation print ( str .strip().replace( "cr" , 'w' )) wicket #replaces substring/literal with substring/literal str = "Hello World" print ( str .split( ' ' )) [ 'Hello' , 'World' ] a = [ 'Hello' , 'World' ] print ( ' ' .join(a)) Hello World |
Quick Tip: Have you noticed that the original string str didn’t change even after performing the operations like split, lower & upper. This is because we didn’t assign it the value. You’ve to explicitly assign the value to the variables (using ‘=’ assignment operator) in order for change to reflect.
Python modules [2]
A module allows you to logically organise your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
To use a module: import module_name
Module test.py
1 2 3 | #Defining Module def college(): print ( "IIT" ) |
Use Module
1 2 3 | #Import import test test.college() |
Output
1 2 | #Print Statement IIT |
OOP in Python
OOP — Object Oriented Programming is a computer programming paradigm that organises software design around data, and objects, rather than functions and logic. OOP consists of objects which can contain data and code, data in the form of fields (often known as attributes or properties), and code, in the form of procedures or methods (functions).
A feature of objects is that an object’s own procedures can access and often modify the data fields of itself. Objects have a notion of self (similar to this in Java). In OOP, computer programs are designed by making them out of objects that interact with one another.[3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #file_name.py class Person: institution = 'College' # class variable def __init__( self , college, location): #constructor self .location = location #instance variables self .college = college def set_college( self , college): self .college = college def set_location( self , location): self .location = location def print ( self ): print ( "Studies in {0} at {1}" . format ( self .college, self .location)) def main(): person = Person( "IIT" , "Delhi" ) print ( "{0}:" . format (Person.institution)) person. print () if __name__ = = "__main__" : #Program starts here main() |
Note: Variables with double underscores on both sides are special variables, like __init__
To run in shell: python3 file_name.py
Output
1 2 3 | #Print Statement College: Studies in IIT at Delhi |
Footnotes
[1,2] Python – Tutorials Point