You are on page 1of 51

Programming in Python (PRP)

MULLAI THIAGU

IFMR, GSB
( DATA505 – PRP )

21 JANUARY, 2023
DATA TYPES AND OPERATORS
Part I: Operators & Operands

Part II: Variables, constants &


statements

SESSION 2- Part III: Creating expressions


AGENDA
Part IV: Comments

Part V: Hands-on
PART I – OPERATORS & OPERANDS
OPERATORS & OPERANDS
 In Python, operators are special symbols that designate that
some sort of computation should be performed

 The values that an operator acts on are called operands

 Operands ? a, b
 Operators ? + =
Category Operators

Arithmetic +, -, *, /, %, //, **
Category Operators

Arithmetic +, -, *, /, %, //, **

Comparison >, < , ==, != , >=, <=


Category Operators

Arithmetic +, -, *, /, %, //, **

Comparison >, < , ==, != , >=, <=

Logical and, or, not

Multiple logical operators and operands can be strung together to


form compound logical expressions
Category Operators

Arithmetic +, -, *, /, %, //, **

Comparison >, < , ==, != , >=, <=

Logical and, or, not

Bitwise &, |, ~, ^, >>, <<

Bitwise operators treat operands as sequences of binary digits


and operate on them bit-by-bit
Category Operators

Arithmetic +, -, *, /, %, //, **

Comparison >, < , ==, != , >=, <=

Logical and, or, not

Bitwise &, |, ~, ^, >>, <<

=, +=, ==, *=, /=, %=, //=,


Assignment
**=, &=, |=, ^=, >>=, <<=

A single equal sign (=) is used to assign a value to a variable

The value to the right of the assignment is an expression containing


other variables
Category Operators

Arithmetic +, -, *, /, %, //, **

Comparison >, < , ==, != , >=, <=

Logical and, or, not

Bitwise &, |, ~, ^, >>, <<

=, +=, ==, *=, /=, %=, //=,


Assignment
**=, &=, |=, ^=, >>=, <<=

Special : identity is, is not


Category Operators

Arithmetic +, -, *, /, %, //, **

Comparison >, < , ==, != , >=, <=

Logical and, or, not

Bitwise &, |, ~, ^, >>, <<

=, +=, ==, *=, /=, %=, //=,


Assignment
**=, &=, |=, ^=, >>=, <<=

Special : identity is, is not

Special : membership in, not in


ARITHMETIC OPERATORS
Operator Meaning Example
+ Add two operands or unary plus x + y+ 2
Subtract right operand from the
- left or unary minus x-y-2

Multiply two operands


* x*y
Divide left operand by the right
/ one (always results into float) x/y
ARITHMETIC OPERATORS
Opera x=9
Meaning Example
tor y=5
Add two operands or unary
+ x + y+ 2 9 + 5 + 2 = 16
plus
ARITHMETIC OPERATORS
x=9
Operator Meaning Example
y=5
Add two operands or
+ x + y+ 2 9 + 5 + 2 = 16
unary plus
Subtract right
operand from the left
- or unary minus x-y-2 9-5-2=2

Multiply two
* operands x*y 9 * 5 = 45

Divide left operand


by the right one 9/5 = 1.8
/ x/y
(always results into
float)
Operator Meaning Example

Modulus - remainder of the division of x%y


%
left operand by the right (remainder of x/y)

Floor division - division that results


// into whole number adjusted to the left x // y
in the number line

Exponent - left operand raised to the x**y


**
power of right (x to the power y)
Operator Meaning Example x=9,y=5
Modulus -
remainder of the x%y
% division of left (remainder of 9%5=4
operand by the x/y)
right
Floor division -
division that
results into whole
// x // y 9 // 5 = 1
number adjusted
to the left in the
number line
Exponent - left
x**y 9 ** 5 = 95 =
** operand raised to
(x to the power y) 59049
the power of right
CREATE A PYTHON CODE

EXECUTE THE CODE AND OBSERVE


IF THE OUTPUT IS CORRECT
CREATE A PYTHON CODE WITH VARIABLE VALUE SET BY USER
COMPARISON OPERATORS

Relational
Math
Meaning operator in
symbol
python
Equals = ==
Not equal ≠ !=
Less than < <
Less than or equal ≤ <=

Greater than > >

Greater than or equal ≥ >=


Expected Output
Relational
Math Example
Meaning operator
symbol
in python A = 10, A = -12, A = 100,
B=5 B = 100 B = 100

Equals = == A == B False False True


Not equal ≠ != A != B True True False
Less than < < A<B False True False
Less than
≤ <= A <= B False True True
or equal
Greater
> > A>B True False False
than
Greater
than or ≥ >= A >= B True False True
equal
LOGICAL OPERATORS
Example
Operator Meaning
(x = 10, y = 20)
(x > 0) and (y <10)
True if both x and y are True
and
False otherwise
False
(x > 0) or (y <10)
True if either x or y is True
or
False otherwise
True
True if x is False
(x >0)
not False if x is True
True
(Logically reverses the sense of x)
LOGICAL OPERATORS
Example
Operator Meaning
(x = 10, y = 20)
(x > 0) and (y >10)
True if both x and y are True
and
False otherwise
True
(x > 0) or (y >10)
True if either x or y is True
or
False otherwise
True
True if x is False
(x <0)
not False if x is True
False
(Logically reverses the sense of x)
LOGICAL OPERATORS
Example
Operator Meaning
(x = 10, y = 20)
(x > 0) and (y >10)
True if both x and y are True
and
False otherwise
True
(x > 0) or (y >10)
True if either x or y is True
or
False otherwise
True
True if x is False
not(x <0)
not False if x is True
True
(Logically reverses the sense of x)
BITWISE OPERATORS

x = 10 -> (0000 1010) in binary


y=4 -> (0000 0100) in binary
PART II – VARIABLES, CONSTANTS &
STATEMENTS
VARIABLES
 Provide a way to associate names with data/objects

 A variable is just a name

 It can be bound to different objects of type int, float, string etc

 An assignment statement associates the name to the left of the =


symbol with the value denoted by the expression to the right

Example: A = 15
Variables:

pi ,
circle_radius,
circle_area

Output
RULES AND NAMING CONVENTION FOR
VARIABLES AND CONSTANTS
 Constant and variable names should have a combination of letters
in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an
underscore (_)

Example: input_data , IMAGE_DATA, , arrayData,


TextfileData

 Create a name that makes sense

Example: data makes more sense than d

 If you want to create a variable name having two words, use


underscore to separate them

Example: sum_initial, sum_final


 Use capital letters possible to declare a constant

Example: PI, G, MASS, SPEED_OF_LIGHT, TEMP

 Never use special symbols like !, @, #, $, %, etc

 Don't start a variable name with a digit


LITERALS
 Literal is a raw data given in a variable or constant

 Types of literals in Python:

 Numeric - immutable (unchangeable)

 String - sequence of characters surrounded by quotes


 Single (‘ ‘)

 Double (“ “)
 Triple (‘’’ ‘’’)

A character literal is a single character surrounded


by single or double quotes
LITERALS CONTD .,

 Types of literals in Python:

 Boolean - can have any of the two


values: True or False

 Special - Python contains one special literal i.e. None

We use it to specify that the field has not been created


POINTS ABOUT INTEGERS, FLOAT AND COMPLEX NUMBERS

 Integers can be of any length, it is only limited by the memory


available

 A floating-point number is accurate up to 15 decimal


places

 Integer and floating points are separated by decimal points

Example: 1 is an integer, 1.0 is a floating-point number

 Complex numbers are written in the form, x + yj, where x is the


real part and y is the imaginary part
Example for int:

1, -3, 5, 10002

Example for float:

1.000053, -3.0, 3.17, 28.72

1.6E3 is float which stands for 1.6 * 103 same as 1600.0

Example for complex:

X = 3.14j is a complex literal


STATEMENTS

 Instructions that a
Python interpreter
can execute are
called statements

 For example, a = 1
is an assignment
statement
PART III – CREATING EXPRESSIONS
EXPERESSION : EXAMPLE
 What is the expected
output ?
A ) 240
B) 60

Operator Precedence
https://realpython.com/python-operators-expressions/
PART IV – COMMENTS
COMMENTS
 Comments in source code are another form of note

 Add them to the code to remember what task the code performs

 A special way to determine the text written in the code does not get
executed by the computer -> ‘Comments’

 Python supports two methods of defining comments:


 Single line - use number sign (#)
 Multiline – starts and ends with three double quotes (“ “ “ ” ” ”)
COMMENTS
COMMENTS
REASONS
 Reminding yourself about what the code does and why you wrote it

 Telling others how to maintain your code

 Making your code accessible to other developers

 Listing ideas for future updates

 Providing a list of documentation sources you used to write the code

 Maintaining a list of improvements you’ve made


PART V – HANDS-ON
CLASSWORK

 Write a python program to ask user to enter three integers


a, b, c. Compute the value of Z as below and print the
output

Z = a + (b * c)

 Write a python program to calculate the sum of 5 numbers


and print their sum

 Write a python program to draw a house using turtle


RECAP – TAKEAWAY !

 Python data types and operators

❖ Python is a CASE-SENSITIVE language (Example: variable


name test ≠ Test)

 Anaconda IDE : File create, save, execute/run and open

 Python datatypes (int, float & string) and operators (+, -, *, /,


//, %,**)
THANK YOU
REFERENCES

Some of the content is taken from the below online sources:

1. https://cs50.harvard.edu/college/2019/fall/notes/0/
2. www.theacademyofcode.com/handouts
3. https://www.skillsyouneed.com/num/polygons.html
4. https://cs50.harvard.edu/college/2019/fall/notes/0/
5. https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/turing-machine
6. https://www.programiz.com/python-programming/variables-constants-literals
7. https://www.programiz.com/python-programming/operators
8. https://www.tutorialspoint.com/python/python_basic_operators.htm
9. https://stackabuse.com/overloading-functions-and-operators-in-python/
10. https://www.w3schools.com/python/ref_math_sin.asp
11. https://www.discogcodingacademy.com/turtle-colours
12. https://www.javatpoint.com/python-turtle-programming
13. https://realpython.com/python-operators-expressions/

You might also like