Professional Documents
Culture Documents
Python programming
fundamentals
Introduction
A Python program is called a script.
In python programming,
❖ data types are inbuilt hence support “dynamic
typing”
❖ declaration of variables is not required.
❖ memory management is automatically done.
Variables
A variable is like a container that stores values that you
can access or change.
a) Identity of variable
b) Type of variable
c) Value of variable
Variables
a) Identity of the variable
Value_1 = 100
L-value = R-value
Value
Multiple assignments:
e.g.
>>>x, y, z, p = 2, 40, 30.5, ‘Vinay’
Variable 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.
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
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
e.g.
print(“I am a student of \n APS \t Yol Cantt”)
4. Operators
Operators can be defined as symbols that are used to
perform operations on operands.
Types of Operators
a. Arithmetic Operators.
b. Relational Operators.
c. Assignment Operators.
d. Logical Operators.
e. Membership Operators
f. Identity Operators
4. 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
4. 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
4. 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
c=19 9.5
c/=2 16
print(c)
d=2
d**=4
print(d)
4. 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):
Output :- print("hello")
hello
4. 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
4. 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
Comments
Comments are statements in the script that are
ignored by the Python interpreter.
Syntax:
def function_name(comma_sep_list_parameters):
statements
User Defined Functions
Example 1:
def display():
print(“Welcome to pyhton”)
>>>display()
Example 2:
def arearec(len, wd):
area=len*wd
return area
>>>arearec(30, 10)