0% found this document useful (0 votes)
28 views

Unit 1

The document discusses the key features and building blocks of the Python programming language including identifiers, keywords, variables, comments, data types like numbers, strings, lists, tuples and dictionaries. It also covers Python's interactive and object-oriented nature and portability across platforms.

Uploaded by

deshmukhayaan81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Unit 1

The document discusses the key features and building blocks of the Python programming language including identifiers, keywords, variables, comments, data types like numbers, strings, lists, tuples and dictionaries. It also covers Python's interactive and object-oriented nature and portability across platforms.

Uploaded by

deshmukhayaan81
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter No: 01: Introduction & Syntax of Python Program (marks = 8)

Features of Python — Interactive, Object — oriented, Interpreted, platform independent

Python building blocks — Identifiers, Keywords, Indention, Variables, Comments

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

Python language is freely available at official website


since, it is open-source, this means that source code is also available to the public. So you
can download it as, use it as well as share it.

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 is Portable language

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.

Interactive and Script mode

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.

Thus, Manpower and manpower are two different identifiers in Python.

Here are naming conventions for Python identifiers-

 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.

Example: Single Line Commment: # This is a comment.

Multiple Line Comment: '''


This is a multiline
comment. '''
Keywords

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.

and exec not

assert finally or

break for pass

class from print

continue global raise

def if return

del import try

elif in while

else is with

except lambda yield

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.

counter = 100 # An integer assignment


miles = 1000.0 # A floating point
name = "John" # A string

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 −

 int (signed integers)


 long (long integers, they can also be represented in octal and hexadecimal)
 float (floating point real values)
 complex (complex numbers)

Example:

int Long (python2) float complex

10 51924361L 0.0 3.14j

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.

String is Immutable data type, i.e. Cannot be change.

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.

String indexing in python

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’

print(subject[1:4]) #output will be ‘yth’


#from 1 position, 4 number of character

print(subject[-3]) #output will be ‘h’

print(subject[2:]) #output will be ‘thon’


#from 2 position upto end of string

print(subject[:4]) #output will be ‘pyth’


#from 0th position, 4 number of character

Following operation not allowed for strings.

subject[0] = ‘M’ # will generate error

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

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


tinylist = [123, 'john']

print list # Prints complete list


print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd

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.

The main differences between lists and tuples are:


Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples
are enclosed in parentheses ( ( ) ) and cannot be updated.

Tuples can be thought of as read-only lists, means tuples are Immutable.

Example

T = ( 'abcd', 786 , 2.23, 'john', 70.2 )

print T # Prints complete list


print T[0] # Prints first element of the list
print T[1:3] # Prints elements starting from 2nd till 3rd
print T[2:] # Prints elements starting from 3rd element

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

square braces ([ ]).

Example 1:

D = {1:‘A’ , 2:‘Apple’ , ‘C’:3000}

print(D) # {1:‘A’ , 2:‘Apple’ , ‘C’:3000}

print(D[1] # A

print(D[‘C’]) # 3000

Example 2:

D = {}
D['one'] = "This is one"
D[2] = "This is two"

print(D)

#output will be {‘one’:’This is one’ , 2:’This is two’}

You might also like