Intro to Python - Day 1
Intro to Python - Day 1
[email protected]
VGNQ9IQTEJ
○ Fundamental
[email protected] Data Types
VGNQ9IQTEJ
● Functions in Python – print()
● Operators
● Python Flow Control
● Pseudocodes
[email protected]
VGNQ9IQTEJ
#
Code commenting is required in order to describe the purpose of the code. This is
not only a good practice but also a mandatory practice in most organizations.
● Variables are containers that store data. Python has no command for
declaring variable
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
Type Casting
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
● A function has a
[email protected] name. You call the function by its
VGNQ9IQTEJname
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
That’s wrong because adding numbers to strings doesn’t make any sense. You
need to explicitly convert the number to string first, in order to join them together.
[email protected]
VGNQ9IQTEJ
Python
Operators
[email protected]
VGNQ9IQTEJ
Arithmetic Relational Logical Special
Operators Operators Operators Operators
Bitwise Assignment
Operators Operators
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
When two strings are added, the operator basically concatenates the two strings.
Subtracting two strings does not make any sense.
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Multiplication
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
< is less than
VGNQ9IQTEJ
<= is less than & equal to
> is greater than
>= is greater than & equal to
== is equal to
!= is not equal to
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Relational operators
[email protected]
VGNQ9IQTEJ
Logical operators in Python are used for conditional statements are either
True or False
[email protected]
AND Returns True if both the operands are True
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
The ‘in’ operator is used to check if a value exists in any sequence object or not.
in For example, if 2 exists in a list, say [4, 5, 7, 2]. This evaluates to True if it finds a
value in the specified sequence object. Otherwise it returns a False.
[email protected]
VGNQ9IQTEJnot in A ‘not in’ works in an opposite way to an ‘in’ operator. A ‘not in’ evaluates to True if
a value is not found in the specified sequence object. Else it returns a False.
Example:
[email protected]
VGNQ9IQTEJ
Python Bitwise Operators take one to two operands, and operates on it/them bit by
bit, instead of whole
[email protected]
VGNQ9IQTEJ & The binary and (&) takes two values and performs an AND-ing on each pair of
(Bitwise bits.
and)
Compared to &, this one returns 1 even if one of the two corresponding bits from
| the two operands is 1.
(Bitwise or)
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The truth tables
& |
(Bitwise and) (Bitwise or)
Example:
[email protected]
VGNQ9IQTEJ
(9 % 3 == 0) & (9 % 5 == 0) (9 % 3 == 0) | (9 % 5 == 0)
a += b is same as a = a + b
[email protected]
VGNQ9IQTEJ a *= b is same as a = a * b
a /= b is same as a = a / b
a %= b is same as a = a % b
a **= b is same as a = a ** b
a //= b is same as a = a // b
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Data slicing
The basic syntax for a slice is square brackets with colons and integers
inside "[0:1:2]".
myStr[start : stop : step]
[email protected]
myStr[ : stop] # By using one colon and leaving the first argument blank
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
[email protected]
VGNQ9IQTEJ
Any program has a flow. The flow is the order in which the program’s code
executes. The control flow of a Python program is controlled by:
[email protected]
VGNQ9IQTEJ 1. Conditional Statements
2. Loops
3. Function Calls
Syntax:
if test expression:
statement(s)
[email protected]
VGNQ9IQTEJ
The ‘if..else’ statement evaluates a test expression and will execute the code
that is part of the ‘if’ expression if the test expression True.
If the test expression is False, the code that is part of the ‘else’ expression is
executed. Note the indentation that is used to separate the ‘if’ and ‘else’ blocks.
[email protected]
VGNQ9IQTEJ
Syntax:
if test expression:
Body of if
else:
Body of else
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The if-else statement
[email protected]
VGNQ9IQTEJ
The ‘while loop’ in Python is used to iterate over a block of code as long as the
test expression holds true.
[email protected]
VGNQ9IQTEJ
We generally use this loop when the number of times to iterate is not known to
us beforehand.
Syntax:
while test_expression:
Body of while
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The while Loop
[email protected]
VGNQ9IQTEJ
The ‘for loop’ in Python is used to iterate over the items of a sequence object like
list, tuple, string and other iterable objects.
The iteration continues until we reach the last item in the sequence object. Note
the indentation that is used in a ‘for loop’ to separate the rest of the code from the
[email protected]
VGNQ9IQTEJ
‘for loop’ syntax
Syntax:
for i in sequence:
Body of for
[email protected]
VGNQ9IQTEJ
[email protected]
Pseudocode is a step-by-step written outline of your code that you can
VGNQ9IQTEJ
input
There are many ways to accomplish the same task in Python, but
there is usually one preferred way to do it. This preferred way is
called "pythonic."
[email protected]
VGNQ9IQTEJ