1.5 Python Built-In Functions
1.5 Python Built-In Functions
3
Assigning Values to Variables
• Python variables do not have to be explicitly declared to
reserve memory space. The declaration happens
automatically when you assign a value to a variable. The
equal sign (=) is used to assign values to variables.
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
4
Multiple Assignment
• You can also assign a single value to several
variables simultaneously. For example:
a = b = c = 1
a, b, c = 1, 2, "john"
5
Python functions (built-in)
• A function is a block of organized, reusable
code that is used to perform a single, related
action.
• In Python a function is some reusable code
that takes arguments(s) as input does some
computation and then returns a result or
results
• Functions provides better modularity for
your application and a high degree of code
reusing.
6
Python functions (built-in)
• Python provides many built-in functions. that you can call in
your programs. When you call a function, you can include any
arguments that are used by the function.
• Some of popular Python built-in functions:
– print()
– input()
– int()
– float()
– round()
– min()
– max()
– abs()
– sqrt()
– eval()
7
Buit-in-Functions
• Python provides many built-in functions for numbers,
including:
• Mathematical functions: round(), pow(), abs(), etc.
• type() to get the type.
• Type conversion functions: int(), float(), str(), bool(),
etc.
• Base radix conversion functions: hex(), bin(), oct().
8
Built-in Functions and Operators for Strings
• You can operate on strings using:
• built-in functions such as len();
• Operators such as in
(contains), + (concatenation), * (repetition),
indexing [i] and [-i], and slicing [m:n:step].
9
Function / Examples
Usage Description
Operator s = 'Hello'
len() len(str) Length len(s) ⇒ 5
in substr in str Contain? 'ell' in s ⇒ True
Return bool of 'he' in s ⇒ False
either True or False
O/P
Enter your first name: Mike
Hello, Mike!
14
input()
• Another way to get input from the user
print ("What is your first name? ")
first_name = input ()
print ("Hello, " + first_name + "!")
O/P
What is your first name?
Mike
Hello, Mike!
15
int(), float
• The int() and float() functions convert the data
argument, which is typically a str value, to int or float
values.
• Syntax: int (data)
– Converts the data argument to the int type and returns
the int value.
• Syntax: float (data)
– Converts the data argument to the float type and
returns the float value.
16
int(), float - Example
• Examples:
quantity = input("Enter the quantity: ")
quantity = int(quantity)
17
round()
• The round() function rounds a numeric value to the
specified number of digits.
• Syntax: round (number [, digits] )
– Rounds the number argument to the number of
decimal digits in the digits argument. If no digits are
specified, it rounds the number to the nearest integer.
miles_driven = 150
gallons_used = 5.875
mpg = miles_driven / gallons_used
mpg = round (mpg, 2)
18
min(), max()
• The max() function allows you to find the highest
number in given range.
• The min() function does the opposite, providing you
with the lowest number in a defined range.
19
lower (), upper ()
• lower () Converts uppercase letters to lowercase
without changing the string itself.
• upper () Converts lowercase letters to uppercase
without changing the string itself.
20
abs()
• The abs() method in Python can be used to convert a
floating-point number or an integer into an absolute
value.
• This is especially useful when you want to know the
difference between two values.
21
sqrt()
• sqrt() function is an inbuilt function in Python
programming language that returns the square root of
any number.
• It returns the square root of the number passed in the
parameter.
22
eval()
• Python eval() function is used to parse an expression
string as python expression and then execute it.
• Example:
• eval(“1+2”) returns 3.
23