Combined Python
Combined Python
Features
Areas of
python
Program
Learning
Programming
Introduction
Features
Areas of
python
Program
Learning
Programming
Introduction
Features
S Areas of
python
Program
Learning
Programming
Introduction
Features
Areas of
python
Program
Learning
Programming
Introduction
Features
Areas of
python
Program
Learning
Programming
Code &
Environment
Modes in
python
Interactive
mode
Scripting
Mode
THANKS for
watching
Code &
Environment
Modes in
python
Interactive
mode
Scripting
Mode
THANKS for
watching
Code &
Environment
Modes in
python
Interactive
mode
Scripting
Mode
Code &
Environment
Modes in
python
Interactive
mode
Scripting
Mode
THANKS for
watching
Code &
Environment
Modes in
python
Interactive
mode
Scripting
Mode
THANKS for
watching
Code &
Environment
Modes in
python
Interactive
mode
Scripting
Mode
THANKS
Introduction
Features
Areas of
python
Program
Learning
Programming
Character
Set
Letters A-Z, a-z
Digits 0-9 Keywords
Special symbols space+*/ **\O]0// = != ==
< , > .*" &#= >=@_(underscore)
Whitespaces Blank space, tabs (), carriage
return (-), newline, form feed.
Identifiers
Other characters Python can process all
ASCII and Unicode characters as part of data or
literals .
Variable
Every component of a Python program is created using
the character set, be it statements or expressions or
other components of a program. Variable
example
Character
Set
Keywords are reserved words.
Variable
Variable
example
Character
Set
Identifiers are names used to identify a variable,
function, or other entities in a program.
The rules for naming an identifier in Python are as Keywords
follows:
The name should begin with an uppercase or a lowercase
alphabet or an underscore sign (_). This may be followed
by any combination of characters a-z, A-Z, 0-9 or Identifiers
underscore (_). Thus, an identifier cannot start with a
digit.
It can be of any length. (However, it is preferred to
keep it short and meaningful). Variable
It should not be a keyword or reserved word given in
Table.
We cannot use special symbols like !, @, #, $, %, etc. in Variable
identifiers. example
Character
Set
marksMaths marksEnglish
marksIP MYFILE Keywords
avg _DS
Marks_IP chk189
Valid identifiers
Identifiers
Marks-Maths 2marksEnglish
break for Variable
[Link] 231ww
Keywords
Identifiers
Variable
Thanks
Data Types
String literals
Numeric Literals
Thanks
Data Types
Thanks
Data Types
Literals/Values
String literals
Numeric Literals
Thanks
Data Types
Thnaks
Data Types
Thanks
Character
Set
Keywords
Identifiers
Variable
Thanks
Data Types
String literals
Numeric Literals
Thanks
Data Types
Thanks
Data Types
Literals/Values
String literals
Numeric Literals
Thanks
Data Types
Thnaks
Data Types
Thanks
Data Types
A=10
B=15 String literals
C=35
D=1234
Numeric Literals
Note don’t start number with zero like 012 because it is
treated as octal not simple number.
Thanks
Data Types
Float data type contains fractional form and exponential
form numbers . It is real literal.
Examples of float literals fractional form Literals/Values
A=2.0
B=17.5
C=-13.0
D=0.3 String literals
Note if no decimal then it is not real literal examples
7
+17/2 Numeric Literals
[Link] All are invalid
17,250.25
Thanks
Data Types
Float data type contains fractional form and exponential
form numbers . It is real literal.
Examples of float literals exponential form Literals/Values
Exponential examples 0.58E01 can be written as
A=152E05
B=1.52E07
C=152E+8 String literals
1.7E ( no exponent)
0.17E2.3 ( exponent can’t have
fractional part) All are invalid Numeric Literals
17,25E25 ( No Comma allowed)
Thanks
Data Types
A Boolean literal in Python represent two types of values
True or False
Example Literals/Values
Markgiven=True
Markgiven=Flase
Test it like this
type(Markgiven) String literals
You will get its datatype as bool.
Keywords
Identifiers
Variable
Thanks
Data Types
Sequence
String
Sequence
List, Tuple
Mapping
Dictionary
Data Types
Sequence
List ,Tuple
Boolean data type (bool) is a subtype of integer. It is a unique
data type, consisting of two constants, True and False.
Boolean True value is non-zero. Boolean False is the value
Thanks
zero.
Data Types
Example 3.2
#To create a list Sequence
String
>>> list1 = [5, 3.4, "New Delhi", "20C", 45]
Numeric
Data Type
Sequences
Mappings
Dictionary
Thanks
Operators
Arithmetic
Operators
Relational
Operators
Logical
Operators
Membership
Operators
Operators
Membership
Operators
Operators
Arithmetic
Operators
Relational
Operators
Logical
Operators
Membership
Operators
Operators
Arithmetic
Operators
Assignment
Operators
Logical
Operators
Membership
Operators
Operators
Arithmetic
Operators
Relational
Operators
Logical
Operators
Membership
Operators
Operators
Arithmetic
Operators
Relational
Operators
Logical
Operators
Membership
Operators
Data Type
Numeric
Data Type
Sequences
Mappings
Dictionary
Thanks
An expression is defined as a combination of constants,
variables and operators.
An expression always evaluates to a value.
A value or a standalone variable is also considered as an
expression but a standalone operator is not an expression.
Some examples of valid expressions are given below.
(i) num – 20.4
(ii) 3.0 + 3.14
(iii) 23/3 -5 * 7(14 -2)
(iv) "Global"+ "Citizen"
An expression represents something ,a statement is a
programming instruction that does something i.e. ,some
action takes place .
An expression always evaluates to a value. While statement
is executed i.e. takes action.
It is not necessary that a statement results in a value ;it may
or may not yield a value .
Some examples of valid statements are given below.
(i) a=15
(ii) b=a-10
(iii) print(a+3)
Precedence of Operators
When an expression contains more than one operator, their
precedence (order or hierarchy) determines which operator
should be applied first.
Higher precedence operator is evaluated before the lower
precedence operator.
In the an expression , '*'and '/' have higher precedence than
'+' and '-'.
Note:
a) Parenthesis can be used to override the precedence of
operators. The expression within () is evaluated first.
b) For operators with equal precedence, the expression
is evaluated from left to right
Precedence of Operators
Example 3.5 How will Python evaluate the following expression?
20 + 30 * 40
Solution:
#precedence of * is more than that of +
= 20 + 1200 #Step 1
= 1220 #Step 2
p,q= 3,5
q,r= p-2,p+2
print(p,q,r)
Result is 3,1,5
1. Write a program to input a welcome message and print it.
# Code starts here
message=input(“Enter welcome message:”)
print()
print( “Hello”, message)
Output
Enter welcome message : welcome to python class
Output
Enter kilometres : 20
Kilometres: 20
Miles: 12.42742
4. Write a program to input a value in tonnes and convert it into quintals and
kilograms.
(1 tonne=10 quintals 1 tonne = 1000kgs, 1 quintal= 100kgs)
# Program starts here
tonnes = float ( input ("Enter tonnes: "))
quintals = tonnes * 10
kgs = quintals * 100
print(“Tonnes:", tonnes)
print("Quintals : ", quintals)
print ("Kilograms: ", kgs)
Output
Enter tonnes: 2.5
Tonnes: 2.5
Quintals: 25.0
Kilograms : 2500.0
5. Write a program to enter a small poem and print it.
# Program starts here
poem= input ("Enter a small poem: ")
print("The poem you entered")
print (poem)
Output
Enter a small poem : "Hope" is the thing with feathers- That perches in the
soul- And sings the tune without the words- And never stops at all -EMILY
DICKINSON
The poem you entered
"Hope" is the thing with feathers- That perches in the soul- And sings the
tune without the words- And never stops at all -EMILY DICKINSON
Debugging
Due to errors, a program may not execute or may
generate wrong output. :
i) Syntax errors
ii) Logical errors
iii) Runtime errors
Syntax Errors
Like any programming language, Python has rules that
determine how a program is to be written.
This is called syntax. The interpreter can interpret a statement of
a program only if it is syntactically correct.
For example , parentheses must be in pairs, so the expression
(10 +12) is syntactically correct, whereas (7 + 11 is not due to
absence of right parenthesis.
If any syntax error is present, the interpreter shows error
message(s) and stops the execution there.
Such errors need to be removed before execution of the
program.
Logical Errors
A logical error/bug (called semantic error) does not stop
execution but the program behaves incorrectly and produces
undesired /wrong output. Since the program interprets
successfully even when logical errors are present in it, it is
sometimes difficult to identify these errors.
For example, if we wish to find the average of two
numbers 10 and 12 and we write the code as 10 + 12/2,
it would run successfully and produce the result 16, which is
wrong. The correct code to find the average should have been
(10 + 12) /2 to get the output as 11.
Runtime Error
A runtime error causes abnormal termination of program while
it is executing. Runtime error is when the statement is correct
syntactically, but the interpreter can not execute it.
Example
Check whether a number is positive, negative, or zero.
We can simply call the function by writing the function name CalcCompInt
whenever compound interest is to be computed and thus reuse the code to
save time and efforts.
Python has many predefined functions called built-in functions. We have
already used two built-in functions print() and input(). A module is a python
file in which multiple functions are grouped together. These functions can be
easily used in a Python program by importing the module using import
command. Use of built-in functions makes programming faster and efficient.
To use a built-in function we must know the following about that function:
• Function Name — name of the function.
• Arguments — While calling a function, we may pass value(s), called
argument, enclosed in parenthesis, to the function. The function works
based on these values. A function may or may not have argument(s).
• Return Value − A function may or may not return one or more values. A
function performs operations on the basis of argument (s) passed to it and
the result is passed back to the calling point. Some functions do not return
any value.
Python has many predefined functions called built-in functions. We have
Let us consider the following Python program using three built-in functions
Observe:
• Two built-in functions are used in the first statement, int() and input().
The third line has a function print().
• The input function accepts an argument, “Enter your name”. Argument(s)
is the value(s) passed within the parenthesis.
• Similarly the print function has four arguments "the square of", num, "is", square
separated by commas.
• The int function in the first line takes as argument the value entered by the user
from the keyboard and converts it into a string and returns it. Thus the return value
from the int() function is an integer.
Some of the most commonly used built-in functions in Python are listed in Table 3.8
under four broad categories.
To repeat certain things for a particular number of times. Iterative
statements are used.
For example, a program has to display attendance for every student of a
class. Here the program has to execute the print statement for every
student.
When all the items in the range are exhausted, the statements within loop
are not executed and Python interpreter starts executing the statements
immediately following the for loop.
While using for loop, we should know in advance the number of times the
loop will execute.
Syntax of the for Loop:
for <control-variable> in <sequence/items in range>:
<statements inside body of the
loop>
Program to print even numbers in a given sequence using for loop.
#Print even numbers in the given sequence
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
Note: Body of the loop is indented with respect to the for statement.
The range() Function:
The range() is a built-in function in Python.
Output
Program : Program to print sum of natural numbers between 1 to 7.
Output
A loop may contain another loop inside it. A loop inside another loop is called
a nested loop.
Output
*
**
***
****
#Demonstrate working of nested for loops
for var1 in range(3):
print( "Iteration " + str(var1 + 1) + " of outer loop")
for var2 in range(2): #nested loop
print(var2 + 1)
print("Out of inner loop")
print("Out of outer loop")
Output:
Iteration 1 of outer loop
12
Out of inner loop
Iteration 2 of outer loop
12
Out of inner loop
Iteration 3 of outer loop
12
Out of inner loop
Out of outer loop
A loop may contain another loop inside it. A loop inside another loop is called
a nested loop.
Output
?????
A loop may contain another loop inside it. A loop inside another loop is called
a nested loop.
Output
?????