Python Basics
What is Python & What can Python do
2
Applications for Python
Web and Internet Development
Frameworks such as Django and Pyramid.
Micro-frameworks such as Flask and Bottle.
• Python's HTM and XML
• JSON
• E-mail processing
Scientific and Numeric
• Python is widely used in scientific and numeric computing:
• SciPy is a collection of packages for mathematics, science, and engineering.
• Pandas is a data analysis and modeling library.
3
Applications for Python
Education
• Python is a superb language for teaching programming, both at the introductory level and in more advanced
courses.
• Books such as How to Think Like a Computer Scientist, Python Programming: An Introduction to Computer
Science, and Practical Programming.
• The Education Special Interest Group is a good place to discuss teaching issues.
Desktop GUIs
• The Tk GUI library is included with most binary distributions of Python.
• Some toolkits that are usable on several platforms are available separately:
• wxWidgets
• Kivy, for writing multitouch applications.
• Qt via pyqt or pyside
4
Applications for Python
Software Development
• Python is often used as a support language for software developers, for build control and management, testing,
and in many other ways.
• SCons for build control.
• Buildbot and Apache Gump for automated continuous compilation and testing.
• Roundup or Trac for bug tracking and project management.
Business Applications
• Python is also used to build ERP and e-commerce systems:
• Odoo is an all-in-one management software that offers a range of business applications that form a complete suite of
enterprise management applications.
• Tryton is a three-tier high-level general purpose application platform.
5
Python Data Types
6
Variables, Syntax & Comments
7
Python Syntax
8
Comments
9
• Python Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
10
Python Type casting
11
Python - Variable Names
12
Python Variables - Assign Multiple Values
13
Python - Output Variables
14
Python - Local & Global Variables
15
• Python Global variables are those which are not defined inside any function and have a global
scope whereas Python local variables are those which are defined inside a function and their scope
is limited to that function only. In other words, we can say that local variables are accessible only
inside the function in which it was initialized whereas the global variables are accessible throughout
the program and inside every function.
Print statement
• Print statement is used generally to provide output of statement.
• Example :: print("Hello, World")
16
Numeric
Python has three built-in numeric data types: integers, floating-point
numbers, and complex numbers.
Integers are whole numbers ex:- 1 , 2 ,45 etc
Floating point numbers also known as Float are numbers with
decimal numbers ex:-1.0, 1.2, 2.5 etc
Complex Number are numbers which are a sum of real and complex
Numers represented as X+Yi ex:- z= 5+3j. Python converts the real
numbers x and y into complex using the function complex(x,y)
Python Numeric interconversion
18
Python Strings
• String in Python are text in simple words
• Strings are denoted by “ ” or ‘ ’ and ‘’’ ’’’ covering the statement
• Example:: “ ” & ‘ ’ is used in same purpose if the text is simple but they are used to nest each other.
• ‘’My name is 'John' and i am '24' years old”
• ‘My name is 'John' and i am “24” years old’
• Statement separated by comma “”” “”” is used.
19
Strings are Arrays
• Like many other popular programming languages, strings in Python are arrays of bytes representing unicode
characters.
• However, Python does not have a character data type, a single character is simply a string with a length of 1.
• Square brackets can be used to access elements of the string.
20
21
Python - Slicing Strings
• Slicing
• You can return a range of characters by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to return a part of the string.
• Positive and Negative slicing with reference to index
22
Simple program to find the sum of two
numbers
23
Modify Strings
Python has a set of built-in methods that you can use on strings.
Upper Case Remove Extra space on extreme right or left
Lower Case Replacing Strings
Capitalize
Split Strings
24
String Methods
25
String Methods
26
String Methods
27
Python - String Concatenation
28
Python - Format - Strings
29
Use the format() method to insert numbers into strings:
Python - Escape Characters
30
Escape Character
To insert characters that are illegal in a string, use an escape character.
An escape character is a backslash  followed by the character you want to insert.
An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
31
Python Operators
32
Operators are used to perform operations on variables and values.
In the example below, we use the + - * / // % operator to add, subtract, multiply, division,
floor division, remainder & power of two values.
Examples shown below:
Python Assignment Operators
33
Python Booleans
34
35
36
Python Lists
• mylist = ["apple", "banana", "cherry"]
• Lists are used to store multiple items in a single variable.
• Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set,
and Dictionary, all with different qualities and usage. Lists are created using square brackets:
37
List Items
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
38
39
Python - Change List Items
40
41
• Append Items
• To add an item to the end of the list, use the append() method:
• Insert Items
42
Python - Add List Items
To insert a list item at a specified index, use the insert() method.
43
• Extend List
• Add Any Iterable
• The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries
etc.).
44
45
Python - Remove List Items
• Remove Specified Item
• The remove() method removes the specified item.
• Remove Specified Index
• The pop() method removes the specified index.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Python Sets
• A set is a collection of unique data. That is, elements of a set
cannot be duplicate. For example
• Suppose we want to store information about student IDs.
Since student IDs cannot be duplicate, we can use a set.
63
Create a Set in Python
64
In Python, we create sets by placing all the elements inside curly braces {}, separated by comma.
A set can have any number of items and they may be of different types (integer, float, tuple, string etc.).
But a set cannot have mutable elements like lists, sets or dictionaries as its elements.
Let's see an example,
65
66
67
68
69
70
71
72
73
74
Python Dictionaries
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
Dictionaries are written with curly brackets, and have keys and values:
thisdict = {"brand":"Ford","model":"Mustang","year":1964}
print(thisdict)
Output : {"brand":"Ford","model":"Mustang","year":1964}
Print the "brand" value of the dictionary:
print(thisdict["brand"])
Output:Ford
Ordered or Unordered?
When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.
Unordered means that the items does not have a defined order, you cannot refer to an item by using an index.
Changeable
Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created.
Duplicates Not Allowed
Duplicate values will overwrite existing values:
thisdict = {"brand":"Ford","model":"Mustang","year":1964,"year":1968}
print(thisdict)
Output: {"brand":"Ford","model":"Mustang","year":1968}
75
76
Python - Access Dictionary Items
• Accessing Items
• You can access the items of a dictionary by referring to its key name, inside square brackets:
• Get the value of the "model" key:
• thisdict = {"brand":"Ford","model":"Mustang","year":1964}
• x = thisdict["model"]
• print(x)
• Output: Mustang
• There is also a method called get() that will give you the same result:
• Get the value of the "model" key:
• x = thisdict.get("model")
• print(x)
• Output: Mustang
77
78
79
80
81
82
83
84
Function define in Python
85
86
87
88
89
90
91
92
93
94
95
Simple calculator using Python
96
To Find the factorial of a number
97
Program to check whether a number is a
prime number
98

Python-Basics.pptx

  • 1.
  • 2.
    What is Python& What can Python do 2
  • 3.
    Applications for Python Weband Internet Development Frameworks such as Django and Pyramid. Micro-frameworks such as Flask and Bottle. • Python's HTM and XML • JSON • E-mail processing Scientific and Numeric • Python is widely used in scientific and numeric computing: • SciPy is a collection of packages for mathematics, science, and engineering. • Pandas is a data analysis and modeling library. 3
  • 4.
    Applications for Python Education •Python is a superb language for teaching programming, both at the introductory level and in more advanced courses. • Books such as How to Think Like a Computer Scientist, Python Programming: An Introduction to Computer Science, and Practical Programming. • The Education Special Interest Group is a good place to discuss teaching issues. Desktop GUIs • The Tk GUI library is included with most binary distributions of Python. • Some toolkits that are usable on several platforms are available separately: • wxWidgets • Kivy, for writing multitouch applications. • Qt via pyqt or pyside 4
  • 5.
    Applications for Python SoftwareDevelopment • Python is often used as a support language for software developers, for build control and management, testing, and in many other ways. • SCons for build control. • Buildbot and Apache Gump for automated continuous compilation and testing. • Roundup or Trac for bug tracking and project management. Business Applications • Python is also used to build ERP and e-commerce systems: • Odoo is an all-in-one management software that offers a range of business applications that form a complete suite of enterprise management applications. • Tryton is a three-tier high-level general purpose application platform. 5
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    • Python Variables Variablesare containers for storing data values. Creating Variables Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. 10
  • 11.
  • 12.
  • 13.
    Python Variables -Assign Multiple Values 13
  • 14.
    Python - OutputVariables 14
  • 15.
    Python - Local& Global Variables 15 • Python Global variables are those which are not defined inside any function and have a global scope whereas Python local variables are those which are defined inside a function and their scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function.
  • 16.
    Print statement • Printstatement is used generally to provide output of statement. • Example :: print("Hello, World") 16
  • 17.
    Numeric Python has threebuilt-in numeric data types: integers, floating-point numbers, and complex numbers. Integers are whole numbers ex:- 1 , 2 ,45 etc Floating point numbers also known as Float are numbers with decimal numbers ex:-1.0, 1.2, 2.5 etc Complex Number are numbers which are a sum of real and complex Numers represented as X+Yi ex:- z= 5+3j. Python converts the real numbers x and y into complex using the function complex(x,y)
  • 18.
  • 19.
    Python Strings • Stringin Python are text in simple words • Strings are denoted by “ ” or ‘ ’ and ‘’’ ’’’ covering the statement • Example:: “ ” & ‘ ’ is used in same purpose if the text is simple but they are used to nest each other. • ‘’My name is 'John' and i am '24' years old” • ‘My name is 'John' and i am “24” years old’ • Statement separated by comma “”” “”” is used. 19
  • 20.
    Strings are Arrays •Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. • However, Python does not have a character data type, a single character is simply a string with a length of 1. • Square brackets can be used to access elements of the string. 20
  • 21.
  • 22.
    Python - SlicingStrings • Slicing • You can return a range of characters by using the slice syntax. • Specify the start index and the end index, separated by a colon, to return a part of the string. • Positive and Negative slicing with reference to index 22
  • 23.
    Simple program tofind the sum of two numbers 23
  • 24.
    Modify Strings Python hasa set of built-in methods that you can use on strings. Upper Case Remove Extra space on extreme right or left Lower Case Replacing Strings Capitalize Split Strings 24
  • 25.
  • 26.
  • 27.
  • 28.
    Python - StringConcatenation 28
  • 29.
    Python - Format- Strings 29 Use the format() method to insert numbers into strings:
  • 30.
    Python - EscapeCharacters 30 Escape Character To insert characters that are illegal in a string, use an escape character. An escape character is a backslash followed by the character you want to insert. An example of an illegal character is a double quote inside a string that is surrounded by double quotes:
  • 31.
  • 32.
    Python Operators 32 Operators areused to perform operations on variables and values. In the example below, we use the + - * / // % operator to add, subtract, multiply, division, floor division, remainder & power of two values. Examples shown below:
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
    Python Lists • mylist= ["apple", "banana", "cherry"] • Lists are used to store multiple items in a single variable. • Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Lists are created using square brackets: 37 List Items List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index [0], the second item has index [1] etc.
  • 38.
  • 39.
  • 40.
    Python - ChangeList Items 40
  • 41.
  • 42.
    • Append Items •To add an item to the end of the list, use the append() method: • Insert Items 42 Python - Add List Items To insert a list item at a specified index, use the insert() method.
  • 43.
  • 44.
    • Extend List •Add Any Iterable • The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.). 44
  • 45.
  • 46.
    Python - RemoveList Items • Remove Specified Item • The remove() method removes the specified item. • Remove Specified Index • The pop() method removes the specified index. 46
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
    Python Sets • Aset is a collection of unique data. That is, elements of a set cannot be duplicate. For example • Suppose we want to store information about student IDs. Since student IDs cannot be duplicate, we can use a set. 63
  • 64.
    Create a Setin Python 64 In Python, we create sets by placing all the elements inside curly braces {}, separated by comma. A set can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have mutable elements like lists, sets or dictionaries as its elements. Let's see an example,
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
    Python Dictionaries Dictionaries areused to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. Dictionaries are written with curly brackets, and have keys and values: thisdict = {"brand":"Ford","model":"Mustang","year":1964} print(thisdict) Output : {"brand":"Ford","model":"Mustang","year":1964} Print the "brand" value of the dictionary: print(thisdict["brand"]) Output:Ford Ordered or Unordered? When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change. Unordered means that the items does not have a defined order, you cannot refer to an item by using an index. Changeable Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created. Duplicates Not Allowed Duplicate values will overwrite existing values: thisdict = {"brand":"Ford","model":"Mustang","year":1964,"year":1968} print(thisdict) Output: {"brand":"Ford","model":"Mustang","year":1968} 75
  • 76.
  • 77.
    Python - AccessDictionary Items • Accessing Items • You can access the items of a dictionary by referring to its key name, inside square brackets: • Get the value of the "model" key: • thisdict = {"brand":"Ford","model":"Mustang","year":1964} • x = thisdict["model"] • print(x) • Output: Mustang • There is also a method called get() that will give you the same result: • Get the value of the "model" key: • x = thisdict.get("model") • print(x) • Output: Mustang 77
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
    To Find thefactorial of a number 97
  • 98.
    Program to checkwhether a number is a prime number 98