Lec_1 Extended
# Example of an Error
def is_even(number):
Errors: return number % 2 == 1 # Incorrect
logic for checking even numbers
result = is_even(4)
1. Syntax Error print(result) # This should print True for
even numbers, but it will print False
Error in Grammar/vocabulary
2. Logic Error
Error in logic or algorithm(incorrect order)
3. Semantic Error
unintended output (correct order)
Variables
• Python Variable is containers that store values.
message = 'And now for something completely different’
n = 17
pi = 3.1415926535897931
type(n)
• Can’t start with number
• can’t have special character except _
• can’t use the python reserved Keywords
Keywords
• Python reserves 35 keywords
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
Asking the user for input
• Sometimes we would like to take the value for a variable from the user via their keyboard. Python
provides a built-in function called input that gets input from the keyboard. When this function is
called, the program stops and waits for the user to type something. When the user presses
Return or Enter, the program resumes and input returns what the user typed as a string.
Comments
• As programs get bigger and more complicated, they get more difficult to read. Formal languages
are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.
• #
Data Types
Numeric Data Types in Python
• The numeric data type in Python represents the data that has a numeric
value. A numeric value can be an integer, a floating number, or even a
complex number. These values are defined as Python int , Python float ,
and Python complex classes in Python .
• Integers – This value is represented by int class. It contains positive or
negative whole numbers (without fractions or decimals). In Python, there
is no limit to how long an integer value can be.
• Float – This value is represented by the float class. It is a real number
with a floating-point representation. It is specified by a decimal point.
Optionally, the character e or E followed by a positive or negative integer
may be appended to specify scientific notation.
• Complex Numbers – A complex number is represented by a complex
class. It is specified as (real part) + (imaginary part)j . For example – 2+3j
Sequence Data Types in Python
• The sequence Data Type in Python is the ordered
collection of similar or different Python data types.
Sequences allow storing of multiple values in an
organized and efficient fashion. There are several
sequence data types of Python:
• Python String
• Python List
• Python Tuple
Strings
• A string is a collection of one or more characters put in
a single quote, double-quote, or triple-quote. In Python,
there is no character data type Python, a character is a
string of length one. It is represented by str class.
• Accessing elements of String
• Creating a String
List Data Type
• Lists are just like arrays, declared in other languages
which is an ordered collection of data. It is very flexible
as the items in a list do not need to be of the same
type.
• Creating a List in Python
• Lists in Python can be created by just placing the
sequence inside the square brackets[].
Tuple Data Type
• Just like a list, a tuple is also an ordered collection of
Python objects. The only difference between a tuple and
a list is that tuples are immutable i.e. tuples cannot be
modified after it is created. It is represented by a tuple
class.
• A=(21,23,24)
Set Data Type in Python
• In Python Data Types, a Set is an unordered collection of
data types that is iterable, mutable, and has no
duplicate elements. The order of elements in a set is
undefined though it may consist of various elements.
• Concept of hash to make it fast
• Set={23,25,34}
Dictionary Data Type in Python
• A dictionary in Python is an unordered collection of data
values, used to store data values like a map, unlike other
Python Data Types that hold only a single value as an
element, a Dictionary holds a key: value pair. Key-value is
provided in the dictionary to make it more optimized. Each
key-value pair in a Dictionary is separated by a colon : ,
whereas each key is separated by a ‘comma’.
• Dict = {1: ‘CS', 2: ‘IT’, 4: ‘FLAME’}
• Dict[1]
• Dict.get(2)
Boolean Data Type in Python
• Python Data type with one of the two built-in values,
True or False.
• Boolean objects that are equal to True are truthy (true),
and those equal to False are falsy (false).
• However non-Boolean objects can be evaluated in a
Boolean context as well and determined to be true or
false. It is denoted by the class bool.
• type(True)
Operators and operands
• Operators are special symbols that represent computations like addition
and multiplication. The values the operator is applied to are called
operands.
• The operators +, -, *, /, and ** perform addition, subtraction,
multiplication, division, and exponentiation.
• Integer Division (//)
• Modulus operator (%)
• The modulus operator works on integers and yields the remainder
when the first operand is divided by the second.
• PEMDAS
Arithmetic Operators in Python
Operator Description Syntax
+ Addition: adds two operands x+y
– Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
% Modulus: returns the remainder when the first operand is x%y
divided by the second
** Power: Returns first raised to power second x ** y
Comparison of Python
Operators
Operator Description Syntax
> Greater than: True if the left operand is greater than the x>y
right
< Less than: True if the left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
Greater than or equal to True if the left operand is greater
>= x >= y
than or equal to the right
Less than or equal to True if the left operand is less than or
<= equal to the right x <= y
Boolean Operators/Logical
Operators
• Boolean Operations in Python are simple arithmetic of
True and False values. These values can be manipulated
by the use of boolean operators which include AND, Or,
and NOT. Common boolean operations are –
• or Operator Description Syntax
• and and Logical AND: True if both the x and y
operands are true
• not
• == (equivalent) or Logical OR: True if either of
the operands is true x or y
• != (not equivalent) Logical NOT: True if the
not operand is false not x
Bitwise Operators in Python
• Python Bitwise operators act on bits and perform bit-by-
bit operations. These are used to operate on binary
numbers. Operator Description Syntax
& Bitwise AND x&y
| Bitwise OR x|y
~ Bitwise NOT ~x
^ Bitwise XOR x^y
>> Bitwise right shift x>>
<< Bitwise left shift x<<
Assignment Operators in Python
Operator Description Syntax
= Assign the value of the right side of the expression to the left side operand x=y+z
Add AND: Add right-side operand with left-side operand and then assign to left
+= a+=b a=a+b
operand
Subtract AND: Subtract right operand from left operand and then assign to left
-= a-=b a=a-b
operand
Multiply AND: Multiply right operand with left operand and then assign to left
*= a*=b a=a*b
operand
/= Divide AND: Divide left operand with right operand and then assign to left operand a/=b a=a/b
Modulus AND: Takes modulus using left and right operands and assign the result to
%= a%=b a=a%b
left operand
Divide(floor) AND: Divide left operand with right operand and then assign the
//= a//=b a=a//b
value(floor) to left operand
Exponent AND: Calculate exponent(raise power) value using operands and assign value
**= a**=b a=a**b
to left operand
&= Performs Bitwise AND on operands and assign value to left operand a&=b a=a&b
|= Performs Bitwise OR on operands and assign value to left operand a|=b a=a|b
^= Performs Bitwise xOR on operands and assign value to left operand a^=b a=a^b
>>= Performs Bitwise right shift on operands and assign value to left operand a>>=b a=a>>b
<<= Performs Bitwise left shift on operands and assign value to left operand a <<= b a= a << b
Identity Operators in Python
• In Python, is and is not are the identity operators both are used to check if two
values are located on the same part of the memory.
• Two variables that are equal do not imply that they are identical.
• is True if the operands are identical
• is not True if the operands are not identical
a = 10
b = 20
c=a
print(a is not b)
print(a is c)
Membership Operators in
Python
• In Python, in and not in are the membership operators
that are used to test whether a value or variable is in a
sequence.
• in True if value is found in the sequence
• not in True if value is not found in the sequence
x = 24
y = 20
list = [10, 20, 30, 40, 50]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
• X= 5-(48*34/56)