python_notes_DS_IanDomel
python_notes_DS_IanDomel
Writing Pane on
Python Codes
Output/Result or
Comments/Error Pane
Python is an object oriented so its data types are classes. In Python, the data type is
set when you assign a value to a variable. Some of the commonly used data types in
Python are:
1. Integer (int): It can store positive or negative whole numbers (without fraction or
decimal). In Python there is no limit to how long an integer value can be. For example:
counter=10000
print(counter)
goals=3
print(goals)
2. Float (float): It can store realnumbers. It is specified by a decimal point. For example:
cgpa=2.95
print(cgpa)
z = 2 + 4j
print(z)
str1="welcome to python"
print(str1)
str2='''welcome to
Python'''
print(str2)
Standard/Common Python Built-In Data Types:
Figure 1.2 – Using Input, Process, Output in Python Programming Data Types
2.
Figure 1.3 – Using str data type for Output and New line.
Figure 1.3 – Using Conditional Operator in Python Programming
Functions in Python
A function in Python is a set of related statements that carry out a particular action.
In the program it is broken up into smaller, modular chunks thanks to functions. Functions
help the program stay organized and manageable as it gets bigger and bigger.
Additionally, it keeps the code from being repeated and makes it usable.
1. The program can be divided into small parts that are easy to understand, debug
and modify.
2. Repetition of code can be avoided. A function can be written once and called
many times.
3. Functions are reusable. A function can be used in many programs.
Types of Functions:
In python language functions are of two types:
1. Built-in (Library) functions.
2. User Defined Functions
In python there are large number of built-in functions that can be used to
perform different operations like math operations and operations on strings, lists
and files.
In this chapter a number of built-in math library functions will be introduced.
In Python programs a number of mathematical operations can be
performed with ease by importing a module named “math” which defines various
functions which makes our programming tasks easier such as sqrt(x). Below are
some of these built-in math functions.
Function Description
Example 1:
import math This is the library to be included math functions
print("3 power 2 is=", pow(3,2))
print("Square root of 5 is=", math.sqrt(5))
x=min(10,23,2,8)
print("Minimum is=",x)
y=max(10,23,2,8)
print("Maximum is=",y)
z = abs(-7.25)
print("Absolute value is=",z)
Output:
3 power 2 is= 9.0
Square root of 5 is= 2.23606797749979
Minimum is= 2
Maximum is= 23
Absolute value is= 7.25
These functions are written by programmers for their own usage in their programs.
Writing a user defined function consists of two steps:
1. Defining the function.
2. Calling the function.
Definition of a function must be before using (calling) the function and it consists of the
following components:
1.Keyword def that marks the start of the function header.
2.A function_name to uniquely identify the function. Function name is given by the
programmer and the naming follows the same rules of writing identifiers in Python.
3.Parameters (arguments) through which we pass values to a function. They are
optional.
4.A colon (:) to mark the end of the function header.
5.One or more valid python statements that make up the function body. Statements must
have the same indentation level (usually 4 spaces).
6.An optional return statement to return a value from the function.
The statement return [expression] exits a function and it optionally passing back the
value of an expression to the caller. A return statement with no arguments is the same as
return none.
Syntax of Calling a Function: