Unit 8 – Python Programming
8.2 Variables and Functions
1
What is a Variable ?
Variables are nothing but reserved memory locations to store values.
Based on the data type of a variable, the python interpreter allocates
memory and decides what can be stored in the reserved memory.
count = 500
marks = 85.5
name = " Peter "
2
Assigning Values to Variables
This is how we create a variable called marks, which contains an integer
value of 78.
marks = 78 Answer for print(marks) is 78
No need to specify the type of a variable when declaring one.
3
Naming Rules in Python Variables
Python variables can only begin with a letter(A-Z/a-z) or an underscore(_).
A variable name cannot contain spaces.
name = "Amal " , Age = 25 , _marks = 50.8 # valid syntax
7abc =25 , $name = " abc ", mark s = 70 # invalid syntax
4
Naming Rules in Python Variables
The rest of the identifier may contain letters(A-Z/a-z), underscores(_), and
numbers(0-9).
Name1 = " John " # valid syntax
Name@ = " Peter " # invalid syntax
python is case-sensitive, and so are Python identifiers.
Here abc and Abc are two abc = 50
different variables Abc = 60
5
Variables –Exercises
Use python IDLE to answer all the questions.
f = 3.56 # a floating point number
a = "Python" # a string
b= "Programming" # a string
1) Display the output of all variables using print() command
2) combination = a + " " + b
3) Display the output of combination using print() command
4) sum = f + f
5) Display the output of sum using print() command
6
What is a function?
Function is a sequence of statements in a certain
order, given a name.
When called, those statements are executed.
It provides code re-usability.
7
Function Types
There are mainly four types of python functions.
1. User Defined Functions
2. Built –in Functions
3. Lamda Functions
4. Recursion Functions
Let’s talk about Built-in and User Defined Functions.
8
Built- in Functions
The Python interpreter has a number of functions and types built into it that
are always available.
Let’s talk about
input() and print()
functions
9
Print() Function
The print() function prints the specified message to the screen,
or other standard output device.
print("hello !!!") – gives the output of hello !!!
Output
print(1500) 1500
print(3.5) 3.5
print("python programming") python programming
print("Next function, please!") Next function, please!
10
Input() Function
The input() function allows user input and reads one line from standard
input and returns it as a string.
a = input("Enter your name: ")
print(“Your name is: ", a)
This would prompt you to enter any string and it would display same string
on the screen.
Output
Enter your name: Kamal Can enter any string
Your name is: Kamal value
11
User Defined Functions
You can define functions to provide the required functionality.
def abc():
"This prints hello and world string values" The first statement of a
function can be an optional
print("Hello") statement
print("World")
return; A Python function may
optionally return a value
Once you call the function you get the output results.
abc() Hello
World
12
Python Function with Parameters
Parameters(Arguments) are specified after the function name,
inside the parentheses “( )”.
Function with one argument (abc)
# Function definition is here
def printme(abc): Output
"This prints a string value"
print(abc) hello world
return; second call to the same function
# Now you can call printme function
printme("hello world")
printme("second call to the same
function")
13
Functions - Exercise
Create two different functions for addition, Subtraction with two parameters
for each function and call them to print the calculations.
def addition(num1,num2): Output
print("answer is: " ,num1+num2)
def subtraction(num1,num2): answer is: 70
print("answer is: " ,num1-num2) answer is: 90
answer is: 120
addition(50,20) answer is: 30
addition(40,50) answer is: 80
addition(100,20) answer is: 100
subtraction(50,20)
subtraction(100,20)
subtraction(200,100)
14
Lesson Summary
Variable – Definition
Assigning Values to Variables
Function – Definition
Input/Output Function
User Defined Functions
15
Thank You.
16