Programming with Python(22616)
UNIT I : Introduction and Syntax of Python Program
Features of python –
1. Python is Interactive – You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
2. Python is Object-Oriented – Python supports object oriented language and concepts of
classes and objects come into existence.
3. Python is Interpreted Python is an interpreted language i.e. interpreter executes the code
line by line at a time. This makes debugging easy and thus suitable for beginners.
4. Python is Platform Independent Python can run equally on different platforms such as
Windows, Linux, Unix and Macintosh etc. So, we can say that Python is a portable
language.
5. It is high level programming language.
6. Python is free and open source programming language.
Python Building Blocks:
1. Python Identifiers and Variable
Python Identifiers Variable name is known as identifier. A variable is nothing but a reserved
memory location to store values.
The rules to name an identifier are given below:
- The first character of the variable must be an alphabet or underscore ( _ ).
- All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore or digit (0-9).
- Identifier name must not contain any white-space, or special character (!, @, #, %, ^,
&, *).
- Identifier name must not be similar to any keyword defined in the language.
- Identifier names are case sensitive.
- for example my name, and MyName is not the same.
- Examples of valid identifiers : a123, _n, n_9, etc.
- Examples of invalid identifiers: 1a, n%4, n 9, etc.
[Link]
Programming with Python(22616)
2. Keywords
The name of keywords can not be used as variable name. For instance a variable
name can not be from because it is a keyword.
3. Indentation
- 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
compulsory.
- White space at the beginning of the logical line is called indentation.
- For example :
a= 1;
if a<=2; here the line is indented that means this statement
will
print (‘True’) execute only if the if statement is true.
4. Comments
- Use the hash (#) symbol to start writing a comment.
- E.g #This is a comment
print('Hello')
- Multi-line comments use triple quotes, either ''' or """
. eg: . """This is also a perfect example of multi-line comments"""
[Link]
Programming with Python(22616)
Modes of working in Python :
Python has two basic modes: script and interactive.
1. Interactive mode is a command line shell which gives immediate feedback for each
statement, while running previously fed statements in active memory.
2. Script mode is also called as normal mode. This is mode in which the python
commanda are stores in file and the file is saved using the extension .py
Python Data Types :
A data type defines the type of data, for example 123 is an integer data while “hello” is a String
type of data.
The data types in Python are divided in two categories:
Data Types
Mutable Immutable
List Dictionary Set Numeric Strings Tuples
Mutable date types are those, whose value can be change after [Link], Sets, and
Dictionary in Python are examples of some mutable data types in Python.
Immutable data types are those, whose values cannot be modified once they are created.
Examples of immutable data types are int, str, bool, float, tuple, etc.
1. Numeric :
Python numeric data type is used to hold numeric values like,
int – holds signed integers of non-limited length.
long- holds long integers(exists in Python 2.x, deprecated in Python 3.x).
float- holds floating precision numbers and it’s accurate upto 15 decimal places.
complex- holds complex numbers
[Link]
Programming with Python(22616)
2. String
The string is a sequence of characters. Python supports Unicode characters. Generally,
strings are represented by either single or double quotes
3. List
List is an ordered sequence of some data written using square brackets ([]) and commas
(,).
[Link]
Programming with Python(22616)
4. Tuple
Tuple is another data type which is a sequence of data similar to list. But it is immutable.
That means data in a tuple is write protected. Data in a tuple is written using parenthesis
and commas.
5. Dictionary
Python Dictionary is an unordered sequence of data of key-value pair form. It is similar
to the hash table type. Dictionaries are written within curly braces in the form key: value.
Assignment Questions :
1. Enlist different features of python.
2. List and define modes in Python.
3. Describe Python Building Blocks.
4. List and Explain different data types used in Python.
5. What are the rules to be followed for variable names.
6. Write a Python Program to display welcome message.
[Link]