0% found this document useful (0 votes)
10 views23 pages

Algo Pekan 04

The document discusses Python programming language. It covers Python basics like variables, data types, functions, comments. It also discusses Python features like indentation, strings, numbers, lists, dictionaries etc. and how to use them.

Uploaded by

Ndi Rabbni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Algo Pekan 04

The document discusses Python programming language. It covers Python basics like variables, data types, functions, comments. It also discusses Python features like indentation, strings, numbers, lists, dictionaries etc. and how to use them.

Uploaded by

Ndi Rabbni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Algorithm and Programming 1

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

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.
print("Hello, World!")
Python Indentation
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.
if 5 > 2:
print("Five is greater than two!")

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)

String variables can be declared either by using single or double quotes


x = "John"
# is the same as
x = 'John'
Python 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:
1. A variable name must start with a letter or the underscore
character
2. A variable name cannot start with a number
3. A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
4. Variable names are case-sensitive (age, Age and AGE are three
different variables)
Assign Value to Multiple Variables
Python allows you to assign values to multiple variables in one line:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

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 = tuple(("apple", "banana", "cherry")) tuple

x = range(6) range
x = dict(name="John", age=36) dict
x = set(("apple", "banana", "cherry")) set

x = frozenset(("apple", "banana", frozenset


"cherry"))
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
Comments
Python has commenting capability for the purpose of in-code
documentation. Comments start with a #, and Python will render the
rest of the line as a comment:
#This is a comment.
print("Hello, World!")

print("Hello, World!") #This is a comment


Comments
""" #This is a comment
This is a comment #written in
written in #more than just one line
more than just one line print("Hello, World!")
"""
print("Hello, World!")
#print("Hello, World!")
print("Cheers, Mate!")
Quiz
Preparing your devices
Thanks

You might also like