Python Programming
Python Programming
UNIT I
THE WAY OF THE PROGRAM
USED:
OUTPUT:
Display data on the screen, save it in a file, send it over the
network, etc.
MATH:
Perform basic mathematical operations like addition and
multiplication.
DEBUGGING
CONDITIONAL EXECUTION:
Check for certain conditions and run the appropriate code.
REPETITION:
Perform some action repeatedly, usually with some variation.
Contd..
1. Print Statements:
Adding print statements at strategic points in the code can
help the user to understand the flow of execution and the
values of variables at different stages.
2. Using `pdb` (Python Debugger):
The `pdb` module provides a built-in debugger for Python.
The user can insert breakpoints in code and run it in
interactive mode, allowing to inspect variables, step through
code, and identify issues.
3. IDEs with Debugging Support:
Integrated Development Environments (IDEs) like PyCharm,
Visual Studio Code, and others offer debugging tools
integrated into the development environment.
These tools provide features like breakpoints, variable
inspection, stepping through code, and more
Contd..
4. Syntax Errors:
Python raises syntax errors when there's a mistake in the
syntax of code.
The user needs to read the error message carefully to identify
the location of the error and fix it.
5. Logical Errors:
These are errors in the logic of code, where it produces
unexpected results.
Use print statements and debugging tools to trace the flow of
execution and identify where the logic deviates from what you
intended.
Contd..
6. Tracebacks:
When an exception occurs, Python generates a traceback that
shows the sequence of function calls leading to the error.
Read the traceback carefully to identify the source of the
error.
7. Unit Testing:
Writing unit tests for code can help identify and fix
errors more efficiently.
Unit tests provide automated checks to verify that
individual components of code behave as expected.
When debugging, it's essential to approach the problem
systematically, starting with understanding the error,
identifying its source, and then applying the appropriate
debugging technique to fix it.
FORMAL AND NATURAL LANGUAGE
NATURAL LANGUAGE:
Natural languages are the languages that people speak,
such as English, Spanish, and French. They were not
designed by people (although people try to impose some
order on them); they evolved naturally
FORMAL LANGUAGE:
Formal languages are languages that are designed by
people for specific applications.
For example, the notation that mathematicians use is a
formal language that is particularly good at denoting
relationships among numbers and symbols.
Chemists use a formal language to represent the chemical
structure of molecules.
Programming languages are formal languages that
have been designed to express computations.
Contd..
Example :
>>> print("Welcome to Nehru Arts and Science College!")
Welcome to Nehru Arts and Science College!
VARIABLES
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
Output:
45
1456.8
John
Contd..
Declaration and Initialization of Variables:
# display
print( Number)
Output:
100
Contd..
Redeclaring variables in Python:
a = b = c = 10
print(a)
print(b)
print(c)
Output:
10
10
10
Contd..
Assigning different values to multiple variables :
a, b, c = 1, 20.2, “NASC"
print(a)
print(b)
print(c)
Output:
1
20.2
NASC
Contd..
Can We Use the Same Name for Different Types? :
a = 10
a = “NASC"
print(a)
Output:
NASC
Contd..
How does + operator work with variables? :
Local variables in Python are the ones that are defined and
declared inside a function.
Global variables in Python are the ones that are defined and
declared outside a function
# Global scope
s = "I love India"
f()
Output:
I love India
EXPRESSIONS
An expression is a combination of operators and operands that is
interpreted to produce some other value.
In any programming language, an expression is evaluated as per
the precedence of its operators.
If there is more than one operator in an expression, their
precedence decides which operation will be performed first.
There are eight types of expressions. They are
1. Constant Expressions
2. Arithmetic Expressions
3. Integral Expressions
4. Floating Expressions
5. Relational Expressions
6. Logical Expressions
7. Bitwise Expressions
8. Combinational Expressions
Contd..
1. Constant Expressions:
These are the expressions that have constant values only.
# Constant Expressions
x = 15 + 1.3
print(x)
Output
16.3
Contd..
2. Arithmetic Expressions:
An arithmetic expression is a combination of numeric
values, operators, and sometimes parenthesis.
The result of this type of expression is also a numeric value.
The operators used in these expressions are arithmetic
operators like addition, subtraction, etc.
Here are some arithmetic operators in Python:
Contd..
Operators Syntax Functioning
+ x+y Addition
– x–y Subtraction
* x*y Multiplication
/ x/y Division
// x // y Quotient
% x%y Remainder
** x ** y Exponentiation
Contd..
# Arithmetic Expressions
x = 40
y = 12
add = x + y
sub = x - y Output
pro = x * y 52
div = x / y 28
print(add) 480
print(sub) 3.3333333333333335
print(pro)
print(div)
Contd..
3. Integral Expressions:
These are the kind of expressions that produce only integer results
after all computations and type conversions.
# Integral Expressions
a = 13
Output
b = 12.0
25
c = a + int(b)
print(c)
Contd..
4. Floating Expressions:
These are the kind of expressions which produce floating point
numbers as result after all computations and type conversions.
# Floating Expressions
a = 13
b=5 Output
2.6
c=a/b
print(c)
Contd..
5. Relational Expressions:
p = (a + b) >= (c - d)
print(p)
Output
True
Contd..
6. Logical Expressions:
It returns true if
both P and Q are
and P and Q
true otherwise
returns false
It returns true if at
or P or Q least one of P and Q
is true
It returns true if
not not P
condition P is false
Contd..
P = (10 == 9)
Q = (7 > 5)
# Logical Expressions
R = P and Q
S = P or Q Output
T = not P False
True
print(R) True
print(S)
print(T)
Contd..
7. Bitwise Expressions:
# Bitwise Expressions
a = 12
x = a >> 2 Output
y = a << 1 3 24
print(x, y)
Contd..
8. Combinational Expressions:
# Combinational Expressions
a = 16
b = 12
Output
c = a + (b >> 1) 22
print(c)
Contd..
OPERATOR PRECEDENCE
If there is more than one operator in an expression, it may give
different results on basis of the order of operators executed.
To sort out these confusions, the operator precedence is
defined.
Operator Precedence simply defines the priority of operators
that which operator is to be executed first.
Operator precedence or priority is given below
Contd..
Precedence Name Operator
1 Parenthesis ()[]{}
2 Exponentiation **
Unary plus or minus,
3 -a , +a , ~a
complement
# Multi-operator expression
a = 10 + 3 * 4
Output
print(a)
22
b = (10 + 3) * 4
52
print(b)
22
c = 10 + (3 * 4)
print(c)
STATEMENTS
a = 16
Print(a) Output
16
Contd..
Conditional Statement:
x=3
Output
if x < 5:
X is less than 5
print("x is less than 5")
else:
print("x is greater than or equal to 5")
Contd..
Statement Set:
Statement Description
Statements spanning multiple
Multi-Line Statements lines using line continuation
or braces.
Statements that contain other
Compound Statements
statements (e.g., if, while, for).
Basic standalone statements
Simple Statements
that perform a single action.
Statements that evaluate and
Expression Statements
produce a value.
A placeholder statement that
pass Statement
does nothing.
Contd..
Statement Description
Used to delete references to
del Statement
objects.
Terminates a function and
return Statement
returns a value (optional).
Imports modules or specific
import Statement
objects from modules.
Control flow statements used
in loops (continue skips to the
continue and break Statements
next iteration, break exits the
loop).
Contd..
Multi-Line Statements:
total = 10 +
\ 20 +
\ 30 print(total) ### Output
Contd..
2.Using parentheses:
fruit_list = ('Apple’,
'Mango’,
‘Banana’,
'Orange’)
print(fruit_list) ### Output
Contd..
Simple Statements:
Try Statement:
The try statement in Python is used to catch exceptions that may
occur during the execution of a block of code.
It ensures that even when an error occurs, the code does not stop
running.
VALUES AND TYPES
print(17)
print('Hello World!')
OUTPUT:
17
Hello World!
Contd..
By using the type function the user can find out the type of the value.
print(type('Hello, World!'))
print(type(17))
print(type(3.2))
OUTPUT:
<class 'str'>
<class 'int'>
<class 'float'>
KEYWORDS
Keywords Description
This is a logical operator which
and returns true if both the operands
are true else returns false.
# code
import keyword
print(keyword.kwlist)
Output