Professional Documents
Culture Documents
Rogers-Automation Platforms: (PROJECT ID#30763) Knowledge Transfer Session#1
Rogers-Automation Platforms: (PROJECT ID#30763) Knowledge Transfer Session#1
PLATFORMS
(PROJECT ID#30763)
Topics
• Introduction to Python
• Presenter : Yajna and Abhishek
• Introduction to DevOps
• Presenter : Abhishek
• LUNCH BREAK
• Introduction to Jenkins
• Presenter : Abhishek
• Introduction to Gitlab
• Presenter : Yajna
• History
• Characteristics of Python
• Python Syntax
• Functions
• Main function
• Regular Expressions
• OOPs
© 2019 Juniper Networks 3
Juniper Business
Juniper Use Only
Internal
WHY LEARN A PROGRAMMING LANGUAGE
ABOUT PYTHON
CHARACTERISTICS
• It can be used as a scripting language or can be compiled to byte-code for building large applications.
• It provides very high-level dynamic data types and supports dynamic type checking.
• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Program execution
APPLICATIONS OF PYTHON
• Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax
• Easy-to-read − Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain − Python's source code is fairly easy-to-maintain.
• A broad standard library − Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and
Macintosh.
• Interactive Mode − Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
• Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms.
• Extendable − You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize
their tools to be more efficient.
• Databases − Python provides interfaces to all major commercial databases.
• GUI Programming − Python supports GUI applications that can be created and ported to many system calls, libraries and windows
systems, such as Windows MFC, Macintosh, and the X Window system of Unix.
• Scalable − Python provides a better structure and support for large programs than shell scripting.
SYNTAX
The Python interpreter has a number of User-defined functions are written by Anonymous functions are also called
functions and types built into it that are the developers to meet certain lambda functions in Python because
always available requirements. instead of declaring them with the
• sum() Function definition standard def keyword,
the lambda keyword is used.
• min() Function call
• max() Function arguments
Example: double = lambda x: x * 2
• set() Global vs Local Variables
print(double(5))
• sorted()
PYTHON RECURSION
EXAMPLE
PYTHON MAIN AND NAME VARIABLE TWO TYPES OF USAGE FOR MAIN
• A module allows you to logically • You can use any Python source file as a • Python's from statement lets you
organize your Python code. module by executing an import statement import specific attributes from a
in some other Python source file. module into the current namespace.
The import has the following syntax The from...import has the following
• Grouping related code into a module syntax −
makes the code easier to understand
and use. – import module1[, module2[,... module]
– from modname import name1[,
• When the interpreter encounters an name2[, ... nameN]]
• A module is a Python object with
import statement, it imports the module
arbitrarily named attributes that you
if the module is present in the search path • This statement does not import the
can bind and reference.
entire module fib into the current
• A module is loaded only once, regardless namespace; it just introduces the item
• Simply, a module is a file consisting of fibonacci from the module fib into the
of the number of times it is imported. This
Python code. A module can define global symbol table of the importing
prevents the module execution from
functions, classes and variables. A module.
happening over and over again if multiple
module can also include runnable
imports occur.
code.
• It is also possible to import all names • When you import a module, the • The module search path is stored in
from a module into the current Python interpreter searches for the the system module sys as
namespace by using the following module in the following sequences the sys.path variable. The sys.path
import statement − variable contains the current directory,
PYTHONPATH, and the installation-
– The current directory.
dependent default.
– from modname import *
– If the module isn't found, Python
• This provides an easy way to import then searches each directory in the • The PYTHONPATH is an environment
all the items from a module into the shell variable PYTHONPATH. variable, consisting of a list of
current namespace; however, this directories. The syntax of
statement should be used sparingly. PYTHONPATH is the same as that of
– If all else fails, Python checks the
the shell variable PATH.
default path. On UNIX, this default
path is normally
/usr/local/lib/python/.
• A Python identifier is a name used to identify a • Class names start with an uppercase letter. All
variable, function, class, module or other object. An other identifiers start with a lowercase letter.
identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters,
underscores and digits (0 to 9) • Starting an identifier with a single leading
underscore indicates that the identifier is private.
• Python accepts single ('), double (") and triple (''' or """)
quotes to denote string literals, as long as the same type of
quote starts and ends the string.
• The triple quotes are used to span the string across multiple
lines
RE MODULE
• A regular expression is a special sequence of characters that helps you match or find other strings or sets of
strings, using a specialized syntax held in a pattern
• Python re module
• match checks for a match only at the beginning of the string, while search checks for a match anywhere in
the string
• The re.match and re.search function return a match object on success, None on failure. We
usegroup(num) or groups() function of match object to get matched expression.
#!/usr/bin/python
import re
line = "Cats are smarter than dogs"
matchObj = re.match( r'(.*) are (.*?) .*', line, re.I)
• Python provides two very important • assert Expression[, Arguments] • If you have some suspicious code that
features to handle any unexpected may raise an exception, you can
error in your Python programs and to defend your program by placing the
add debugging capabilities in them − suspicious code in a
When it encounters an assert statement,
Python evaluates the accompanying – try: block
– Assertions
expression, which is hopefully true. If the
– Exception handling – except: statement,
expression is false, Python raises
an AssertionError exception.
followed by a block of code which
handles the problem as elegantly as
possible.
• An exception is an event, which occurs
during the execution of a program that
disrupts the normal flow of the
program's instructions..
• procedure-oriented : blocks of • Classes and objects are the two main • variables that store integers are
statements which manipulate data aspects of object oriented variables which are instances (objects)
programming. of the int class
• Another way of organizing your
program which is to combine data and • A class creates a • Variables that belong to an object or
functionality and wrap it inside new type where objects are instances class are referred to as fields. Objects
something called an object. of the class. can also have functionality by using
functions that belong to a class. Such
functions are called methods of the
• This is called the object class.
oriented programming paradigm.
• Class methods have only one specific class Person: • The __init__ method is run as soon as
difference from ordinary functions an object of a class is instantiated
def __init__(self, name):
self.name = name • The method is useful to do
– they must have an extra first name any initialization (
that has to be added to the def say_hi(self):
print('Hello, my name is', self.name) • Notice the double underscores both
beginning of the parameter list, at the beginning and at the end of the
– but you do not give a value for this p = Person(‘Rogers’) name.
parameter when you call the p.say_hi()
• One of the major benefits of object
method, # The previous 2 lines can also be written as #
oriented programming is reuse of
Person('Swaroop').say_hi()
– Python will provide it. code and one of the ways this is
$ python oop_init.py
achieved is through
Hello, my name is Rogers
the inheritance mechanism.
Inheritance can be best imagined as
implementing a type and
subtype relationship between classes.
Thank You