You are on page 1of 54

INTRODUCTION TO PYTHON

Python is Interpreted

Python is Interactive

Python is Object-Oriented

Python is Beginner's Language


Python Features

Easy-to-learn

Easy-to-read

Easy-to-maintain

A broad standard library

Interactive Mode

Portable

GUI Programming
Advantages:
• Software quality

• Developer productivity

• Program portability

• Support libraries

• Component integration

• Enjoyment

Disadvantages:

• not always be as fast as that of compiled languages such as C and

C++
Create a Python List

myList = ["The", "earth", "revolves", "around", "sun"]

>>> myList

>>> myList[0]

>>> myList[4]

>>> myList[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> myList[-1]

Add Elements to a List

>>> myList.insert(0,"Yes")

>>> myList.append(["a", "true"])

>>> len(myList)
>>> myList.extend(["statement", "for", "sure"])

Slice Elements from a List

can access a part of complete list by using index range.

>>> myList[1:4]

>>> myList[:4]

>>> myList[4:]

>>> myList[:]
Search the Lists and find Elements

>>> myList.index("revolves")

>>> myList.index("a")

>>> myList.index(["a", "true"])

>>> "sun" in myList

Delete Elements from the List

>>> myList.remove("Yes")

>>> myList.remove(["a", "true"])


Python List Operators

>>> myList = myList + ["sure"]

>>> myList += ["."]

>>> myList *= 2
Create a Python Dictionary

>>> myDict = {"A":"Apple", "B":"Boy", "C":"Cat"}

Access Dictionary Elements

>>> myDict["A"]
'Apple'
>>> myDict["B"]
'Boy'
>>> myDict["C"]
'Cat'

Update Dictionary Elements

>>> myDict["A"] = "Application"

>>> myDict["A"]
Delete Dictionary Elements

>>> myDict
{'A': 'Application', 'C': 'Cat', 'B': 'Boy'}
>>> del myDict["A"]
>>> myDict
{'C': 'Cat', 'B': 'Boy'}

>>> myDict
{'C': 'Cat', 'B': 'Boy'}
>>> myDict.clear()
>>> myDict
{}
Characteristics of Python Dictionaries

Dictionaries are Unordered

>>> myDict["D"] = "Dog"


>>> myDict
{'A': 'Apple', 'C': 'Cat', 'B': 'Boy', 'D': 'Dog'}
>>> myDict["E"] = "Elephant"
>>> myDict
{'A': 'Apple', 'C': 'Cat', 'B': 'Boy', 'E': 'Elephant', 'D': 'Dog'}

Dictionary Keys are Case Sensitive

>>> myDict["F"] = "Fan"


>>> myDict["f"] = "freeze"
>>> myDict
{'A': 'Apple', 'C': 'Cat', 'B': 'Boy', 'E': 'Elephant', 'D': 'Dog', 'F': 'Fan', 'f': 'freeze'}
Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered
zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end. Example: Your
many cats' names.
Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are
stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the
names of the months of the year.

Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for
each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't
numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for
each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't
numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the
values in dictionaries. Example: telephone book.
Function Basics – def Statements

You might also like