DevScript Saga

Welcome to DevScript Saga!

A little Helping Hand for Devs

Today , we will see Python Conditional Statements, Loops Python Class & objects , Regular Expressions in Python. Before we move forward, we will see some important points that one should know :

  • In Python, we don’t have any semicolons and curly braces.
  • Python uses indentation to indicate a block of code. (Indentation means spaces)
  • Python will give you an error if you skip the indentation:
  • Variables are created the moment you assign a value to it .
  • Comments start with # .
  • Python does not really have a syntax for multi line comments. To add a multi-line comment you could insert a # for each line .

1.Conditional Statements:

  • if:
  • Conditional statements are handled by if statements in Python.
  • In Python, If Statement is used for decision making. It will run the body of code only when IF statement is true.
  • Syntax:
if (expression):
 Statement1

  • else:
  • The “else condition” is usually used when you have to judge one statement on the basis of other. If one condition goes wrong, then there should be another condition that should justify the statement or logic.
  • Syntax:
if (expression):
   statement1
else:
   Statement2

elif:

To correct the previous error made by “else condition”, we can use “elif” statement. By using “elif” condition, you are telling the program to print out the third condition or possibility when the other condition goes wrong or incorrect.

if (expression) :
    statement1
elif (expression):
    statement 2
else:
    statement3

2.Loops

Like other languages, Python too have for loop, while loop, continue, break statements. They have the same meaning in other languages. Here point to remember is only syntax.

for Syntax:

for variable_name in range( lower_limit, upper_limit):     
statement

while Syntax:

while (condition) :
    Statement

3.Python OOPs:

“Class” is a logical grouping of functions and data. Python class provides all the standard features of Object Oriented Programming.

How to define Python classes?

Step 1) In Python, classes are defined by the “Class” keyword

	class myClass():

Step 2) Inside classes, you can define functions or methods that are part of this class

Step 3) Everything in a class is indented, just like the code in the function, loop, if statement, etc. Anything not indented is not in the class.

NOTE: About using “self” in Python

  • The self-argument refers to the object itself. Hence the use of the word self. So inside this method, self will refer to the specific instance of this object that’s being operated on.
  • Self is the name preferred by convention by Pythons to indicate the first parameter of instance methods in Python. It is part of the Python syntax to access members of objects

Step 4) To make an object of the class

	c = myClass()

Step 5) To call a method in a class

    c.method1()
    c.method2(" Testing is fun")
  • Notice that when we call the method1 or method2, we don’t have to supply the self-keyword. That’s automatically handled for us by the Python runtime.
  • Python runtime will pass “self” value when you call an instance method on in instance, whether you provide it deliberately or not
  • You just have to care about the non-self arguments

How Inheritance works??

Inheritance is a feature used in object-oriented programming; it refers to defining a new class with less or no modification to an existing class. The new class is called derived class and from one which it inherits is called the base. Python supports inheritance; it also supports multiple inheritances. A class can inherit attributes and behavior methods from another class called subclass or heir class.

Python Constructors

A constructor is a class function that instantiates an object to predefined values. It begins with a double underscore (_). It __init__() method

4. Python Regular Expressions :

A regular expression in a programming language is a special text string used for describing a search pattern. It is extremely useful for extracting information from text such as code, files, log, spreadsheets or even documents.

While using the regular expression the first thing is to recognize is that everything is essentially a character, and we are writing patterns to match a specific sequence of characters also referred as string.

In Python, a regular expression is denoted as RE are imported through re module. Python supports regular expression through libraries. In Python regular expression supports various things like Modifiers, Identifiers, and White space characters.

  • “^” : This expression matches the start of a string
  • “w+” : This expression matches the alphanumeric character in the string.
  • “s”: This expression is used for creating a space in the string .

The “re” package provides several methods to actually perform queries on an input string .

  1.  re.match() – The match function is used to match the RE pattern to string with optional flags.
  2. re.search()– A regular expression is commonly used to search for a pattern in a text. This method takes a regular expression pattern and a string and searches for that pattern with the string.
  3. re.findall() – re.findall() module is used when you want to iterate over the lines of the file, it will return a list of all the matches in a single step. 

Python Flags

Many Python Regex Methods and Regex functions take an optional argument called Flags. This flags can modify the meaning of the given Regex pattern.

Various flags used in Python includes

Syntax for Regex FlagsWhat does this flag do
[re.M]Make begin/end consider each line
[re.I]It ignores case
[re.S]Make [ . ]
[re.U]Make { \w,\W,\b,\B} follows Unicode rules
[re.L]Make {\w,\W,\b,\B} follow locale
[re.X]Allow comment in Regex
Day 2 Assignment:
1.Write a Python program to accept a filename from the user and print the extension of that.
2.Program to find factorial of a number.
3.Program to print ASCII Value of a character
4.Program for n-th Fibonacci number
5.Program to check if given number is prime or not
6.Program for Sum of squares of first n natural numbers
7. Program to concatenate all elements in a list into a string and return it.
8.Program to check Armstrong Number.
Click here for Solution...

Leave a comment