You are on page 1of 14

Python: Basics

• Variables
• Data types
• Operators
• Arrays
• Flow Control
• Methods
• File Handling
• OOPS

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Expressions
• Most statements (logical lines) contain expressions
• A simple example of an expression is 2 + 3.
• An expression can be broken down into operators and operands.
• All expressions have a value.
• The process of determining the expression’s value is called evaluation.
• The literal value 54 evaluates to 54.
• The value of a variable named x is the value stored in the memory location bound to x.
• The value of a more complex expression is found by evaluating the smaller expressions that make it up
and combining them with operators to form potentially new values.
• When an operator has mixed operands—one operand an integer and the other a
floating-point number—the interpreter treats the integer operand as floating-point
number and performs floating-point arithmetic.
value1 = int(input('Please enter a number: '))
value2 = int(input('Please enter another number: '))
sum = value1 + value2
print(value1, '+', value2, '=', sum)

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operators can be used as unary
operators, has only
one operand
• Python operators
Operator Name Explanation Examples

+ Plus Adds the two objects 3 + 5 gives 8. 'a' + 'b'


gives 'ab'.
- Minus Either gives a negative number or gives the -5.2 gives a negative
subtraction of one number from the other number. 50 - 24 gives
26.
* Multiply Gives the multiplication of the two numbers or 2 * 3 gives 6. 'la' * 3
returns the string repeated that many times. gives 'lalala'.
** Power Returns x to the power of y 3 ** 4 gives 81 (i.e. 3 *
3 * 3 * 3)

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operators
• Python operators
Operator Name Explanation Examples

/ Divide Divide x by y 4 / 3 gives


1.3333333333333333
// Floor Division Returns the floor of the quotient 4 // 3 gives 1.

% Modulo Returns the remainder of the division 8 % 3 gives 2. -25.5 %


2.25 gives 1.5.
<< Left Shift Shifts the bits of the number to the left by 2 << 2 gives 8. 2 is
the number of bits specified. (Each represented by 10 in
number is represented in memory by bits bits. Left shifting by 2
or binary digits i.e. 0 and 1) bits gives 1000 which
represents the decimal
8.

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operators
• Python operators
Operator Name Explanation Examples

>> Right Shift Shifts the bits of the number to the 11 >> 1 gives 5. 11 is
right by the number of bits represented in bits by 1011
specified. which when right shifted by 1
bit gives 101 which is
the decimal 5.
& Bitwise AND Bitwise AND of the numbers 5 & 3 gives 1.

| Bit-wise OR Bitwise OR of the numbers 5 | 3 gives 7

^ Bit-wise XOR Bitwise XOR of the numbers 5 ^ 3 gives 6

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operators
• Python operators
Operator Name Explanation Examples

~ Bit-wise invert The bit-wise inversion of x is -(x+1) ~5 gives -6.

< Less Than Returns whether x is less than y. All 5 < 3 gives False and 3 < 5
comparison operators return True gives True.
or False. Note the capitalization of Comparisons can be chained
these names. arbitrarily: 3 < 5 < 7
gives True.
> Greater Than Returns whether x is greater than y 5 > 3 returns True. If both
operands are numbers, they
are first converted to a
common type. Otherwise, it
always returns False.

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operators
• Python operators
Operator Name Explanation Examples

<= Less Than or Returns whether x is less than or x = 3; y = 6; x <= y returns


Equal To equal to y True.
>= Greater Than Returns whether x is greater than or x = 4; y = 3; x >= 3 returns
or Equal To equal to y True.
== Equal To Compares if the objects are equal x = 2; y = 2; x == y returns
True.
x = 'str'; y = 'stR'; x == y
returns False.
x = 'str'; y = 'str'; x == y
returns True..
!= Not Equal To Compares if the objects are not x = 2; y = 3; x != y returns
equal True.

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operators
• Python operators
Operator Name Explanation Examples

not Boolean NOT If x is True, it returns False. If x = True; not x returns False.
x is False, it returns True.
and Boolean AND x and y returns False if x is False, else it x = False; y = True; x and y returns
returns evaluation of y False since x is False. In this case,
Python will not evaluate y since it
knows that the left hand side of the
'and’ expression is False which
implies that the whole expression
will be False irrespective of the
other values. This is called short
circuit evaluation.
or Boolean OR If x is True, it returns True, x = True; y = False; x or y returns
else it returns evaluation of y True.
Short-circuit evaluation applies
here as well.
DEPARTMENT OF APPLIED PHYSICS,
EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Operator Precedence and Associativity
• Precedence—when an expression contains different kinds
of operators, which should be applied first?
• Associativity—when an expression contains two operators
with the same precedence, which should be applied first?
Arity Operators Associativity
Binary ** Right
Unary +, -
Binary *, /, //, % *, /, //, % decreasing
Binary +, - Left
Binary = Right
DEPARTMENT OF APPLIED PHYSICS,
EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Assignment Operator
• Programmers use the assignment operator only to build assignment
statements.
• Python does not allow the assignment operator to be part of a larger
expression or part of another statement.
y = 2
x = (y = 2) + 3
x = y + 3
• The notions of precedence and associativity do not apply in the context of
the assignment operator.
• Support a special kind of assignment statement called chained assignment,
w = x = y = z
• To initialize several variables to zero in one statement,
slightly
sum = count = 0 shorter
sum, count = 0, 0
DEPARTMENT OF APPLIED PHYSICS,
EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Formatting Expressions
• Mathematical expression : 3𝑥 + 2𝑦 − 5
print(3*x + 2*y - 5)
print(3*x+2*y-5)
print(3 * x + 2 * y - 5)
print(3 * x+2 * y-5)

• Multiple operator
print(3 * (x + 2) * (y - 5))
print(3 * (x + 2) * (y - 5))

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Boolean Expression
• Boolean comes from the name of the British mathematician
George Boole
• Boolean expression may have only one of two possible
values: false or true
• Boolean algebra is dedicated to the study of the properties
and the manipulation of logical expressions
>>> True >>> x = 10 >>> x <= 10 >>> x > 10
True >>> x True False
>>> False 10 >>> x == 10 >>> x < 100
False True True
>>> x < 10
>>> type(True)
False >>> x >= 10 >>> x < 5
<class 'bool'>
>>> type(False) >>> x <= 10 True False
<class 'bool'> True
DEPARTMENT OF APPLIED PHYSICS,
EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Errors
• Syntax Errors: If program not written with proper sytnax
y = 5 File "error.py", line 3
y + 2 = x
x = y + 2 ˆ mismatched
y + 2 = x SyntaxError: can't assign to operator string quotes
• SyntaxError: invalid syntax
• SyntaxError: EOL while scanning string literal
• IndentationError: unexpected indent

• Runtime exceptions: if proper type of values not used


Traceback (most recent call last):
x = y + 2
File "<ipython-input-7-e873f39c4291>", line 1,
in <module>
x = y + 2

NameError: name 'y' is not defined

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA
Errors
• Logic errors: if program not written by following proper logical steps

# Establish some variables -17.7778


degreesF, degreesC = 0, 0
# Define the relationship between F and C
degreesC = 5/9*(degreesF - 32)
# Prompt user for degrees F
degreesF = float(input('Enter the
temperature in degrees F: '))
# Report the result
print(degreesF, "degrees F =', degreesC,
'degrees C')

DEPARTMENT OF APPLIED PHYSICS,


EVEN SEMESTER
UNIVERSITY OF CALCUTTA

You might also like