Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
aa.gusti
/
coba
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 665b4d40
authored
Feb 24, 2025
by
aa.gusti
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
latihan
1 parent
1970af04
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
174 additions
and
0 deletions
opensipkd/coba/views/latihan.py
opensipkd/coba/views/latihan.py
0 → 100644
View file @
665b4d4
"""
A. Struktur Data dalam python
1. String
2. Integer
3. Float
4. Date
5. DateTime
menggunakan module datetime
import datetime
dt = datetme.date(year, month, day)
dt_time = datetme.datetime(year, month, day, hour, minutes, seconds.miliseconds)
Import Fungsi
>>> import datetime
>>> datetime.datetime.now()
>>> dt = datetime.date(2025,10,2)
>>> print(dt)
2025-10-02
>>> from datetime import date
>>> dt = date(2025,10,2)
>>> print(dt)
2025-10-02
Fungsi yang berhubungan dengan Date
strftime(format) konversi menjadi string yang terformat
%
d,
%
m,
%
Y
%
H
%
M
%
S
strptime("date string", format) konversi dari string ke datetime
dt_from_str = datetime.datetime.strptime('2025-08-05',"
%
Y-
%
m-
%
d")
6. List/Array
alist = [1,2,3,4,5,6]
blist = [5,6,7]
index list dimulai 0
Operasi
a. Append alist.append(7)
b. Insert alist.insert(0, 0)
c. Delete del alist[index]
alist.pop(index) menghapus dan menyimpan ke variable lain
7. Tuple
atuple = (1,2,3,4,5,6)
btuple = (5,6,7)
index tuple dimulai 0
8. Dictionary terdiri dari key dan value
dictionary = {"a": 1, "b": 2, "c": 3}
dictionary = dict(a=1,
b=2,
c=3}
Campuran:
dictionary = {"a": 1, "b": [1,2,3,4,5,6], "c": (1,2,3,4,5,6), "d":{"d1":"D1", "d2":1}}
B. Variable
C. def -> function
def kali(a, b):
return a*b
def kali1(a=1, b=2):
return a*b
def kali2(*arg):
c = 1
for i in arg:
c = c * i
return c
def kali3(a=1, b=2):
c = a*b
return c
def kali4(**kwarg):
c = 1
for kw in kwarg:
c = c * kwarg[kw]
return c
D. class -> Object
E. Framework
F. Perintah standard
type(obj) Mendafatkan type variable/object
print(obj,)
str(obj) merubah menjadi type string
int(obj) meribah menjadi type obj
float(obj) merubah type menjadi Float
dir(obj) melihat daftar fungsi/attribut dalam suatu object
hasattr(obj, name) cek object mempunyai attribute
getattr(obj, name) mengambil attribute biasanya kedalam suatu variable
"""
class
Hitung
():
def
__init__
(
self
,
a
,
b
):
self
.
a
=
a
self
.
b
=
b
self
.
kali
()
def
kali
(
self
):
c
=
self
.
a
*
self
.
b
print
(
f
"Kali: {self.a} * {self.b}={c}"
)
return
c
def
bagi
(
self
):
c
=
self
.
a
/
self
.
b
print
(
f
"Bagi: {self.a} / {self.b}={c}"
)
return
c
def
sisa
(
self
):
c
=
self
.
a
%
self
.
b
print
(
f
"Sisa: {self.a}
%
{self.b}={c}"
)
return
c
class
Hitung2
(
Hitung
):
def
tambah
(
self
):
c
=
self
.
a
+
self
.
b
print
(
f
"Tambah: {self.a} + {self.b} = {c}"
)
return
c
# Contoh Procedure
def
kali
(
a
,
b
):
return
a
*
b
def
kali1
(
a
=
1
,
b
=
2
):
return
a
*
b
def
kali2
(
*
arg
):
c
=
1
for
i
in
arg
:
c
=
c
*
i
return
c
def
kali4
(
**
kwarg
):
c
=
1
for
kw
in
kwarg
:
c
=
c
*
kwarg
[
kw
]
return
c
def
kali5
(
**
kwarg
):
c
=
1
if
"b"
in
kwarg
:
c
=
c
*
kwarg
[
"b"
]
return
c
"""
>>> kali(2, 2)
4
>>> kali2(2, 2)
4
>>> kali2(2, 2, 3, 4, 5)
240
>>> kali3()
2
>>> kali3(3, 4)
12
>>> kali3(b=3, a=5)
15
>>> kali3(3, 5, d=10, e=12)
1800
"""
def
main
():
hitung
=
Hitung2
(
5
,
5
)
print
(
hitung
.
a
,
hitung
.
b
)
if
__name__
==
"__main__"
:
main
()
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment