18mit14c U1
18mit14c U1
Radha Priya 1
PYTHON PROGRAMMING
Introduction
• Python is a high-level, interpreted, general purpose, dynamic
programming language.
• Python Started in late 1980’s and its usage began in Dec 1989.
• Python has the property of code termed reusability
• Python can be used in multiple programming styles, including object-
oriented, functional programming, procedural programming.
• Python can be used on any OS.
• Python is free and open-source Software
Python Overview
The key features are
• The code written in python is automatically compiled to byte code and
executed.
• It can be used as a scripting language, for implementing web applications
• Extending python with C and C++ increase the performance and speed of
execution.
• Text Editors
• Full IDEs(integrated Development Environment)
• Notebook Environments
Text Editors
• General editors for any text file
• Sublime text and atom(popular editors)
• Sublime text editor – www.sublimetext.com
Full IDEs
• Development environment designed specifically for python
• Larger programs
• Only Community editions are free
• Designed specifically for python, lots of extra functionality
• Most popular pycharm and spyder
PYTHON COMMENTS #
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
Comments starts with a #, and Python will ignore them:
Ex:
>>>#This is a comment
>>>print("Hello, World!")
Hello World!
>>>8+9#addition
17
prepared by S.Radha Priya 8
PYTHON IDENTIFIER
An identifier is a name given to entities like class, functions, variables, etc. It
helps to differentiate one entity from another.
Rules for writing identifiers
1. Identifiers can be a combination of letters in lowercase (a to z) or
uppercase (A to Z) or digits (0 to 9) or an underscore _
2. An identifier cannot start with a digit.
3. Keywords cannot be used as identifiers.
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier
Ex: Valid identifier
MyName
My_Name
Your_name
print_this_to_screen
Invalid identifier
1variable = invalid
a@=invalid
• Strings:Strings in Python are used to store textual information. They are used
to carry out operations that perform positional ordering among items.
• Lists:The list data type is the most generic Python data type. Lists can consist
of a collection of mixed data types, stored by relative positions.
• Tuples:Tuples are one among the immutable Python data types that can store
values of mixed data types. They are basically a list that cannot be changed.
The number data type is divided into the following five data types:
• Integer
• Long Integer
• Octal and Hexadecimal
• Floating-point Numbers
• Complex Numbers
Integers in Python
Python integers are nothing but whole numbers, whose range dependents on
the hardware on which Python is run. Integers can be of different types such
as positive, negative, zero, and long.
Example:
I = 123 #Positive Integer
J = -20 #Negative Integer
K=0 #Zero Integer
Floating-point numbers symbolize the real numbers that are written with a
decimal point dividing the integer and fractional parts.
Floating-point numbers may also come with scientific notation with E or e,
indicating the power of 10.
Example:
5.6e2
I = 2.5
J = 7.5e4
two_dim_list = [ [0]*3 ] *3
print(two_dim_list)
Output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Three-dimensional Lists in Python:
two_dim_list = [[ [0]*3 ] *3]*3
print(two_dim_list)
Output:
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
Example
List = [1,2,3,4,5]
List1 = [ i for i in range(5)]
print(List1)
Output: [0, 1, 2, 3, 4]
Arithmetic Operators
These operators are used to perform arithmetic operations such as
addition, subtraction, multiplication and division. Arithmetic operators are:
== If the values of two operands are equal, then the (a == b) is not true.
condition becomes true.
<> If values of two operands are not equal, then (a <> b) is true. This is similar to != operator.
condition becomes true.
> If the value of left operand is greater than the value (a > b) is not true.
of right operand, then condition becomes true.
< If the value of left operand is less than the value of (a < b) is true.
right operand, then condition becomes true.
>= If the value of left operand is greater than or equal (a >= b) is not true.
to the value of right operand, then condition
becomes true.
<= If the value of left operand is less than or equal to (a <= b) is true.
the value of right operand, then condition becomes
true.
prepared by S.Radha Priya 31
a = 21
b= 10
c=0
if ( a == b ):
print "Line 1 - a is equal to b"
else:
print "Line 1 - a is not equal to b“
if ( a != b ):
print "Line 2 - a is not equal to b"
else:
print "Line 2 - a is equal to b"
if ( a <> b ):
print "Line 3 - a is not equal to b“
else:
print "Line 3 - a is equal to b"
if ( a < b ):
print "Line 4 - a is less than b“
else:
print "Line 4 - a is not less than b"
prepared by S.Radha Priya 32
if ( a > b ):
print "Line 5 - a is greater than b"
else:
print "Line 5 - a is not greater than b"
a = 5;
b = 20;
if ( a <= b ):
print "Line 6 - a is either less than or equal to b“
else:
print "Line 6 - a is neither less than nor equal to b"
if ( b >= a ):
print "Line 7 - b is either greater than or equal to b“
else:
print "Line 7 - b is neither greater than nor equal to b"
~+- Complement, unary plus and minus (method names for the last two are +@ and
-@)
>>>s=“hello python”
>>>print s.lower()
hello python
>>>print s.upper()
HELLO PYTHON
>>>print s.find(“l”)
2
>>>print s.replace(“l”,”p”)
Heppo python
>>>print s.count(“o”) prepared by S.Radha Priya 53
2
>>>print len(s)
12
>>>s=“Hello”
>>>print s.isalpha() #string contain alphabets
True
>>>print s.isdigit() #string does not contain digits
False
Control Statements
• There may be a situation when you need to execute a block of code
several number of times.
• Programming languages provide various control structures that allow for
more complicated execution paths.
• A loop statement allows us to execute a statement or group of statements
multiple times. The following diagram illustrates a loop statement −
range() function
It is a built-in function in python that helps us to iterate over a sequence of
numbers.
>>>range(8)
[0,1,2,3,4,5,6,7] #range starts from 0 and ends n-1.
>>>range(3,9)
[3,4,5,6,7,8] #begin index 3 end index 9(9-1=8)
>>>range[3,40,5]
[3,8,13,18,23,28,33,38]
If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if
statement is executed. If boolean expression evaluates to FALSE, then the first set of code
after the end of the if statement(s) is executed.
Output
Enter the first number 12
Enter the second number 12
Both the numbers entered are equal
Ex:
n1=int(input(“enter the first number”))
n2=int(input(“enter the second number’))
If n1>n2:
print(n1, “ is greater than”,n2)
Else:
print(n2,” is greater than”,n1)
Output
Enter the number 45
Entered number is 45
45 is divisible by 5 or 10
• There may be a situation when you want to check for another condition
after a condition resolves to true. In such a situation, you can use the
nested if construct.
• In a nested if construct, you can have an if...elif...else construct inside
another if...elif...else construct.
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
a. Input() function:
• Input() function has an optional parameter, which is a prompt string.
• When the input() function is called, in order to take input from the user
then the execution of program halts and waits for the user to provide an
input.
• The input given by user through keyboard and it is ended by return key.
• If the input is integer it will return integer value. If the input is string it will
return string value.
Ex:
name=input(“what is your name”)
Print(“hello”+name+”!”)
Output
What is your name? rama
Hello rama!