PYTHON Varun Jain & Senior Software Engineer

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Chapter 2 Writing Simple Programs
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Python Introduction.
Python quick start guide
Intermediate PHP (2) File Input/Output & User Defined Functions.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Python Let’s get started!.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Chapter 2 Writing Simple Programs
Web Database Programming Using PHP
Fundamentals of Programming I Overview of Programming
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Topics Introduction to Functions Defining and Calling a Void Function
Python Variable Types.
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Chapter 1: Introduction to computers and C++ Programming
Introduction to Python
Key Ideas from day 1 slides
Computer Programming Fundamentals
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Web Database Programming Using PHP
Topics Introduction to Repetition Structures
loops for loops iterate over a given sequence.
Objectives Identify the built-in data types in C++
Variables, Expressions, and IO
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Functions CIS 40 – Introduction to Programming in Python
Scripts & Functions Scripts and functions are contained in .m-files
SVTRAININGS. SVTRAININGS Python Overview  Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed.
CS190/295 Programming in Python for Life Sciences: Lecture 1
Arrays, For loop While loop Do while loop
Introduction to Python
Python Primer 2: Functions and Control Flow
Introduction to Python
An Introduction to Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Web DB Programming: PHP
Python Primer 1: Types and Operators
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Intro to PHP.
G. Pullaiah College of Engineering and Technology
COMPUTER PROGRAMMING SKILLS
PHP an introduction.
General Computer Science for Engineers CISC 106 Lecture 03
Class code for pythonroom.com cchsp2cs
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Functions John R. Woodward.
Introduction to Python
Programming for Business Computing Introduction
Presentation transcript:

PYTHON Varun Jain & Senior Software Engineer Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT CenturyLink Technologies India PVT LTD

About Python Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP. Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs. Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects. Python is a Beginner's Language: Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.

Python Features Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly. 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: PythEasy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.

Python Features 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. on has support for an interactive mode which allows interactive testing and debugging of snippets of code. 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.

Python – Installation Python distribution is available for a wide variety of platforms. You need to download only the binary code applicable for your platform and install Python. Windows Installation Open a Web browser and go to https://www.python.org/downloads/. Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install. To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI. Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done. Setting path at Windows To add the Python directory to the path for a particular session in Windows − At the command prompt − type path %path%;C:\Python and press Enter. Note − C:\Python is the path of the Python directory

Python – Basic Syntax Let us write a simple Python program in a script. Python files have extension .py. Type the following source code in a test.py file: Print “Hello python!” This produces the following result: Hello, Python! Quotation in Python Python accepts single ('), double (") quotes to denote string literals, as long as the same type of quote starts and ends the string. Like. word = 'word' sentence = “This is a sentence.”

Python - Variable Types #!/usr/bin/python counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string print counter print miles print name This produces the following result: 100, 1000.0 and John Multiple Assignment a,b,c = 1,2.0,"john“ print a print b print c This produces the following result: 1, 2.0 and john

Python – String Strings in Python are identified as a contiguous set of characters represented in the quotation marks #!/usr/bin/python str = 'Hello World!' print str # Prints complete string print str[0] # Prints first character of the string print str[2:5] # Prints characters starting from 3rd to 5th print str[2:] # Prints string starting from 3rd character print str * 2 # Prints string two times print str + "TEST" # Prints concatenated string This will produce the following result − Hello World! H llo llo World! Hello World!TEST

Python – List Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type. #!/usr/bin/python list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print list # Prints complete list print list[0] # Prints first element of the list print list[1:3] # Prints elements starting from 2nd till 3rd print list[2:] # Prints elements starting from 3rd element print tinylist * 2 # Prints list two times print list + tinylist # Prints concatenated lists This produce the following result − ['abcd', 786, 2.23, 'john', 70.200000000000003] abcd [786, 2.23] [2.23, 'john', 70.200000000000003] [123, 'john', 123, 'john'] ['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']

Python - Dictionaries A dictionary is a data type similar to arrays, but works with keys and values instead of indexes. Each value stored in a dictionary can be accessed using a key, which is any type of object (a string, a number, a list, etc.) instead of using its index to address it. Example phonebook = {} phonebook["John"] = 938477566 phonebook["Jack"] = 938377264 phonebook["Jill"] = 947662781 print(phonebook) Received Input : {'Jill': 947662781, 'Jack': 938377264, 'John': 938477566}

Python - Dictionaries Iterating over dictionaries Dictionaries can be iterated over, just like a list. However, a dictionary, unlike a list, does not keep the order of the values stored in it. To iterate over key value pairs, use the following syntax: Example phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781} for name, number in phonebook.items(): print("Phone number of %s is %d" % (name, number)) Received Input : Phone number of Jill is 947662781 Phone number of Jack is 938377264 Phone number of John is 938477566 Removing a value Example phonebook = { "John" : 938477566, "Jack" : 938377264, "Jill" : 947662781 } del phonebook["John"] print(phonebook) Received Input : {'Jill': 947662781, 'Jack': 938377264}

Python - Loops A loop statement allows us to execute a statement or group of statements multiple times. For Loop - Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. For Loop Example # Prints out the numbers 0,1,2,3,4 for x in range(5): print(x) #Prints out 3,4,5 for x in range(3, 6): Received Input: 0 1 2 3 4 3 4 5

Python - Loops While Loop - Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. While Loop Example # Prints out 0,1,2,3,4 count = 0 while count < 5: print(count) count += 1 # This is the same as count = count + 1 Received Input: 0 1 2 3 4

Python – if Condition If Condition - Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. If Condition Example phonebook = { "John" : 938477566, "Jack" : 938377264, "Jill" : 947662781 } phonebook["Jake"] = 938273443 del phonebook["Jill"] # testing code if "Jake" in phonebook: print("Jake is listed in the phonebook.") if "Jill" not in phonebook: print("Jill is not listed in the phonebook.") Received Input: Jake is listed in the phonebook. Jill is not listed in the phonebook.

Python – Functions A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Defining a Function Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. The first statement of a function can be an optional statement - the documentation string of the function or docstring. The code block within every function starts with a colon (:) and is indented. The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Python – Functions #!/usr/bin/python def printme( str ): Example #!/usr/bin/python def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function") it produces the following result − I'm first call to user defined function! Again second call to the same function

Python – Functions Default arguments - A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. #!/usr/bin/python # Function definition is here def printinfo( name, age = 35 ): "This prints a passed info into this function" print "Name: ", name print "Age: ", age return; # Now you can call printinfo function printinfo( age=50, name=“Miki" ) printinfo( name=“Miki" ) it produces the following result − Name: miki Age: 50 Name: Miki Age: 35

Python – Functions Pass by reference vs value - All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function #!/usr/bin/python # Function definition is here def changeme( mylist ): "This changes a passed list into this function" mylist.append([1,2,3,4]); print "Values inside the function: ", mylist return # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist this would produce the following result − Values inside the function: [10, 20, 30, [1, 2, 3, 4]] Values outside the function: [10, 20, 30, [1, 2, 3, 4]]

Python – Functions Keyword arguments - Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. #!/usr/bin/python # Function definition is here def printinfo( name, age ): "This prints a passed info into this function" print "Name: ", name print "Age: ", age return; # Now you can call printinfo function printinfo( age=50, name=“Miki" ) # Note that the order of parameters does not matter. it produces the following result − Name: Miki Age: 50

Python – Functions The Anonymous Functions - These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions. #!/usr/bin/python # Function definition is here sum = lambda arg1, arg2: arg1 + arg2; # Now you can call sum as a function print "Value of total : ", sum( 10, 20 ) print "Value of total : ", sum( 20, 20 ) it produces the following result − Value of total : 30 Value of total : 40

Python - Files I/O Reading Keyboard Input – Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are − raw_input Input The raw_input Function - The ‘raw_input’ function reads one line from standard input and returns it as a string. #!/usr/bin/python str = raw_input("Enter your input: "); print "Received input is : ", str This prompts you to enter any string Enter your input: Hello Python Received input is : Hello Python

Python - Files I/O The input Function - The input function is equivalent to raw_input, except that it assumes the input is a valid Python expression and returns the evaluated result to you. #!/usr/bin/python i = input("Enter your input: "); print "Received input is : ", i # i = 10 j = input("Enter your input: "); print "Received input is : ", j # j = 20 t=i+j print “Sum of t = i+j : ", t This would produce the following result against the entered input − Sum of t = i+j : 30

Python – Classes / Object Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Class variable: A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are. Method : A special kind of function that is defined in a class definition Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class. Object: A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.

Python – Classes / Object Example Accessing Object Variables class MyClass: variable = “var" def function(self): print("This is a message inside the class.") myobjectx = MyClass() myobjecty = MyClass() myobjecty.variable = “jain" # Then print out both values print(myobjectx.variable) print(myobjecty.variable) Received input : var jain

Python – Classes / Object Example Accessing Object Functions class MyClass: variable = "blah" def function(self): print("This is a message inside the class.") myobjectx = MyClass() myobjectx.function() Received input : This is a message inside the class.

Thank You!!!