Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Introduction To Python Programming

The document discusses Python programming including its history, advantages, basics, data types, variables, statements, functions of print and type. It also discusses the difference between interactive and script mode in Python and provides examples of writing simple Python programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Introduction To Python Programming

The document discusses Python programming including its history, advantages, basics, data types, variables, statements, functions of print and type. It also discusses the difference between interactive and script mode in Python and provides examples of writing simple Python programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Python Programming

Introduction
History of Python
Python is a high level programming language developed by
Guido Rossum in 1989 at National Research Institute for
Mathematics and Computer Science in Netherlands.
Python became a popular programming language, widely used
in both industry and academia because of its simple, concise
and extensive support of libraries.
Advantages of Python
COBOL, C, C++ and Java are a few of the many programming
languages available. So, why Python??
There are some well-known advantages of Python language
which make it a popular programming language. These are given
below:
1. Readability: Readability of the code is most crucial factors in
programming. The longest part of any software’s life cycle is
maintenance. If a software has a highly readable code, then it is
easier to maintain and update the software. Python offers more
readability of code when compared to other programming
languages.
Advantages of Python
2. Portability: Python is platform independent i.e., it runs on all
platforms. The language is designed for portability.
3. Vast support of libraries: Python has a large collection of in-built
functionalities called standard library functions e.g., NumPy
provide support for large, multidimensional arrays and matrices.
4. Software integration: It can easily extend communicate and
integrate with other languages. Python code can invoke libraries of
C and C++ programming languages. It can also communicate with
Java and .net components.
Advantages of Python
5. Developer’s productivity: It is dynamically types language. There
is no need to declare variables explicitly. Also size of code written
is typically smaller than half of the code written in some other
languages such as C, C++ or Java.
As the size of code is reduced, there is less time to type and
debug. The amount of time needed to compile and execute the
code is also less.
Basics of Python Programming
Program written in any programming language consists of set of
instructions or statements. Here, all these instructions are written
using specific words and symbols according to the syntax rules or
the grammar of language.
Hence, every programmer should know these rules i.e., the syntax
supported by language for the proper execution of a program.
Python interactive shell
Python character set
Any Program written in Python contains words or statements
which follow a sequence of characters. When these characters are
submitted to the Python interpreter, they are interpreted or
uniquely identified in various contexts, such as characters,
identifiers, names, or constants.
Character Set:
Letters: Uppercase and lowercase
Digits: 0,1,2,…,9
Special symbols: _,+,-,&,$,#,:,;
White spaces: \t,\n etc.
Token
Python program consist of sequence of instructions. Python
break statement into a set of lexical components called
token. Each token corresponds to a substring of statements.
Python Tokens:
• Literals
• Keywords
• Identifiers/variables
• Operators
• Delimiters
Literals
Literals are numbers or strings or characters that appear directly in a
program.
E.g., 78 //Integer literal
• 24.78 // floating point literal
• ‘R’ //character literal
• “hello” // string literal
Python offers an in-built method called type to know the exact type of the literals.
e.g.
>>>type(‘hello’)
<class ‘str’>
>>>type (123)
<class ‘int’>
Keywords
Keywords are reserved words with fixed meaning assigned to them. Keywords can
not be used as identifiers or variables.

Keywords in Python programming language


False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
The above keywords may get altered in different versions of Python. Some extra
might get added or some might be removed.
Operator and Delimiter
Operators: Python contains various operators like Arithmetic operators
Assignment operators, Comparison operators, Logical operators,
Identity operators, Membership operators, Bitwise operators.
Delimiters are symbols that perform a special role in Python like
grouping, punctuation and assignments. Following symbols are used as
delimiters in python.
(), [], {}, , :, ;, = etc.
Identifier/Variable
Identifier is the name used to find a variable, function, class or other objects.
All identifiers must obey following rules:
• Is a sequence of characters that consists of letters, digits, and underscore.
• Can be of any length.
• Starts with a letter (upper or lower case).
• Can start with an underscore.
• Cannot start with a digit
• Cannot be a keyword.
• Some examples of valid identifiers: Firstname, Roll_no, A1
• Variable names are case sensitive, so a variable named greeting is
• not the same as a variable named Greeting.
Python core datatypes
Floating point datatype: it consist of whole number, decimal point and a fractional part. A floating point
number can be written using decimal notation and scientific notation.
Example:
>>>3.4e1
34
>>>3.4*10
34
>>>34.4
34.4
Float function: converts a string to float; example
>>>float(‘12.6’)
12.6
Python core data types
Complex Number: can be expressed in the form a+bj;
Example:
>>>2+5j
(2+5j)
>>>type(2+3j)
<class ‘complex’>
>>>8j
8j
Python core data types
Boolean data type: Boolean data type is represented in
python as bool. It is a primitive data type having one of
the two values viz. True and False. True value is
represented as 1 and false as 0.
>>>type(True)
<class ‘bool’)
>>>5==4
False
>>>10==10
True
Python core data types
String datatype can be str function: converts a number into
a string.
created using single and
Example;
double quote.
>>>str(12.5)
Example: ‘12.5’
>>>s=“hello”
‘hello’
>>>s=‘hello’
>>>s
‘hello’
String Function
str function: converts a
number into a string.
Example;
>>>str(12.5)
‘12.5’
Print Function
It is used to display contents
on the screen.
syntax:
print(arguments)
Print function
WAP to display the message “hello”, “world”. The message
should get displayed on a different line.
print (‘Hello’)
print(‘World’)
The print function automatically prints a linefeed(\n) to cause the
output to advance to the next line. To display the message in one
line print function is invoked with a special argument named end=‘
‘.
print (‘Hello’, end=‘ ‘)
print(‘World’)
Difference between Interactive mode and Script
Mode Interactive Mode Script Mode
In the script mode, the Python program is written
It is a way of executing a Python program in in a file. Python interpreter reads the file and
which statements are written in command then executes it and provides the desired result.
prompt and result is obtained on the same. The program is compiled in the command
prompt.
The interactive mode is more suitable for writing Script mode is more suitable for writing long
very short programs. programs.
Editing of code can be done but it is a tedious
Editing of code can be easily done in script mode.
task.
We get output for every single line of code in
In script mode entire program is first compiled
interactive mode i.e. result is obtained after
and then executed.
execution of each line of code.
Code cannot be saved and used in the future. Code can be saved and can be used in the future.
It is more preferred by experts than the
It is more preferred by beginners.
beginners to use script mode.
Assigning values to a variable
In Python, the equal sign(=) is used as the assignment
operator. The statement for assigning a value to a variable is
called assignment statement.
Syntax:
variable= expression
Example:
>>>z=1 >>>radius=8
>>>z >>>radius
>>>e= (5+1)*8
48
Statement in Python
In Python, statement is a command that programmer
gives to the program. Statement in Python can be an
assignment statement, expression of print statement. The
example of statement is as follows:
>>>a=10 # Assignment statement
>>>b=20 # Assignment statement
>>>a*b # Statement with expression
200
More on assigning values to a variable
Assignment to multiple variables:
>>>p=q=r=100
Multiple assignments:
Var1, Var2, Var3, ….= Exp1, Exp2,
Exp3…
Example:
>>>p=20 Using multiple assignments,
>>>q=10 the task can be simplified
>>>temp=p as follows:
>>>p=q >>>p,q=q,p
>>>q=temp
Writing simple program in Python
Step1: Design a algorithm for the given problem. An
algorithm describes how a problem is to be solved by
listing all the actions need to be carried out. An
algorithm helps the programmer to plan for the
program before actually writing it in a programming
language.
Step 2: Translate the algorithm into programming
instructions or code.
Writing program in Python to calculate the area of
a rectangle
Length =10
Breadth =20
print(‘length =‘, length, ‘breadth=‘, breadth)
Area=Length*Breadth
print(‘Area is=‘, Area)

You might also like