Chapter 1 introduction to Python
Chapter 1 introduction to Python
with Python
Introduction to Python
An ordered set of instructions or commands to be
executed by a computer is called a program.
In python programming,
data types are inbuilt henc e support
“dynamic typing”
dec laration of variables is not required.
memory management is automatic ally
done.
Features of Python
Note: while installing python one thing keep in mind i.e. tick the button Add
python 3.10 to path so that your python address will added implicitly to
environment variable. And continue to install now if you got a prompt window to
ask to run the just simply click yes .
Two modes to interact with Python
(Python Shell)
(Python Editor)
Execution Modes:
1) Interactive Mode :
we can type a Python statement on the
>>> prompt directly. As soon as we
press enter, the interpreter executes the
statement and displays the result(s)
Working in the interactive mode is convenient
for testing a single line code for instant
execution. But in the interactive mode, we
cannot save the statements for future use and
we have to retype the statements to run them
again.
2) Script Mode:
In the script mode, we can write a Python program in a
file, save it and then use the interpreter to execute the
program from the file. Such program files have a .py
extension and they are also known as scripts. But for
programs having more than a few lines, we should
always save the code in files for future use. Python
scripts can be created using any editor. Python has a
built-in editor called IDLE (Integrated Development and Learning
Environment )which can be used to create programs. After
opening the IDLE, we can click File>New File to create a
new file, then write our program on that file and save it
with a desired name. By default, the Python scripts are
saved in the Python installation folder.
Python Character Set
A set of valid characters recognized by python.
• Python uses the traditional ASCII character set.
• The latest version recognizes the Unicode character set.
The ASCII character set is a subset of the Unicode
character set.
Letters: a-z, A-Z
Digits : 0-9
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Delimiters
1. Keywords
Reserved word of the compiler/interpreter which can’t
be used as identifier.
2. Identifiers
A Python identifier is a name used
to identify a variable, function,
class, module or other object.
Identifier Naming Convention
1. A variable name can contain letter, digits and underscore (_). No
other characters are allowed.
5. Variable names are case sensitive. Num and num are different.
Types of Operators
a. Arithmetic Operators.
b. Relational Operators.
c. Assignment Operators.
d. Logical Operators.
e. Membership Operators
f. Identity Operators
Operators
a. Arithmetic Operators.
Arithmetic Operators are used to perform arithmetic
operations like addition, multiplication, division etc.
print ("hello"+"python")
print(2+3)
print(10-3)
print(22%5)
print(19//5)
print(2**3)
Output:
hellopython
5
7
2
3
8
Operators
b. Relational Operators.
Relational Operators are used to compare the values.
print(5==3) False
print(7>3) True
print(15<7) False
print(3!=2) True
print(7>=8) False
print(3<=4) True
Operators
c. Assignment Operators.
Used to assign values to the variables.
a=10
print(a)
a+=9
print(a)
Output:
b=11 10
b*=3 19
print(b) 33
9.5
c=19
c/=2 16
print(c)
d=2
d**=4
print(d)
Operators
d. Logical Operators.
Logical Operators are used to perform logical operations
on the given two variables or values.
a=30 a=70
b=20 b=20
if(a==30 and b==20):
print("hello")
if(a==30 or b==20):
print("hello")
Output :-
hello
Operators
e. Membership Operators
It used to validate whether a value is found within a
sequence such as such as strings, lists, or tuples.
E.g. E.g.
a = 22 a = 22
list = [11, 22,33,44] list = [11, 22,33,44]
ans= a in list ans= a not in list
print(ans) print(ans)
Output: True Output: False
Operators
f. Identity Operators
Identity operators in Python compare the memory
locations of two objects.
5. Delimiters
These are the symbols which can be used as
separator of values or to enclose some values.
e.g ( ) {} [ ] , ; :
Token Category
X Variable
y Variable
z Variable
print Keyword
() Delimiter
/ Operator
68 Literal
“x, y, z” Literal
Comment
• Comments are used to add a remark or a note in
the source code. Comments are not executed by
interpreter. They are added with the purpose of
making the source code easier for humans to
understand. They are used primarily to document
the meaning and purpose of source code.
• In Python, a single line comment starts with # (hash
sign). Everything following the # till the end of that
line is treated as a comment and the interpreter
simply ignores it while executing the statement.
• Multiline comment can be given using
‘'‘ Docstring‘‘‘
Example :- ‗„ this is program for
find maximum of 2 numbers‘‘‘
Expressions
Expressions are c ombination of value(s). i.e.
constant, variable and operators.
Expression Value
5+2*4 13
8+12*2-4 28
Data types
Floating
Integer Complex Strings Dictionaries
Point
Boolean Lists
Tuples
Data Type
a. int (integer): Integer represents whole
numbers. (positive or negative)
e.g. -6, 0, 23466
b. float (floating point numbers): It
represents numbers with decimal
point.
e.g. -43.2, 6.0
c. Complex numbers: It is made up of pair
of real and imaginary number.
e.g. 2+5j
Data Type
Boolean (bool): It represents one of the
two possible values – True or False.
Example:
>>> s = set ([1,2,34])
>>> s
output: {1, 2, 34}
None: It is a special data type with single
value.
It is used to signify absenc e of value
evaluating to false in a situation.
(A) Dictionary
• Dictionary in Python holds data items in key-value
pairs and Items are enclosed in curly brackets {}.
dictionaries permit faster access to data. Every key
is separated from its value using a colon (:) sign. The
key value pairs of a dictionary can be accessed
using the key. Keys are usually of string type and
their values can be of any data type. In order to
access any value in the dictionary, we have to
specify its key in square brackets [ ].
Example of Dictionary
#create a dictionary
>>>dict1 ={'Fruit':'Apple', 'Climate':'Cold',
'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
#getting value by specifying a key
>>> print(dict1['Price(kg)'])
120
Mutable and Immutable Variables
1. List
2. Set
3. Dictionary
An immutable variable change of value
will not happen in place. Modifying an
immutable variable will rebuild the
same variable.
1. int
2. float
3. decimal
4. complex
5. bool
6. string
7. tuple
Precedence of Operators
Input/Output
Python provides three functions for getting
user's input.
p = int(input("Enter Principal:"))
r = float(input("Enter Rate:"))
t = int(input("Enter Time:"))
si=0.0
si=(p*r*t)/100
print("Simple Interest is:", si)
To calculate total and percentage