Algo Pekan 04
Algo Pekan 04
Muhamad Sabar
Informatics Engineering
Sekolah Tinggi Teknologi Bandung
2019
Python
Python is a popular programming
language. It was created by Guido van
Rossum, and released in 1991. It is used
for:
1. web development (server-side),
2. software development,
3. mathematics,
4. system scripting.
What Pyhton can do?
1. Python can be used on a server to create web applications.
2. Python can be used alongside software to create workflows.
3. Python can connect to database systems. It can also read and
modify files.
4. Python can be used to handle big data and perform complex
mathematics.
5. Python can be used for rapid prototyping, or for production-ready
software development.
Python Quick Start
if 5 > 2:
Python will give you an error if you
print("Five is greater than two!") skip the indentation
Python Indentation
The number of spaces is up to you as a programmer, but it has to be at
least one.
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Python Variables
Variables are containers for storing data values. Unlike other
programming languages, Python has no command for declaring a
variable. A variable is created the moment you first assign a value to it.
x=5
y = "John"
print(x)
print(y)
Python Variables
Variables do not need to be declared with any particular type and can
even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
And you can assign the same value to multiple variables in one line:
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables
The Python print statement is often used to output variables. To combine
both text and a variable, Python uses the + character:
x = "awesome"
print("Python is " + x)
You can also use the + character to add a variable to another variable:
x = "Python is "
y = "awesome"
z= x+y
print(z)
Global 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:
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Global Variables
If you create a variable 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
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
Normally, when you create a variable
The global Keyword inside a function, that variable is
local, and can only be used inside
that function. To create a global
variable inside a function, you can
use the global keyword.
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
The global Keyword
Also, use the global keyword if you want to change a global variable
inside a function.
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Built-in Data Types
In programming, data type is an
important concept. Variables can
store data of different types, and
different types can do different
things. Python has the following
data types built-in by default, in
these categories:
Getting the Data Type
You can get the data type of any object by using the type() function:
x=5
print(type(x))
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
x = frozenset({"apple", "banana", frozenset
"cherry"})
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
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 = range(6) range
x = dict(name="John", age=36) dict
x = set(("apple", "banana", "cherry")) set