SlideShare a Scribd company logo
1 of 99
PYTHON
PROGRAMMING (1)
Yusuf Ayuba
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.
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.
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.
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.
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
Syntax
or
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
Python Identation
not
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
Comments
or
or
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)
Variables
not
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
Multiple value
assign values to multiple variables
in one line
same value to multiple variables
in one line
Unpack a Collection
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.
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
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
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
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
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
Praktikum
• Mencoba menjalankan syntax python menggunakan
Command Line
• Mencoba menjalankan python menggunakan file (.py)
• Mencoba Comments
• Mencoba identation
• Mencoba variables
• Mencoba berbagai tipe data
PEMROGRAMAN
PYTHON (2)
Yusuf Ayuba
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
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
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
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
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
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
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
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
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
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
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
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
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.
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.
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.
Yusuf Ayuba
PEMROGRAMAN
PYTHON (3)
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.
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).
IF Statement
Syntax
Flow Diagram
Example
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
Example (IF…ELSE)
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.
The elif statement (2)
Syntax Flow Diagram
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.
Example
Single Statement Suites
If the suite of an if clause consists only of a single line, it
may go on the same line as the header statement.
PEMROGRAMAN
PYTHON (4)
Yusuf Ayuba
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.
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.
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.
Example
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.
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.
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.
Example
Iterating by Sequence Index
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.
Example
Nested Loop
Syntax
Example
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
break Statements
Syntax
Flow Diagram
The break statement is used for
premature termination of the current loop.
Example
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
Example
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).
Example
PEMROGRAMAN
PYTHON (5)
Yusuf Ayuba
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.
Example
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.
Mathematic
al Functions
abs() Method
Example
ceil() Method
Example
exp() Method
Example
fabs() Method
Example
floor() Method
Example
log() Method
Example
log10() Method
Example
max() Method
Example
min() Method
Example
modf() Method
Example
sqrt() Method
Example
End of Slides

More Related Content

Similar to modul-python-all.pptx

1. PGA2.0-Python Programming-Intro to Python.pptx
1. PGA2.0-Python Programming-Intro to Python.pptx1. PGA2.0-Python Programming-Intro to Python.pptx
1. PGA2.0-Python Programming-Intro to Python.pptxRakesh Ahuja
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfdata2businessinsight
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptxrohithprabhas1
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : PythonRaghu Kumar
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptxAnum Zehra
 
Python programming
Python programmingPython programming
Python programmingsaroja20
 
Python_Haegl.powerpoint presentation. tx
Python_Haegl.powerpoint presentation. txPython_Haegl.powerpoint presentation. tx
Python_Haegl.powerpoint presentation. txvishwanathgoudapatil1
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unitmichaelaaron25322
 
An overview on python commands for solving the problems
An overview on python commands for solving the problemsAn overview on python commands for solving the problems
An overview on python commands for solving the problemsRavikiran708913
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning ParrotAI
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answerskavinilavuG
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxNimrahafzal1
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizeIruolagbePius
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golangwww.ixxo.io
 

Similar to modul-python-all.pptx (20)

Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
 
Python basics
Python basicsPython basics
Python basics
 
1. PGA2.0-Python Programming-Intro to Python.pptx
1. PGA2.0-Python Programming-Intro to Python.pptx1. PGA2.0-Python Programming-Intro to Python.pptx
1. PGA2.0-Python Programming-Intro to Python.pptx
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
Python
PythonPython
Python
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptx
 
Python programming
Python programmingPython programming
Python programming
 
Python programming
Python  programmingPython  programming
Python programming
 
Unit -1 CAP.pptx
Unit -1 CAP.pptxUnit -1 CAP.pptx
Unit -1 CAP.pptx
 
Python_Haegl.powerpoint presentation. tx
Python_Haegl.powerpoint presentation. txPython_Haegl.powerpoint presentation. tx
Python_Haegl.powerpoint presentation. tx
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
An overview on python commands for solving the problems
An overview on python commands for solving the problemsAn overview on python commands for solving the problems
An overview on python commands for solving the problems
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

modul-python-all.pptx

  • 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.
  • 47. The elif statement (2) Syntax Flow Diagram
  • 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.
  • 50. Single Statement Suites If the suite of an if clause consists only of a single line, it may go on the same line as the header statement.
  • 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
  • 66. break Statements Syntax Flow Diagram The break statement is used for premature termination of the current loop.
  • 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.