python programming unit 2
python programming unit 2
SYEDA HUSNA(BE,Mtech)
UNIT 2
Input and output, error and types, Value and variable data type, string, keywords, identifiers,
statement and expression, comment, constant, tuple, array, operators, function (types of
function), import modules, import files.
Input is data entered by user (end user) in the program. In python, input () function is available
for input.
Syntax for input () is: variable = input (“data”)
x=input ("enter the name:")
y=int (input ("enter the number")) enter the number 3 >>>
OUTPUT: Output can be displayed to the user using Print statement
Syntax Errors
Syntax errors occur when the code violates the programming language's rules.
Examples:
1. Missing or mismatched brackets: print("Hello"
2. Incorrect indentation: if True: print("Hello") (should be indented)
3. Typo in keywords: prnt("Hello") (should be print)
Types of Errors in Python
1. SyntaxError: Errors in code syntax.
2. TypeError: Errors when using incompatible data types. Example: 1 + "hello"
3. ValueError: Errors when a function or operation receives an argument with an incorrect value.
Example: int("hello")
4. NameError: Errors when using an undefined variable. Example: print(x) (if x is not defined)
5. RuntimeError: Errors that occur during code execution.
Eg, Values are 2, 42.0, and 'Hello, World!'. (These values belong to different datatypes.)
Data type:
1. Every value in Python has a data type.
2. It is a set of values, and the allowable operations on those values.
Data Types in Python:
Every value in Python has a datatype.
Since everything is an object in Python programming, data types are actually classes and
variables are instance (object) of these classes.
There are various data types in Python.
Some of the important types are listed below.
1. Numbers
2. Strings
3. List
4. Tuples
5. Sets
6. Dictionary
NUMBERS
Python has three built-in numeric data types: integers, floating-point numbers, and complex
numbers.
Float: In Python, floating point numbers (float) are positive and negative real numbers with a
fractional part denoted by the decimal symbol.
or the scientific notation E or e, e.g. 1234.56, 3.142, -1.55, 0.23, 1.432e2,1e-4
Sequence:
LISTS
List is an ordered sequence of items.
Values in the list are called elements /items.
It can be written as a list of comma-separated items (values) between square brackets[].
Items in the lists can be of different datatypes.
Example:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
mylist = ["apple", "banana", "cherry"]
TUPLES
A tuple is a collection of objects that are ordered and immutable. Tuples are defined using
parentheses () and elements are separated by commas.
my_tuple = (1, 2, 3, "hello")
print(my_tuple[0]) # Output: 1
Tuples are another container type extremely similar in nature to lists.
The only visible difference between tuples and lists is that tuples use parentheses and
lists use square brackets.
Functionally. there is a more significant difference, and that is the fact that tuples are
immutable.
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other
3 are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable.
Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
1. Ordered: When we say that tuples are ordered, it means that the items have a defined order,
and that order will not change.
2. Unchangeable: Tuples are unchangeable, meaning that we cannot change, add or remove
items after the tuple has been created.
3. Allow Duplicates: Since tuple are indexed, tuples can have items with the same value
Expressions:
-An expression is a combination of values, variables, and operators.
- A value all by itself is considered an expression, and also a variable.
COMMENT
A hash sign # is the beginning of a comment anything return after # in a line ignored by
interpreter
Ex: #calculate percentage of a hour
Percentage=(minute*100)/60
CONSTANT
A constant is a type of variable whose value cannot be changed.
Ex: pi=3.14
Gravity = 9.8
. OPERATORS:
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator
Types of Operators
Python language supports the following types of operators
a. Arithmetic Operators
b. Comparison (Relational)Operators
c. Assignment Operators
d. Logical Operators
e. Bitwise Operators
f. Membership Operators
g. Identity Operators
FUNCTIONS: Function is a sub program which consists of set of instructions used to perform
a specific task. A large program is divided into basic building blocks called function.
Need for Function:
When the program is too complex and large they are divided into parts. Each
part is separately coded and combined into single program. Each subprogram is
called as function.
Debugging, Testing and maintenance becomes easy when the program is
divided into subprograms.
Functions are used to avoid rewriting same code again and again in a program.
Function provides code re-usability
The length of the program is reduced
Types of function: Functions can be classified into two categories:
i) user defined function
ii) Built in function
Arrays
Arrays are an ordered collection of elements of the same data type. Python arrays can only hold
a sequence of multiple items that are of the same type.
Difference between Python Lists and Python Arrays Lists and arrays behave similarly.
Just like arrays, lists are an ordered sequence of elements.
They are also mutable and not fixed in size, which means they can grow and shrink throughout
the life of the program.
Items can be added and removed, making them very flexible to work with.
However, lists and arrays are not the same thing.
Lists store items that are of various data types. This means that a list can contain integers,
floating point numbers, strings, or any other Python data type, at the same time. That is not the
case with arrays.
Arrays store only items that are of the same single data type. There are arrays that contain only
integers, or only floating point numbers, or only any other Python data type you want to use.
Advantages of Python Arrays
Lists are built into the Python programming language, whereas arrays aren't. Arrays are not a
built-in data structure, and therefore need to be imported via the array module in order to be
used. Arrays of the array module are are useful when you want to work with homogeneous
data. They are also more compact and take up less memory and space which makes them more
size efficient compared to lists.
MODULES IN PYTHON
A python module is a file that consists of python definition and statements. A module can
define functions, classes and variables.
It allows us to logically arrange related code and makes the code easier to understand and
use.
Syntax: import
module_namemodule_name.function_name(variable)
1.Import statement:
• An import statement is used to import python module in some python source file.
Syntax: import module1 [, module2 […module]]
Example:
>>>import math
>>>print (math.pi)
3.14159265