Module1PPT (1)
Module1PPT (1)
Programming (BPLCK205B)
Dr. Thyagaraju G S
Dr.Thyagaraju GS
Modules
• Module1: Python Basics, Flow control, Functions •
Module2 : Lists, Tuples and Dictionaries • Module3:
Strings, Reading and Writing Files • Module 4:
Organizing Files and Debugging • Module 5: Classes
and Objects, Classes and Methods, Classes and
Functions
Dr.Thyagaraju GS
Module 1:
• Python Basics: Entering Expressions into the Interactive Shell, The Integer,
Floating-Point, and String Data Types, String Concatenation and Replication,
Storing Values in Variables, Your First Program, Dissecting Your Program,
• Flow control: Boolean Values, Comparison Operators, Boolean Operators,
Mixing Boolean and Comparison Operators, Elements of Flow Control, Program
Execution, Flow Control Statements, Importing Modules, Ending a Program Early
with sys.exit(),
• Functions: def Statements with Parameters, Return Values and return
Statements,The None Value, Keyword Arguments and print(), Local and Global
Scope, The global Statement, Exception Handling, A Short Program: Guess the
Number
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Value
•A value is a letter or a number.
•In Python, a value is a fundamental piece of data that
can be assigned to variables, used in expressions, and
manipulated by operations.
• Values can be of different types, such as numbers,
strings, booleans, lists, tuples, dictionaries, and more.
Each type of value has its own characteristics and
behaviors.
Dr.Thyagaraju GS
Examples
•x = 10 # integer
•y = 3.14 # floating-point number
•z = 2 + 3j # complex number
•name = "John" # string
•message = 'Hello, World!' # string
•is_true = True # Boolean Value
Dr.Thyagaraju GS
type() function
•In Python, the type() function is used to determine the
type of a given object or value.
•It returns the data type of the object as a result.
Dr.Thyagaraju GS
D
r.Thyagaraju GS
Dr.
Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagar
aju GS
String Concatenation
Dr.Thyagaraju GS
String Replication
• String replication allows you to repeat a string multiple times. In Python, you can
replicate a string by using the * operator.
• Here's an example:
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Comments
• Comments are readable explanation or descriptions that help
programmers better understand the intent and functionality of the source
code.
• Comments are completely ignored by interpreter. Dr.Thyagaraju GS
Dr.Thyagaraju GS
Single Line Comments in Python:
Dr.Thyagaraju GS
Multiline Comments
1. Using # at the beginning of each line of comment on multiple
lines Example:
# It is a
# multiline
# comment
2. Using String Literals ''' at the beginning and end of multiple lines
Example:
'''
I am a
Multiline comment!
'''Dr.Thyagaraju GS
Dr.Thyagaraju GS
3*(12/3)
Example3 : Invalid Expressions
Dr.Thyagaraju GS
• Digits :0-9
White Spaces : Blank Space, tabs(->), Carriage return , new line , form feed •
Other Characters : All other 256 ACII and Unicode characters
Dr.Thyagaraju GS
Python Tokens:
1.Identifiers
• Identifiers are names that you
give to
a variable , class or Function.
• There are certain rules for naming
identifiers similar to the variable
declaration rules , such as :
• No Special character except_ ,
• Keywords are not used as
identifiers ,
• the first character of an identifier should
be _ underscore or a character ,
• but a number is not valid for identifiers
and
• identifiers are case sensitive In the above example, we have used identifiers like my_variable, counter,
calculate_area, MyClass, and math.
Dr.Thyagaraju GS
Dr.Thyagaraju GS
5. Keywords
• The reserved words of Python which have a special fixed meaning for the
interpreter are called keywords.
• No keyword can be used as an identifier or variable names. There are 36 keywords
in python as listed below:
Dr.Thyagaraju GS
Dr.Thyagaraju GS
type(False)
# output : bool
type(true)
# output: Name Error : name “ true” is not
defined
context = True
Dr.Thyagaraju GS
• A Boolean expression is an
Boolean Expressions
expression that evaluated to
produce a result which is a
Boolean value.
P = “hel”
P + “lo” == “hello” # output: True Dr.Thyagaraju GS
Comparison Operators 55 == 55 # output: True
55
== 79 # output: False
>= Greater than
Operato Meaning or equal to
r == Equal to
7!=10 # output : True
!= Not Equal to
7!=7 #output : False
< Less than True == True # output: True
> Greater than True != False # output: True
<= Les than or
equal to 12< 13 # output: True
55.55 > 66.75 # output : False
“tag”< = 2 Type error : ‘<’ is not supported between instances of ‘str’ and
‘int’. Dr.Thyagaraju GS
# output :
Boolean Operators:
True and True # output : True True and False # output : False False and True #
output : False False and False # output :False
Op1 Op2 Op1 and Op2
True False False
False True False
False False False
True True True
Not operator:
• It is a unary operator and evaluates the expression to
opposite value true or false as illustrated below :
x = 10
y = 20
x<y and x>y # output : False
(x<y) and (x!=y) or (x*2) and (x<20 or y<20) # output : True
2+2 == 4 and not 2+2 == 5 and 2*2 == 2+2 #output : True
5*7 +8 == 7 or not 5+7 ==10 and 5*4 ==20 #output : True
Dr.Thyagaraju GS
a) Conditions:
Conditions are Boolean expressions with the boolean values True or False. Flow control statements decides what to do
based on the condition whether it is true or false.
b) Blocks of Code /Clause:
The set of more than one statements grouped with same indentation so that they are syntactically equivalent to a single
statement is known as Block or Compound Statement. One can tell when a block begins, and ends based on indentation
of the statements. Following are three rules for blocks:
1. Blocks begin when the indentation increases 2. Blocks can have nested blocks
3. Blocks end when the indentation decreases to zero Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
previous if or elif conditions are false. • break statement: Terminates the innermost loop and continues
• else statement: Executes a block of code if none of the previous with the next statement after the loop.
conditions are true. • continue statement: Skips the rest of the current iteration and
moves to the next iteration of the loop.
Looping Statements: • pass statement: Acts as a placeholder, allowing you to create
empty code blocks without causing syntax errors
• for loop: Iterates over a sequence (such as a list, tuple, string, or
range) and executes a block of code for each item in the sequence.
• while loop: Repeats a block of code as long as a specified Exception Handling Statements:
condition is true. • try statement: Defines a block of code where exceptions might
Flow control statements in Python are used to control occur.
the order of execution and make decisions based on
• except statement: Specifies the code to execute if a specific
certain conditions. The main flow control statements
in Python include exception occurs within the try block.
• finally statement: Defines a block of code that will be executed
regardless of whether an exception occurred or not.
Dr.Thyagaraju GS
if
statement
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Dr.Thyagaraju GS
2. If else
statement:
Dr.Thyagaraju GS
Dr.Thyagaraju GS
3. Nested if
else:
Dr.Thyagaraju GS
Dr.Thyagaraju GS
4. if – elif
ladder:
Dr.Thyagaraju GS
Example
Dr.Thyagaraju GS
while
Statement
Dr.Thyagaraju GS
Dr.Thyagaraju GS
for Loop
Dr.Thyagaraju GS
Dr.Thyagaraju GS
range ( ) function:
The range() function returns a sequence of numbers,
starting from 0 to a specified number ,incrementing
each time by 1.
Syntax :
range(start,step,stop)
Dr.Thyagaraju GS
Dr.Thyagaraju GS
Infinite Loop
A loop becomes infinite
loop
if a condition never
becomes
FALSE. You must use
caution
when using while loops
because of the possibility
that this condition never
resolves to a FALSE value.
This results in a loop that
never ends. Such a loop is
called an infinite loop.
Dr.Thyagaraju GS
break
and
continue
Dr.Thyagar
aju GS
The Pass
Statement :
• The pass statement in Python is
used
when a statement is required
syntactically but you do not want
any
command or code to execute.
• The pass statement is
a null operation; nothing happens
when it executes. The pass is also
useful in places where your code will
eventually go, but has not been
written yet (e.g., in stubs for example):
Dr.Thyagaraju GS
Importing Modules
• Each module is a Python program that contains a related group of functions that can be
embedded in your programs. For example, the math module has mathematics related functions,
the random module has random number–related functions, and so on.
In code, an import statement consists of the following: • The import keyword
• The name of the module
• Optionally, more module names, as long as they are separated by commas Dr.Thyagaraju GS
Importing math
Dr.Thyagaraju GS
Importing
random
Dr.Thyagaraju GS
Importing random
Dr.Thyagaraju GS
Ending a Program
Early with sys.exit ()
Dr.Thyagaraju GS