KEMBAR78
Programming in python Unit-1 Part-1 | PDF
Programming in Python
Vikram Neerugatti
content
• Introduction to python
• Installation of Python in windows
• Running Python
• Arithmetic operators
• Values and types
• Formal and Natural languages
Introduction to Python
• Dynamic interoperated language that allows both functional and object oriented programming languages
• Interpreter
• Python is a language
• Simple
• On all platforms
• Real programming language
• High level problems
• Can split into modules
• GUI can be done
• It is a interpreter language
• Very short compare with other languages
• Indents will be used instead of brackets
• After the bbc shows monty python cant do anything on reptiles
Installation of Python in windows
Installation of Python in windows
Installation of Python in windows
Running a python
• Start
• Select python
• In the prompt type the statement
Print (“hello world”)
Arithmetic operations
• In the prompt of the python type
• 3+4
• 3-4
• ¾
• 3%4
• 3*4
• Type with variables also
• A=3
• B=5, etc.
Values and types
• Python numbers
• Integer
• 2,4,5
• Float
• 2.3,4.5,7.8
• Complex
• 3+5J
• Python lists
• can be any
• [3, 4, 5, 6, 7, 9]
• ['m', 4, 4, 'nani']
• Slicing operator, list index starts from zero
• A[4],a[:2],a[1:2]
• mutable
• Python tuples ()
• Cannot change, slicing can be done a[4].
• immutable
Values and types
• Python Strings
• Sequence of Characters
• Single line
• Ex. s='zaaaa nani'
• Multi line
• Ex. s='''zaaaa nani
• print(s)
• nani'''
• print (s)
• Sliicing can be done
• Ex. S[5]
Values and types
• Python sets
• Collection of unordered elements, elements will not be in order
• S={3,4,5.6,’g’,8}
• Can do intersection (&), union(|), difference (-), ^ etc.
• Ex. A&b, a|b, a-b, a^b’
• Remove duplicates
• {5,5,5,7,88,8,88}
• No slicing can be done, because elements are not in order.
• Python dictionaries
Python dictionaries
• Python dictionary is an unordered collection of items.
• While other compound data types have only value as an element, a
dictionary has a key: value pair.
• Dictionaries are optimized to retrieve values when the key is known.
• Creating a dictionary is as simple as placing items inside curly braces {}
separated by comma.
• An item has a key and the corresponding value expressed as a pair, key:
value.
• While values can be of any data type and can repeat, keys must be of
immutable type (string, number or tuple with immutable elements) and
must be unique.
Python dictionaries (creating)
1.# empty dictionary
2.my_dict = {}
4.# dictionary with integer keys
5.my_dict = {1: 'apple', 2: 'ball'}
7.# dictionary with mixed keys
8.my_dict = {'name': 'John', 1: [2, 4, 3]}
10.# using dict()
11.my_dict = dict({1:'apple', 2:'ball'})
13.# from sequence having each item as a pair
14.my_dict = dict([(1,'apple'), (2,'ball')])
Python dictionaries (accesing)
• my_dict = {'name':'Jack', 'age': 26}
• # Output: Jack
• print(my_dict['name’])
• # Output: 26
• print(my_dict.get('age’))
• # Trying to access keys which doesn't exist throws error#
my_dict.get('address’)
• # my_dict['address’]
• Key in square brackets/get(), if you use get(), instead of error , none will be
displayed.
Change/ add elements in a dictionaries
• my_dict = {'name':'Jack', 'age': 26}
• # update value
• my_dict['age'] = 27
• #Output: {'age': 27, 'name': 'Jack’}
• print(my_dict)
• # add item
• my_dict['address'] = 'Downtown’
• # Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}print(my_dict)
• Mutable, if key is there: value will be updated, if not created new one.
How to remove or delete the item in
dictionaries
• We can remove a particular item in a dictionary by using the method
pop(). This method removes as item with the provided key and
returns the value.
• The method, popitem() can be used to remove and return an
arbitrary item (key, value) form the dictionary. All the items can be
removed at once using the clear() method.
• We can also use the del keyword to remove individual items or the
entire dictionary itself.
How to remove or delete the item in
dictionaries
• # create a dictionary squares = {1:1, 2:4, 3:9, 4:16, 5:25}
• # remove a particular item # Output: 16
• print(squares.pop(4)) # Output: {1: 1, 2: 4, 3: 9, 5: 25}print(squares)
• # remove an arbitrary item
• # Output: (1, 1) print(squares.popitem()) # Output: {2: 4, 3: 9, 5: 25} print(squares)
• # delete a particular item
• del squares[5] # Output: {2: 4, 3: 9}print(squares)
• # remove all items
• squares.clear()
• # Output: {}
• print(squares)
• # delete the dictionary itself
• del squares
• # Throws Error# print(squares)
Nested Dictionary
• In Python, a nested dictionary is a dictionary inside a dictionary.
• It's a collection of dictionaries into one single dictionary.
Example:
nested_dict = { 'dictA': {'key_1': 'value_1’},
'dictB': {'key_2': 'value_2'}}
• Here, the nested_dict is a nested dictionary with the dictionary dictA
and dictB.
• They are two dictionary each having own key and value.
Nested dictionaries
Example:
>>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female’}}
>>print(people)
#Accessing elements
>>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female’}}
>>print(people[1]['name’])
>>print(people[1]['age’])
>>print(people[1]['sex'])
Add or updated the nested dictionaries
>>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female’}}
>>people[3] = {}
>>people[3]['name'] = 'Luna’
>>people[3]['age'] = '24’
>>people[3]['sex'] = 'Female’
>>people[3]['married'] = 'No’
>>print(people[3])
Add another dictionary
>>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female'},
3: {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No’}}
>>people[4] = {'name': 'Peter', 'age': '29', 'sex': 'Male', 'married’:
'Yes’}
>>print(people[4]);
Delete elements from the dictionaries
>>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female'},
3: {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No'},
4: {'name': 'Peter', 'age': '29', 'sex': 'Male', 'married': 'Yes’}}
>>del people[3]['married’]
>>del people[4]['married’]
>>print(people[3])
>>print(people[4])
How to delete the dictionary
>>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female'},
3: {'name': 'Luna', 'age': '24', 'sex': 'Female'},
4: {'name': 'Peter', 'age': '29', 'sex': 'Male’}}
>>del people[3], people[4]
>>print(people)
Natural Language and Formal Language
• The languages used by the humans called as a natural languages
• Examples: English, French, Telugu,etc.
• The languages designed and developed by the humans to
communicate with the machines is called as the formal languages.
• Example: C, C++, Java, Python, etc.
Summary
• Introduction to python
• Installation of Python in windows
• Running Python
• Arithmetic operators
• Values and types
• Formal and Natural languages
Any questions
• vikramneerugatti@gmail.com
• www.vikramneerugatti.com

Programming in python Unit-1 Part-1

  • 1.
  • 2.
    content • Introduction topython • Installation of Python in windows • Running Python • Arithmetic operators • Values and types • Formal and Natural languages
  • 3.
    Introduction to Python •Dynamic interoperated language that allows both functional and object oriented programming languages • Interpreter • Python is a language • Simple • On all platforms • Real programming language • High level problems • Can split into modules • GUI can be done • It is a interpreter language • Very short compare with other languages • Indents will be used instead of brackets • After the bbc shows monty python cant do anything on reptiles
  • 4.
  • 5.
  • 6.
  • 7.
    Running a python •Start • Select python • In the prompt type the statement Print (“hello world”)
  • 8.
    Arithmetic operations • Inthe prompt of the python type • 3+4 • 3-4 • ¾ • 3%4 • 3*4 • Type with variables also • A=3 • B=5, etc.
  • 9.
    Values and types •Python numbers • Integer • 2,4,5 • Float • 2.3,4.5,7.8 • Complex • 3+5J • Python lists • can be any • [3, 4, 5, 6, 7, 9] • ['m', 4, 4, 'nani'] • Slicing operator, list index starts from zero • A[4],a[:2],a[1:2] • mutable • Python tuples () • Cannot change, slicing can be done a[4]. • immutable
  • 10.
    Values and types •Python Strings • Sequence of Characters • Single line • Ex. s='zaaaa nani' • Multi line • Ex. s='''zaaaa nani • print(s) • nani''' • print (s) • Sliicing can be done • Ex. S[5]
  • 11.
    Values and types •Python sets • Collection of unordered elements, elements will not be in order • S={3,4,5.6,’g’,8} • Can do intersection (&), union(|), difference (-), ^ etc. • Ex. A&b, a|b, a-b, a^b’ • Remove duplicates • {5,5,5,7,88,8,88} • No slicing can be done, because elements are not in order. • Python dictionaries
  • 12.
    Python dictionaries • Pythondictionary is an unordered collection of items. • While other compound data types have only value as an element, a dictionary has a key: value pair. • Dictionaries are optimized to retrieve values when the key is known. • Creating a dictionary is as simple as placing items inside curly braces {} separated by comma. • An item has a key and the corresponding value expressed as a pair, key: value. • While values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.
  • 13.
    Python dictionaries (creating) 1.#empty dictionary 2.my_dict = {} 4.# dictionary with integer keys 5.my_dict = {1: 'apple', 2: 'ball'} 7.# dictionary with mixed keys 8.my_dict = {'name': 'John', 1: [2, 4, 3]} 10.# using dict() 11.my_dict = dict({1:'apple', 2:'ball'}) 13.# from sequence having each item as a pair 14.my_dict = dict([(1,'apple'), (2,'ball')])
  • 14.
    Python dictionaries (accesing) •my_dict = {'name':'Jack', 'age': 26} • # Output: Jack • print(my_dict['name’]) • # Output: 26 • print(my_dict.get('age’)) • # Trying to access keys which doesn't exist throws error# my_dict.get('address’) • # my_dict['address’] • Key in square brackets/get(), if you use get(), instead of error , none will be displayed.
  • 15.
    Change/ add elementsin a dictionaries • my_dict = {'name':'Jack', 'age': 26} • # update value • my_dict['age'] = 27 • #Output: {'age': 27, 'name': 'Jack’} • print(my_dict) • # add item • my_dict['address'] = 'Downtown’ • # Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}print(my_dict) • Mutable, if key is there: value will be updated, if not created new one.
  • 16.
    How to removeor delete the item in dictionaries • We can remove a particular item in a dictionary by using the method pop(). This method removes as item with the provided key and returns the value. • The method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary. All the items can be removed at once using the clear() method. • We can also use the del keyword to remove individual items or the entire dictionary itself.
  • 17.
    How to removeor delete the item in dictionaries • # create a dictionary squares = {1:1, 2:4, 3:9, 4:16, 5:25} • # remove a particular item # Output: 16 • print(squares.pop(4)) # Output: {1: 1, 2: 4, 3: 9, 5: 25}print(squares) • # remove an arbitrary item • # Output: (1, 1) print(squares.popitem()) # Output: {2: 4, 3: 9, 5: 25} print(squares) • # delete a particular item • del squares[5] # Output: {2: 4, 3: 9}print(squares) • # remove all items • squares.clear() • # Output: {} • print(squares) • # delete the dictionary itself • del squares • # Throws Error# print(squares)
  • 20.
    Nested Dictionary • InPython, a nested dictionary is a dictionary inside a dictionary. • It's a collection of dictionaries into one single dictionary. Example: nested_dict = { 'dictA': {'key_1': 'value_1’}, 'dictB': {'key_2': 'value_2'}} • Here, the nested_dict is a nested dictionary with the dictionary dictA and dictB. • They are two dictionary each having own key and value.
  • 21.
    Nested dictionaries Example: >>people ={1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female’}} >>print(people) #Accessing elements >>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female’}} >>print(people[1]['name’]) >>print(people[1]['age’]) >>print(people[1]['sex'])
  • 22.
    Add or updatedthe nested dictionaries >>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female’}} >>people[3] = {} >>people[3]['name'] = 'Luna’ >>people[3]['age'] = '24’ >>people[3]['sex'] = 'Female’ >>people[3]['married'] = 'No’ >>print(people[3])
  • 23.
    Add another dictionary >>people= {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}, 3: {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No’}} >>people[4] = {'name': 'Peter', 'age': '29', 'sex': 'Male', 'married’: 'Yes’} >>print(people[4]);
  • 24.
    Delete elements fromthe dictionaries >>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}, 3: {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No'}, 4: {'name': 'Peter', 'age': '29', 'sex': 'Male', 'married': 'Yes’}} >>del people[3]['married’] >>del people[4]['married’] >>print(people[3]) >>print(people[4])
  • 25.
    How to deletethe dictionary >>people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}, 3: {'name': 'Luna', 'age': '24', 'sex': 'Female'}, 4: {'name': 'Peter', 'age': '29', 'sex': 'Male’}} >>del people[3], people[4] >>print(people)
  • 26.
    Natural Language andFormal Language • The languages used by the humans called as a natural languages • Examples: English, French, Telugu,etc. • The languages designed and developed by the humans to communicate with the machines is called as the formal languages. • Example: C, C++, Java, Python, etc.
  • 27.
    Summary • Introduction topython • Installation of Python in windows • Running Python • Arithmetic operators • Values and types • Formal and Natural languages
  • 28.