Unit 1
Unit 1
Python environment setup — Installation and working of IDE, Running Simple Python
scripts to display 'welcome' message.
Python Data Types: Numbers, String, Tuples, Lists, Dictionary. Declaration and use of
data types
Features of Python:
Free and Open Source
Object-Oriented Language
One of the key features of python is Object-Oriented programming. Python supports object
oriented language and concepts of classes, objects encapsulation etc.
Python language is also a portable language, for example, if we have python code for
windows and if we want to run this code on other platform such as Linux, UNIX and Mac
then we do not need to change it, we can run this code on any platform.
Interpreted Language
Python is an Interpreted Language. Because python code is executed line by line at a time. Like other
language c, c++, java etc. there is no need to compile python code this makes it easier to debug our
code. The source code of python is converted into an immediate form called bytecode.
Python has two basic modes: script and interactive. The normal mode is the mode where the
scripted .py files are run in the Python interpreter.
Interactive mode is a command line shell which gives immediate feedback for each statement, while
running previously fed statements in active memory.
Python Building Blocks
Identifiers
A Python identifier is a name used to identify a variable, function, class, module or other object. An
identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case
sensitive programming language.
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strong private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language- defined
special name.
Indention
Python provides no braces to indicate blocks of code for class and function definitions or flow
control. Blocks of code are denoted by line indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements within the block must be
indented the same amount. For example −
if True:
print "1"
else:
print "0"
However, the following block generates an error −
if True:
print "Answer"
print "1"
else:
print "Answer"
print "0"
Comments in Python
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to
the end of the physical line are part of the comment and the Python interpreter ignores them.
The following list shows the Python keywords. These are reserved words and you cannot use them
as constant or variable or any other identifier names. All the Python keywords contain lowercase
letters only.
assert finally or
def if return
elif in while
else is with
Variable
Variables are nothing but reserved memory locations to store values. This means that when you
create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be
stored in the reserved memory.
Python doesn’t required to declare a variable, instead it dynamically a type is assigned to a variable.
For example in following example whenever we are assigning different type values according to type
of value a variable is also assigned with a same type.
Python allows you to assign a single value to several variables simultaneously. For example –
a = b = c = 1
Python Data Types
Numbers
String
List
Tuple
Dictionary
Numeric Type
Python supports four different numerical types −
Example:
String Type
Strings in Python are identified as a contiguous set of characters represented in the quotation marks.
Python allows for either pairs of single or double quotes.
Subsets of strings can be taken using the slice operator ([ ] and [:]) with indexes starting at 0 in the
beginning of the string and working their way from -1 at the end.
Subject = “Python”
Left --> 0 1 2 3 4 5
P y t h o n
Right --> -6 -5 -4 -3 -2 -1
Accessing String in python
subject=’Python’
print(subject[0]) #output will be ‘P’
List
Lists are ordered and mutable data types in python.
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.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes
starting at 0 in the beginning of the list and working their way to end -1.
Example
Tuple
A tuple is another sequence data type that is similar to the list. A tuple consists of a number
of values separated by commas. Unlike lists, however, tuples are enclosed within round
parentheses.
Example
Dictionary
Dictionary in python is a collection of key value pairs.
A dictionary key can be almost any Python type, but are usually numbers or strings. Values,
on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using
Example 1:
print(D[1] # A
print(D[‘C’]) # 3000
Example 2:
D = {}
D['one'] = "This is one"
D[2] = "This is two"
print(D)