2. What is Python
Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991. It is
used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
3. What can Python do
• Python can be used on a server to create web
applications.
• Python can be used alongside software to create
workflows.
• Python can connect to database systems. It can also
read and modify files.
• Python can be used to handle big data and perform
complex mathematics.
• Python can be used for rapid prototyping, or for
production-ready software development.
4. Why Python
• Python works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs
with fewer lines than some other programming languages.
• Python runs on an interpreter system, meaning that code
can be executed as soon as it is written. This means that
prototyping can be very quick.
• Python can be treated in a procedural way, an object-
oriented way or a functional way.
5. Python Syntax compared to other
programming languages
• Python was designed for readability, and has some
similarities to the English language with influence from
mathematics.
• Python uses new lines to complete a command, as
opposed to other programming languages which often
use semicolons or parentheses.
• Python relies on indentation, using whitespace, to
define scope; such as the scope of loops, functions and
classes. Other programming languages often use curly-
brackets for this purpose.
6. Python Syntax
• Python is an interpreted programming language, this
means that as a developer you write Python (.py) files in
a text editor and then put those files into the python
interpreter to be executed
• Python syntax can be executed by writing directly in the
Command Line
• Or by creating a python file on the server, using the .py
file extension, and running it in the Command Line
8. Identation
• Indentation refers to the spaces at the beginning of a
code line.
• Where in other programming languages the indentation
in code is for readability only, the indentation in Python
is very important.
• Python uses indentation to indicate a block of code and
will error if you skip the indentation
10. Comments
• Comments can be used to explain Python code.
• Comments can be used to make the code more
readable.
• Comments can be used to prevent execution when
testing code.
• Comments starts with a #, and Python will ignore them
• Comments can be placed at the end of a line, and
Python will ignore the rest of the line
• A comment does not have to be text that explains the
code, it can also be used to prevent Python from
executing code
12. Variables
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume). Rules for
Python variables: A variable name must start with a letter
or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE
are three different variables)
14. Variables (Assign Multiple Values)
• Python allows you to assign values to multiple variables
in one line
• Python can assign the same value to multiple variables
in one line
• If you have a collection of values in a list, tuple etc.
Python allows you to extract the values into variables.
This is called unpacking
15. Multiple value
assign values to multiple variables
in one line
same value to multiple variables
in one line
Unpack a Collection
16. Global and Local Variables
• Variables that are created outside of a function (as in all
of the examples above) are known as global variables.
• Global variables can be used by everyone, both inside
of functions and outside.
• Variable created with the same name inside a function,
this variable will be local, and can only be used inside
the function.
• The global variable with the same name will remain as it
was, global and with the original value.
17. Data Types
Category Types
Text Type str
Numeric Types int, float, complex
Sequence Types list, tuple, range
Mapping Type dict
Set Types set, frozenset
Boolean Type bool
Binary Types bytes, bytearray, memoryview
None Type NoneType
18. Setting The Data Type
In Python, the data type is set when you assign a value
to a variable
Example Data Type
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
19. Setting The Data Type
Example Data Type
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
20. Setting the Spesific Data Type
Example Data Type
x = str("Hello World”) str
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = list(("apple", "banana", "cherry”)) list
x = tuple(("apple", "banana", "cherry")) tuple
x = range(6) range
x = dict(name="John", age=36) dict
x = set(("apple", "banana", "cherry")) set
21. Setting the Spesific Data Type
Example Data Type
x = frozenset(("apple", "banana", "cherry")) frozenset
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
22. Praktikum
• Mencoba menjalankan syntax python menggunakan
Command Line
• Mencoba menjalankan python menggunakan file (.py)
• Mencoba Comments
• Mencoba identation
• Mencoba variables
• Mencoba berbagai tipe data
24. Basic Operators
Python language supports the following types of
operators
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
25. Arithmetic Operators
Operator Description Example
+ Addition Adds values on either side of the
operator.
a + b = 31
- Subtraction Subtracts right hand operand
from left hand operand.
a – b = -11
• Multiplication Multiplies values on either side
of the operator
a * b = 210
/ Division Divides left hand operand by
right hand operand
b / a = 2.1
Assume variable a = 10 and variable b = 21
26. Arithmetic Operators (2)
Operator Description Example
% Modulus Divides left hand operand by
right hand operand and returns
remainder
b % a = 1
** Exponent Performs exponential (power)
calculation on operators
a**b =10 to the power 20
// Floor Division - The division of
operands where the result is the
quotient in which the digits
after the decimal point are
removed.
9//2 = 4 and
9.0//2.0 = 4.0
Assume variable a = 10 and variable b = 21
27. Comparison Operators
Operator Description Example
== If the values of two operands are
equal, then the condition
becomes true.
(a == b) is not true.
!= If values of two operands are not
equal, then condition
becomes true.
(a!= b) is true.
> If the value of left operand is
greater than the value of right
operand, then condition
becomes true.
(a > b) is not true.
Assume variable a = 10 and variable b = 21
28. Comparison Operators (2)
Operator Description Example
< If the value of left operand is
less than the value of right
operand, then condition
becomes true.
(a < b) is true.
>= If the value of left operand is
greater than or equal to the
value of right operand, then
condition becomes true.
(a >= b) is not true.
<= If the value of left operand is
less than or equal to the value
of right operand, then condition
becomes true.
(a <= b) is true.
Assume variable a = 10 and variable b = 21
29. Assignment Operators
Operator Description Example
= Assigns values from right side
operands to left side operand
c = a + b assigns value of a + b
into c
+= Add AND It adds right operand to the left
operand and assign the result to
left operand
c += a is equivalent to c = c + a
-= Subtract AND It subtracts right operand from
the left operand and assign the
result to left operand
c -= a is equivalent to c = c - a
30. Assignment Operators (2)
Operator Description Example
*= Multiply AND It multiplies right operand with
the left operand and assign the
result to left Operand
c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the
right operand and assign the
result to left Operand
c /= a is equivalent to
c = c / ac /= a is equivalent to c
= c / a
%= Modulus AND It takes modulus using two
operands and assign the result
to left operand
c %= a is equivalent to
c = c % a
31. Assignment Operators (3)
Operator Description Example
**= Exponent AND Performs exponential (power)
calculation on operators and
assign value to the left Operand
c **= a is equivalent to
c = c ** a
//= Floor Division It performs floor division on
operators and assign value to
the left operand
c //= a is equivalent to
c = c // a
32. Bitwise Operators
Operator Description Example
& Binary AND Operator copies a bit to the
result, if it exists in both
operands
(a & b) (means 0000 1100)
| Binary OR It copies a bit, if it exists in either
operand.
(a | b) = 61 (means 0011 1101)
^ Binary XOR It copies the bit, if it is set in one
operand but not both.
(a ^ b) = 49 (means 0011 0001)
Assume variable a = 60 and variable b = 13. Which is in binary a = 0011 1100, b = 0000 1101
33. Bitwise Operators (2)
Operator Description Example
~ Binary Ones Complement It is unary and has the effect of
'flipping’ bits.
(~a ) = -61 (means 1100 0011 in
2’s complement form due to a
signed binary number.
<< Binary Left Shift The left operand’ s value is
moved left by the number of bits
specified by the right operand.
a << = 240 (means 1111 0000)
>> Binary Right Shift The left operand’ s value is
moved right by the number of
bits specified by the right
operand.
a >> = 15 (means 0000 1111)
Assume variable a = 60 and variable b = 13. Which is in binary a = 0011 1100, b = 0000 1101
34. Logical Operators
Operator Description Example
and Logical AND If both the operands are true
then condition becomes true.
(a and b) is False.
or Logical OR If any of the two operands are
non-zero then condition
becomes true.
(a or b) is True.
not Logical NOT Used to reverse the logical state
of its operand.
Not(a and b) is True.
Assume variable a = True and variable b = False
35. Membership Operators
Operator Description Example
In Evaluates to true, if it finds a
variable in the specified
sequence and false otherwise.
x in y, here in results in a 1 if x is
a member of sequence y.
not in Evaluates to true, if it does not
find a variable in the specified
sequence and false otherwise.
x not in y, here not in results in a
1 if x is not a member of
sequence y.
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples
36. Identity Operators
Operator Description Example
is Evaluates to true if the variables
on either side of the operator
point to the same object and
false otherwise.
x is y, here is results in 1 if id(x)
equals id(y).
is not Evaluates to false if the
variables on either side of the
operator point to the same
object and true otherwise.
x is not y, here is not results in 1
if id(x) is not equal to id(y).
Identity operators compare the memory locations of two objects.
37. Operators Precedence
Operator Description
** Exponentiation (raise to the power)
~ + - Ccomplement, unary plus and minus (method names for the last two
are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^ | Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
Operator precedence affects the evaluation of an an expression.
38. Operators Precedence
Operator Description
= %= /= //= -= += *=
**=
Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Operator precedence affects the evaluation of an an expression.
40. Decision Making
• Decision-making is the anticipation
of conditions occurring during the
execution of a program and
specified actions taken according to
the conditions.
• Decision structures evaluate
multiple expressions, which produce
TRUE or FALSE as the outcome.
• Python programming language
assumes any non-zero and non-null
values as TRUE, and any zero or
null values as FALSE value.
41. Decision Making Statements
Statement Description
if statements An if statement consists of a Boolean expression followed by one or
more statements.
if...else statements An if statement can be followed by an optional else statement, which
executes when the Boolean expression is FALSE.
nested if statements You can use one if or else if statement inside
another if or else if statement(s).
44. IF...ELIF...ELSE Statements
Syntax
if…else
Flow Diagram
An else statement can be combined with an if statement. An else
statement contains a block of code that executes if the
conditional expression in the if statement resolves to 0 or a
46. The elif statement
• The elif statement allows you to check multiple
expressions for TRUE and execute a block of code as
soon as one of the conditions evaluates to TRUE.
• Similar to the else, the elif statement is optional.
However, unlike else, for which there can be at the most
one statement, there can be an arbitrary number of elif
statements following an if.
48. Nested IF Statements
There may be a situation when you want to check for another condition after a condition resolves to true. In such a s
you can use the nested if construct. In a nested if construct, you can have an if...elif...else construct inside another
if...elif...else construct.
52. Loops
• In general, statements are
executed sequentially- The first
statement in a function is
executed first, followed by the
second, and so on. There may be
a situation when you need to
execute a block of code several
number of times.
• Programming languages provide
various control structures that
allow more complicated execution
paths.
• A loop statement allows us to
execute a statement or group of
statements multiple times.
53. Types of loops
Loop Type Description
while loop Repeats a statement or group of
statements while a given
condition is TRUE. It tests the
condition before executing the
loop body.
for loop Executes a sequence of statements
multiple times and
abbreviates the code that manages
the loop variable.
nested loops You can use one or more loop
inside any another while, or
for loop.
54. Loop Statements
Syntax
Flow Diagram
A while loop statement in Python programming language
repeatedly executes a target statement as long as a given
condition is true.
56. The Infinite Loop
An infinite loop might be useful in client/server
programming where the server needs to run continuously
so that client programs can communicate with it as and
when required.
57. Else Statement with Loops
• If the else statement is used with a for loop, the else
statement is executed when the loop has exhausted
iterating the list.
• If the else statement is used with a while loop, the else
statement is executed when the condition becomes
false.
58. for Loop Statements
Syntax
Flow Diagram
The for statement in Python has the ability to iterate over the
items of any sequence, such as a list or a string.
61. else Statement with Loops
• If the else statement is used with a for loop, the else
block is executed only if for loops terminates normally
(and not by encountering break statement).
• If the else statement is used with a while loop, the else
statement is executed when the condition becomes
false.
65. Loop Control Statements
The Loop control
statements
change the
execution from
its normal
sequence. When
the execution
leaves a scope,
all automatic
objects that were
created in that
scope are
destroyed.
Control Statement Description
break statement Terminates the loop statement and
transfers
execution to the statement
immediately
following the loop.
continue statement Causes the loop to skip the remainder
of its
body and immediately retest its
condition prior
to reiterating.
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
68. continue Statements
Syntax
Flow Diagram
The continue statement in Python returns the control to
the beginning of the current loop.
When encountered, the loop starts next iteration without
executing the remaining statements in the current
70. pass Statements
Syntax
It 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 statement is also useful in places where your
code will eventually go, but has not been written yet i.e. in stubs).
73. Numbers
Python supports different numerical types-
• int (signed integers): They are often called just integers or ints.
They are positive or negative whole numbers with no decimal
point. Integers in Python 3 are of unlimited size. Python 2 has two
integer types - int and long. There is no 'long integer' in Python 3
anymore.
• float (floating point real values): Also called floats, they
represent real numbers and are written with a decimal point
dividing the integer and the fractional parts. Floats may also be in
scientific notation, with E or e indicating the power of 10 (2.5e2 =
2.5 x 102 = 250).
• complex (complex numbers): are of the form a + bJ, where a
and b are floats and J (or j) represents the square root of -1
(which is an imaginary number). The real part of the number is a,
and the imaginary part is b. Complex numbers are not used much
in Python programming.
75. Number Type Conversion
• Type int(x) to convert x to a plain integer.
• Type long(x) to convert x to a long integer.
• Type float(x) to convert x to a floating-point number.
• Type complex(x) to convert x to a complex number with
real part x and imaginary part zero.
• Type complex(x, y) to convert x and y to a complex
number with real part x and imaginary part y. x and y
are numeric expressions.