Chapter - 3 Python Programming Fundamentals

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

Chapter - 3

Python programming
fundamentals
Introduction
A Python program is called a script.

Script is a sequence of definitions and commands.

These commands are executed by Python interpreter known as


PYTHON SHELL.

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 variable has three main components:

a)Identity of variable
b)Type of variable
c) Value of variable
Variables
a) Identity of the variable

It refers to object’s address in the memory.


>>> x=10
e.g. >>> id (x)
Data Type
b) Type of the variable

Type means data type of a variable.

1. Number: It stores numerical values.

Python supports three built in numeric types –


integer,
floating point numbers and
complex numbers.
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
2. String (str): It is a sequence of characters.
(combination of letters, numbers and symbols).
It is enclosed within single or double quotes.
e.g. (‘ ’ or “ ”)

>>> rem= “Hello Welcome to Python”


>>> print (rem)
Hello Welcome to Python
Data Type
3. Boolean (bool): It represents one of the two
possible values – True or False.

>>> bool_1 = (6>10)


>>> print (bool_1)
False
>>> bool_2 = (6<10)
>>> print (bool_2)
True
Data Type
4. None: It is a special data type with single
value.
It is used to signify absence of value evaluating
to false in a situation.

>>> value_1 = None


>>> print (value_1)
None
Data Type
type() – if you wish to determine type of the
variable.
e.g.
>>> type(10)
<class ‘int’>
>>> type(8.2)
<class ‘float’>
>>> type(“hello”)
<class ‘str’>
>>> type(True)
<class ‘bool’>
Value
c) Value of variable

Values are assigned to a variable using


assignment operator (=)..

e.g. marks – Name of the variable


>>>marks=87
>>>print(marks)
87 87 Value of the variable
Value
The concept of assignment:

Value_1 = 100

Variable Assignment Value


operator

There should be only one variable on the left-hand side of assignment operator.
This variable is called Left value or L-value

There can be any valid expression on the right-hand side of assignment


operator.
This variable is called Right value or R-value

L-value = R-value
Value
Multiple assignments:

We can declare multiple variables in a single


statement.

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.

2. A variable name must start with an alphabet or and underscore (_).

3. A variable name cannot contain spaces.

4. Keyword cannot be used as a variable name.

5. Variable names are case sensitive. Num and num are different.

6. Variable names should be short and meaningful.

Invalid variable names – 3dgraph, roll#no, first name, d.o.b, while


Input/Output
Python provides three functions for getting user’s
input.

1. input() function– It is used to get data in script


mode.
The input() function takes string as an argument.
It always returns a value of string type.
Input/Output
2. int() function– It is used to convert input string value to
numeric value.

3. float() function – It converts fetched value in float type.

4. eval() – This function is used to evaluate the value of a string.


It takes input as string and evaluates this string as number and
return numeric result.

NOTE : input() function always enter string value in python 3.


So on need int(), float() function can be used for data
conversion.
Sample Program

n1 = input("Enter first number")


n2 = input("Enter second number")
Sum=n1+n2
print("Sum is:", Sum)
Sample Program
n1 = eval(input("Enter first number"))
n2 = eval(input("Enter second number"))
Sum=n1+n2
print("Sum is:", Sum)

n1 = int(input("Enter first number"))


n2 = int(input("Enter second number"))
Sum=n1+n2
print("Sum is:", Sum)
Sample Program
n1 = eval(input("Enter first number"))
n2 = eval(input("Enter second number"))
Sum=n1+n2
print("Sum is:", Sum)

n1 = int(input("Enter first number"))


n2 = int(input("Enter second number"))
Sum=n1+n2
print("Sum is:", Sum)
To calculate simple interest

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

m1 = int(input("Enter marks in English:"))


m2 = int(input("Enter marks in Hindi:"))
m3 = int(input("Enter marks in Maths:"))
m4 = int(input("Enter marks in Science:"))
m5 = int(input("Enter marks in Social Science:"))
total=m1+m2+m3+m4+m5
per = total/5
print("Total is:", total)
print("Percenatge is:", per)
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
Special symbols :
Special symbol
available over
keyboard
White spaces: blank space, tab, carriage return, new
line, form feed
Tokens

Smallest individual unit in a program is


known as token.

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.
3. Literals
Literals in Python can be defined as number, text, or
other data that represent values to be stored in
variables.
Example of String Literals in Python
name = ‘Johni’ , fname=“johny”

Example of Integer Literals in Python(numeric literal)


age = 22

Example of Float Literals in Python(numeric literal)


height = 6.2

Example of Special Literals in Python


name = None
3. Literals
Escape sequence

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
9.5
c=19
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.

Comments (hash symbol = #) makes code more


readable and understandable.

# Program to assign value


x=10
x=x+100 # increase value of x by 100
print(x)
Expressions
Expressions are combination of value(s). i.e. constant,
variable and operators.
Expression Value
5+2*4 13
8+12*2-4 28

Converting mathematical expression to equivalent


Python expression
Algebraic Expression Python Expression
y y=3*x/2
z= 3bc + 4 z = 3*b*c + 4
User Defined Functions
A function is a group of statements that exists within a
program for the purpose of performing a specific task.

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy