Python
What is Python?  High- level language.  Object oriented  Interpreted  Server scripting language.
Designed by Guido Van Rossum in 1989. Developer
Why Python?  Designed to be easy to learn and master. - Clean and clear syntax - Very few keywords  Highly portable. -Runs almost everywhere  Extensible.  Reduced development Time -code is 2-10x shorter than C,C++ and Java.
Comparison
Who uses Python?  Google  Yahoo  NASA  PBS  ….the list goes on
Applications  Software Development  Web developments and its applications  Mobile applications  Embedded devices  Networking programming
Python Interpreter $gedit file.py (mainly used in linux) $ python file.py or $python
Variables Syntax: variable_name = value >>> answer = 42 >>> PI = 3.14 >>> team = ‘B25’ >>> truthy = True >>> falsey = False >>> nil = None >>> choice = ‘a’
Data Types >>> type(1) <type ‘int’> >>> type(PI) <type ‘float’> >>> type(team) <type ‘str’> >>> type(truthy) <type ‘bool’> >>> type(falsey) <type ‘bool’> >>> type(nil) <type ‘NoneType’> Note: ’type’ keyword return the type of data it receives as argument.
Comments Single Line Comment Multiple Line Comment # I Love Python # Python uses interpreter """ It is easy to initialize a variable using python"""
Indentation IndentationError: expected an indented block Compiles def spam(a): pizza = pizza + a return pizza print spam(5) def spam(a): pizza = pizza + a return pizza print spam(5)
Arithmetic and logical operators
Addition >>> print 6+1 7 Substraction >>>print 6-2 4 Multiplication >>>print 3*2 6 Division >>>print 10/2 5
Exponentiation (**) >>> print 2 ** 10 1024 >>> print 3.0**2 9.0 Modulo(%) >>>print 3%2 1 >>>print 5%3 2
Strings >>>hi=“Hello World” >>>print hi Hello World >>>sem=“Hangman” Indexing In Strings >>> hi='hello Jarvis' >>> print hi[-1] s
String Concatenation String concatenation is a process in which two strings are merged together. ’+’ operator is used for concatenation. Example: >>>message=“Hello” >>>messageone=“Jharvard” >>>print message + messageone HelloJharvard >>>h=“python” >>> a=2 >>>print h +str(a) python2
Control Flow
>>>var = 100 >>>if var == 200: print “PLUTO" print var >>>elif var == 100: print "DONALD" print var >>>else: print "DAISY" print var OUTPUT: DONALD 100 Example:
Loops For…… for <item> in <collection>: #Syntax <body> >>>for letter in 'Python': # First Example print 'Current Letter :', letter Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n
While……… >>>def factorial(n): num = 1 while n >= 1: num = num * n n = n - 1 return num >>>fact(5) 120
Functions Function Syntax: The Python interpreter has a number of functions built into it that are available. But if the user want to define his own function he must def factorial(arg): num=1 while(arg>=1): num=arg*num arg=arg-1 return num Example: >>>factorial(5) 120
Data Structures Data Structures in python usually consist of Compound Data types.. These data types are of three categorizes: 1.Tuples 2.Lists 3.Dictionaries
Tuples A tuple is an immutable sequence of elements. Created with (). >>>a=(1,8,5,4,6) >>>print a (1,8,5,4,6) For Singleton Tuple we use : >>>a=(1,) >>>print a (1) >>>del tuplename #delete the tuple
Lists • Look a lot like tuples – Ordered sequence of values, each identified by an index – Use [1,2,3] rather than (1,2, 3) – Singletons are now just [4] rather than (4, ) • BIG DIFFERENCE – Lists are mutable!! – While tuple, int, float, str are immutable – So lists can be modified after they are created!
len(list) max(list) min(list) list.append(obj) list.count(obj) list.index(obj) list.reverse() list.remove(obj) list.insert(i,obj) list.pop([i]) list.sort() List Methods:
Example: >>>lst=[5,8,3,4,6,4] >>>print len(lst) >>>print max(lst) >>>print min(lst) >>>lst.append(10) >>>print lst >>>print lst.count(4) >>>lst.reverse() >>>print lst >>>lst.remove(6) >>>print lst >>>lst.insert(1,0) >>>print lst >>>print lst.pop(3) >>>lst.sort() >>>print lst Output: 6 8 3 [5, 8, 3, 4, 6, 4, 10] 2 [10, 4, 6, 4, 3, 8, 5] [10, 4, 4, 3, 8, 5] [10, 0, 4, 4, 3, 8, 5] 4 [0, 3, 4, 5, 8, 10]
Dictionary Dictionary is an unordered set of key:value pairs. Key are always unique to values. Use {} curly brackets to construct the dictionary,and [] square brackets to index it. d = {'key1' : 1, 'key2' : 2, 'key3' : 3} Monthly_numbers={‘Jan’:1, ’Feb’:2, ’Mar’:3, 1 :’Jan’, 2 :’Feb’,3 :’Mar’} >>>Monthly_numbers[‘Feb’] 2 Syntax:
Dictionary methods ● clear() ● copy() ● has_key() ● keys() ● pop() ● values() ● del dict_name[key_name] ● dict_nme[key_name]=value
What do you mean by Range() in python?
In python range () is a function that is used as a shortcut for generating list. It is of 3 different types: 1.range(start,stop) 2.range(stop) 3.range(start ,stop,step) Examples: >>>range(5,10) [5,6,7,8,9] #last element will not be included >>>range(5,10,2) [5,7,9] >>>range(5) [0,1,2,3,4]
Importing Modules  import <module_name>  from <module_name> import <module>  from <module_name> import * A module is a python file that (generally) has only definitions of variables, functions, and classes.
Example: >> import math >>> print math.sqrt(25) 5 >>> from math import * >>> sqrt(36) 6 >>>pi 3.145926535897931 >>>from datetime import datetime >>>now=datetime.now() >>>now.year 2015 >>>now.month 7 >>>now.day 21
Thank You!

Python-The programming Language

  • 1.
  • 2.
    What is Python? High- level language.  Object oriented  Interpreted  Server scripting language.
  • 3.
    Designed by GuidoVan Rossum in 1989. Developer
  • 4.
    Why Python?  Designedto be easy to learn and master. - Clean and clear syntax - Very few keywords  Highly portable. -Runs almost everywhere  Extensible.  Reduced development Time -code is 2-10x shorter than C,C++ and Java.
  • 5.
  • 6.
    Who uses Python?  Google Yahoo  NASA  PBS  ….the list goes on
  • 7.
    Applications  Software Development Web developments and its applications  Mobile applications  Embedded devices  Networking programming
  • 8.
    Python Interpreter $gedit file.py(mainly used in linux) $ python file.py or $python
  • 9.
    Variables Syntax: variable_name =value >>> answer = 42 >>> PI = 3.14 >>> team = ‘B25’ >>> truthy = True >>> falsey = False >>> nil = None >>> choice = ‘a’
  • 10.
    Data Types >>> type(1) <type‘int’> >>> type(PI) <type ‘float’> >>> type(team) <type ‘str’> >>> type(truthy) <type ‘bool’> >>> type(falsey) <type ‘bool’> >>> type(nil) <type ‘NoneType’> Note: ’type’ keyword return the type of data it receives as argument.
  • 11.
    Comments Single Line CommentMultiple Line Comment # I Love Python # Python uses interpreter """ It is easy to initialize a variable using python"""
  • 12.
    Indentation IndentationError: expected an indentedblock Compiles def spam(a): pizza = pizza + a return pizza print spam(5) def spam(a): pizza = pizza + a return pizza print spam(5)
  • 13.
  • 14.
    Addition >>> print 6+1 7 Substraction >>>print6-2 4 Multiplication >>>print 3*2 6 Division >>>print 10/2 5
  • 15.
    Exponentiation (**) >>> print 2** 10 1024 >>> print 3.0**2 9.0 Modulo(%) >>>print 3%2 1 >>>print 5%3 2
  • 16.
    Strings >>>hi=“Hello World” >>>print hi HelloWorld >>>sem=“Hangman” Indexing In Strings >>> hi='hello Jarvis' >>> print hi[-1] s
  • 17.
    String Concatenation String concatenationis a process in which two strings are merged together. ’+’ operator is used for concatenation. Example: >>>message=“Hello” >>>messageone=“Jharvard” >>>print message + messageone HelloJharvard >>>h=“python” >>> a=2 >>>print h +str(a) python2
  • 18.
  • 19.
    >>>var = 100 >>>ifvar == 200: print “PLUTO" print var >>>elif var == 100: print "DONALD" print var >>>else: print "DAISY" print var OUTPUT: DONALD 100 Example:
  • 20.
    Loops For…… for <item> in<collection>: #Syntax <body> >>>for letter in 'Python': # First Example print 'Current Letter :', letter Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n
  • 21.
    While……… >>>def factorial(n): num =1 while n >= 1: num = num * n n = n - 1 return num >>>fact(5) 120
  • 22.
    Functions Function Syntax: The Pythoninterpreter has a number of functions built into it that are available. But if the user want to define his own function he must def factorial(arg): num=1 while(arg>=1): num=arg*num arg=arg-1 return num Example: >>>factorial(5) 120
  • 23.
    Data Structures Data Structuresin python usually consist of Compound Data types.. These data types are of three categorizes: 1.Tuples 2.Lists 3.Dictionaries
  • 24.
    Tuples A tuple isan immutable sequence of elements. Created with (). >>>a=(1,8,5,4,6) >>>print a (1,8,5,4,6) For Singleton Tuple we use : >>>a=(1,) >>>print a (1) >>>del tuplename #delete the tuple
  • 25.
    Lists • Look alot like tuples – Ordered sequence of values, each identified by an index – Use [1,2,3] rather than (1,2, 3) – Singletons are now just [4] rather than (4, ) • BIG DIFFERENCE – Lists are mutable!! – While tuple, int, float, str are immutable – So lists can be modified after they are created!
  • 26.
  • 27.
    Example: >>>lst=[5,8,3,4,6,4] >>>print len(lst) >>>print max(lst) >>>printmin(lst) >>>lst.append(10) >>>print lst >>>print lst.count(4) >>>lst.reverse() >>>print lst >>>lst.remove(6) >>>print lst >>>lst.insert(1,0) >>>print lst >>>print lst.pop(3) >>>lst.sort() >>>print lst Output: 6 8 3 [5, 8, 3, 4, 6, 4, 10] 2 [10, 4, 6, 4, 3, 8, 5] [10, 4, 4, 3, 8, 5] [10, 0, 4, 4, 3, 8, 5] 4 [0, 3, 4, 5, 8, 10]
  • 28.
    Dictionary Dictionary is anunordered set of key:value pairs. Key are always unique to values. Use {} curly brackets to construct the dictionary,and [] square brackets to index it. d = {'key1' : 1, 'key2' : 2, 'key3' : 3} Monthly_numbers={‘Jan’:1, ’Feb’:2, ’Mar’:3, 1 :’Jan’, 2 :’Feb’,3 :’Mar’} >>>Monthly_numbers[‘Feb’] 2 Syntax:
  • 29.
    Dictionary methods ● clear() ●copy() ● has_key() ● keys() ● pop() ● values() ● del dict_name[key_name] ● dict_nme[key_name]=value
  • 30.
    What do youmean by Range() in python?
  • 31.
    In python range() is a function that is used as a shortcut for generating list. It is of 3 different types: 1.range(start,stop) 2.range(stop) 3.range(start ,stop,step) Examples: >>>range(5,10) [5,6,7,8,9] #last element will not be included >>>range(5,10,2) [5,7,9] >>>range(5) [0,1,2,3,4]
  • 32.
    Importing Modules  import<module_name>  from <module_name> import <module>  from <module_name> import * A module is a python file that (generally) has only definitions of variables, functions, and classes.
  • 33.
    Example: >> import math >>>print math.sqrt(25) 5 >>> from math import * >>> sqrt(36) 6 >>>pi 3.145926535897931 >>>from datetime import datetime >>>now=datetime.now() >>>now.year 2015 >>>now.month 7 >>>now.day 21
  • 34.