unit-1 python
unit-1 python
COURSE OBJECTIVES
To learn about data structures lists, tuples, and dictionaries in Python.
To build packages with Python modules for reusability and handle user/custom exceptions.
To create real world GUI applications, establish Database connectivity and Networking
UNIT 1 INTRODUCTION TO PYTHON 12 Hrs.
History of Python- Introduction to the IDLE interpreter (shell) - Data Types - Built-in function – Conditional
statements - Iterative statements- Input/output functions - Python Database Communication - data analysis
and visualization using python.
Practical:
Implement built-in functions and trace the type of data items.
Implement concepts of Conditional and Iterative Statements.
Use the built-in csv module to read and write from a CSV file in Python.
Perform data analysis and visualization on a given dataset using Python libraries like pandas, numpy,
matplotlib and display charts, graphs, and plots.
In one line only a single executable statement should be written and the line change act
as command terminator in python.
To write two separate executable statements in a single line, you should use
a semicolon ;
to separate the commands.
For example,
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
>>> a = 10
>>> b = 20
>>> a + b
30
• Variables can hold values, and every value has a data-type. Python is a
dynamically typed language; hence we do not need to define the type of the
variable while declaring it. The interpreter implicitly binds the value with its
type.
a=5
• The variable a holds integer value five and we did not define its type. Python
interpreter will automatically interpret variables a as an integer type.
• Python enables us to check the type of the variable used in the program.
Python provides us the type() function, which returns the type of the
variable passed.
2. Conversion of Float
2 long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
3 float(x)
Converts x to a floating-point number.
4 complex(real [,imag])
Creates a complex number.
5 str(x)
Converts object x to a string representation.
6 repr(x)
Converts object x to an expression string.
7 eval(str)
Evaluates a string and returns an object.
8 tuple(s)
Converts s to a tuple.
12 frozenset(s)
Converts s to a frozen set.
13 chr(x)
Converts an integer to a character.
14 unichr(x)
Converts an integer to a Unicode character.
15 ord(x)
Converts a single character to its integer value.
16 hex(x)
Converts an integer to a hexadecimal string.
17 oct(x)
Converts an integer to an octal string.
K.Abinaya Assistant Professor SBU
Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can
modify after creation), and has unique elements.
In set, the order of the elements is undefined; it may return the changed sequence
of the element. The set is created by using a built-in function set(), or a sequence of
elements is passed in the curly braces and separated by the comma. It can contain
various types of values.
x = 20 int
x = 20.5 float
x = 1j complex
x = range(6) range
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
x = range(6) range
K.Abinaya Assistant Professor SBU
x = dict(name="John", age=36) dict
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
str(x)
Converts x into a string
representation
Check String
Remove Whitespace
-strip() method removes any whitespace from the beginning or the end:
Split String
The split() method returns a list where the text between the specified separator becomes
the list items.
is not Returns True if both variables are not the same x is not y
object
not in Returns True if a sequence with the specified value is not present in x not in y
the object
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true,
then try this condition".
OR
With the while loop we can execute a set of statements as long as a condition is true.
Continue Statement
With the continue statement we can stop the current iteration, and continue with the
next:Continue to the next iteration if i is 3:
Break Statement
• With the break statement we can stop the loop before it has looped through all the
items:
Continue Statement:
With the continue statement we can stop the current iteration of the loop, and continue
with the next:
Break the loop when x is 3, and see what happens with the else block:
Calling a Function
To call a function, use the function name followed by parenthesis:
All the above modules have almost the same syntax and methods to handle the
databases.
If you are using Jupyter Notebook, you can install it within a notebook cell by using:
Visualization Control xlim(), ylim() Sets limits for the X and Y axes.