IDLE - Python shell or interactive mode
two important component of a program are:
1. code / instructions
2. data
in Python data can be classified into two broad categories
1. numbers
a) int - integers
... -4, -3, -2, -1, 0, 1, 2, 3, ...
10 is int
b) float - floating point value
real number
... -2.5, -1.2, -0.7, 0, 0.5, 1.7, ...
10.0 is float
c) complex
2. non numbers
1. str - string
anything enclosed with ' / "
'DPS', "ahmadi", '***', "1234",
'2.75'
'Pin Code - 61010'
2. bool - Boolean
True or False
print() - displays value(s) on the screen
print() can display more than one value
in that case values are to separated by comma (,)
but does not appear in the output
comma(,) replaced by space
how to display comma(,) in the output
one has to use sep=',' in the print() function
with numbers we can:
1. add +
int+int is int
float+float is float
int+float is float
float+int is float
2. subtract -
int-int is int
float-float is float
int-float is float
float-int is float
3. multiply *
int*int is int
float*float is float
int*float is float
float*int is float
4. divide /, //
/ real division
always returns a float value
// floor division
int//int is int
float//float is float
int//float is float
float//int is float
floor division returns the quotient
5. remainder operator (modulo operator) %
returns remainder of numerator divide
by denominator
int%int is int
float%float is float
int%float is float
float%int is float
6. raise to the power **
a^b a raised to the power b
in Python a^b is written as a**b
operation with string
1. + concatenation
2. * replicating a string
variable is container
what does variable contain?
a variable contains value
why variable is called a variable
content of a variable varies
how to create a variable
1. by assigning a value
2. by inputting a value from the keyboard during run-time
type() - returns data type of a value / a variable / an expression
data type of a variable in Python depends on the data type of the value stored in
the variable
when creating a variable, variable name must be on the LHS of the assignment
operator (=)
rules for naming a variable (identifier)
1. an identifier (variable) name must start with a letter or an underscore (_)
2. an identifier (variable) name may contain more than one characters, second
character onwards one may use letter / digit / underscore (_)
3. no special characters are allowed in an identifier name (a variable name) except
underscore (_)
4. identifier names (variable names) are case sensitive
5. a keyword cannot be used as an identifier name (a variable name)
writing first Python program
1. start IDLE
2. click file and then click New File
OR
CTRL+N
New window will open
3. write at least one python statement
4. save the program
5. execute the program by clicking Run and then clicking Run Module
OR
press function key F5
Write a Python program (script) to do the following:
create a variable and assign your name
create another variable to assign marks obtain in the math exam
display the name and the marks
Write a Python program to create three floating point variables; calculate the sum
and the average of the values stored in the three variables; display the sum and
the average.
input() functions can only input string value
using input() function one can input some value from the keyboard during the run-
time (during the execution of a program)
when using input(), generally a string is used as a prompt
print()
type()
input()
int()
float()
eval()
lowest precedence: binary +, binary -
next level: *, /, //, %
highest: **
token: is a program element
building block of a program
five categories of token
1. keyword: is also called reserve word and it has a special meaning
keywords are stored in Python compiler
keywords cannot used as an identifier name
list of keywords
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
2. identifier: a token which is identified by the Python compiler
a) built-in: these are the identifier created by the developer of Python
* built-in identifier - built-in functions, classes, objects
built-in functions: print(), input(), type(), int(), float(), eval(), str()
functions from the module: math module, random module, statistics module
methods from objects
b) user defined: identifiers created by the programmer
variables created by a programmer
unlike keywords, built-in identifer can be used as a variable name
it is a bad practice to use a bult-in identifier as a variable name
3. operators:
a) arithmetic operators:
unary +, unary -, binary +, binary -, *, /, //, %, **
b) relational: >, >=, <, <=, ==, !=
logical operators: not, and, or
c) unary operators: needs one operand
examples: unary +, unary -, not
d) binary operators: needs two operands
binary +, binary -, *, /, //, %, **
augmented assignment operators: +=, -=, /=, //=, %=, **=
assignment operator (=)
e) operators working from left to right:
unary +, unary -, binary +, binary -, *, /, //, %
>, >=, <, <=, ==, !=
not, and, or
f) operators working from right to left
**, =, +=, -=, /=, //=, %=, **=
4. literals : are the constants
a) int - integers
... -4, -3, -2, -1, 0, 1, 2, 3, ...
10 is int
b) float - floating point value
real number
... -2.5, -1.2, -0.7, 0, 0.5, 1.7, ...
10.0 is float
c) complex
2+3j, 1.5+2.7j
d) bool is boolean type
True, False
e) str is string
'FAIPS', "DPS", '123', "3.4", '*****', "###"
'Pin Code-10078'
5. delimiter:
a) assignments: = +=, -=, *=, /=, //=, %=, **=
b) groupings: [], (), {}
c) punctuators: , ; : . @ !
augmented assignment
a+=b a=a+b
a-=b a=a-b
a*=b a=a*b
a/=b a=a/b
a//=b a=a//b
a%=b a=a%b
a**=b a=a**b
print()
input()
type()
eval()
ord() - returns the ASCII Code or the Unicode of a single character string
char code
'A' 65
'B' 66
'C' 67
:
'Z' 90
'a' 97
'b' 98
'c' 99
:
'z' 122
'0' 48
'1' 49
'2' 50
:
'9' 57
chr() - returns single character string, given ASACII code / Unicode
abs() - returns the absolute value of a number
int() - returns an integer for given string
float() - returns a float for given string
str() - return a string for a given number
eval() - returns a value for a given expression stored as a string
len() - returns number of characters present in a string
round() - returns a number rounded