Python 01: Dasar-Dasar Bahasa Pemrograman Python

Memerkenalkan hal-hal dasar yang harus kalian ketahui pada bahasa pemrograman Python

Photo by Kevin Ku from Pexels
Daftar isi:
1. Algoritma dan Pemrograman
2. Pengenalan Bahasa Pemrograman Python
3. Aturan Umum Syntax Python
4. Variabel
5. Print Function
6. Input dan Output
7. Built-In Data Type
8. Konversi Tipe Data
9. Operator di Python

1. Algoritma dan Pemrograman

Tanpa algoritma tidak akan pernah ada program.

  • Input: masukan data
  • Output: menampilkan data
  • Math: operasi matematika dasar
  • Conditional execution: sebuah kondisi untuk mengatur kode yang akan dieksekusi
  • Repetition: tindakan perulangan
Fase-fase dari sebuah algoritma [2]

Contoh algoritma

Perbandingan runtime dari algoritma simple search dan binary search [3]

Visualisasi algoritma menggunakan flowchart

Sumber: https://www.smartdraw.com/flowchart/flowchart-symbols.htm.
Contoh flowchart sederhana (Gambar oleh penulis)

Kontribusi biologi di bidang algoritma

Struktur algoritma neural networks (Gambar oleh penulis)

2. Pengenalan Bahasa Pemrograman Python

Sumber: https://staging.python.org/community/logos/
  • Sintaks yang ringkas, sederhana, dan mirip seperti bahasa inggris
  • Memiliki komunitas yang besar dan aktif
  • Didukung oleh banyak library yang powerfull
  • Dapat dijalankan pada platform yang berbeda (Windows, Mac, Linux, dll).
  • Mendukung beberapa paradigma pemrograman, seperti berorientasi objek, imperatif, pemrograman fungsional, dan gaya prosedural
  • Kecepatan runtime yang lambat jika dibandingkan bahasa pemrograman lain seperti C, C++, dan Java
  • Jika sudah belajar Python, akan sulit jika ingin berpindah ke bahasa pemrograman lain
  • Memiliki konsumsi memori yang lebih banyak
  • Tidak terlalu bagus untuk mobile apps (belum ada library yang mendukung)
  • Akses database yang kurang bagus

3. Aturan Umum Syntax Python

Menggunakan baris baru untuk menyelesaikan perintah

Komentar di Python

Komentar pada Python (Gambar oleh penulis)

Indentasi di Python

Contoh penggunaan indentasi di Python (Gambar oleh penulis)

Docstring di Python

Docstring pada Python (Gambar oleh penulis)

4. Variabel

name = "Lewandowski"
age = 19
Contoh penulisan nama variabel (Gambar oleh penulis)
  • Nama variabel harus diawali dengan huruf atau underscore (_)
  • Nama variabel tidak bisa diawali dengan angka
  • Nama variabel harus singkat, jelas, dan tidak boleh terlalu panjang
  • Nama variabel peka terhadap huruf kecil dan huruf besar (case-sensitive)
  • Tidak bisa menggunakan keyword di Python untuk nama variabel (False, True, int, dll.)
  • Camel case → setiap kata dimulai dengan huruf besar kecuali kata pertama
  • Pascal case → setiap kata dimulai dengan huruf besar
  • Snake case → setiap kata dipisahkan dengan karakter underscore
Gaya penulisan nama variabel (Gambar oleh penulis)

5. Print Function

Penggunaan fungsi print (Gambar oleh penulis)

6. Input and Output

Ilustrasi proses input dan ouput (Gambar oleh penulis)
Ilustrasi alur program input dan output sederhana (Gambar oleh penulis)
# implementasi untuk input
nama = input("masukan nama: ")
umur = input("masukan umur: ")
# implementasi untuk menghasilkan output
print(f”nama: {nama} umur: {umur}”)

7. Built-In Data Type (int, float, str, bool)

Integer (bilangan bulat)

#integer
a = 35656222554887711
b = -35656222554887711
c = 0
d = 100_000_000
#bukan integer
e = 099
f = 100, 000, 000
Integer representasi oleh kelas int (Gambar oleh penulis)

Float (bilangan desimal)

#float
a = 20.5
b = 7.0
c = 10.35656222554887711
#float
a = 15e8
b = 3E89
c = -75.7e100
Float representasi oleh kelas float (Gambar oleh penulis)

String (karakter)

#string
name = 'brawijaya'
age = "twenty"
status = """died"""
year = "2002"
String representasi oleh kelas str (Gambar oleh penulis)

Boolean (benar atau salah)

#boolean
a = True
b = False
Boolean representasi oleh kelas bool (Gambar oleh penulis)

8. Konversi Tipe Data

Konversi tipe data ke integer

Sebelum diubah ke tipe data integer (Gambar oleh penulis)
Setelah diubah ke tipe data integer (Gambar oleh penulis)
Error ketika mengubah string karakter menjadi integer (Gambar oleh penulis)

Konversi tipe data ke float

Sebelum diubah ke tipe data float (Gambar oleh penulis).
Setelah diubah ke tipe data float (Gambar oleh penulis)
Error ketika mengubah string karakter menjadi float (Gambar oleh penulis)

konversi tipe data ke string

Sebelum diubah ke tipe data string (Gambar oleh penulis)
Setelah diubah ke tipe data string (Gambar oleh penulis)

Konversi tipe data ke boolean

Sebelum diubah ke tipe data boolean (Gambar oleh penulis)
Setelah diubah ke tipe data boolean (Gambar oleh penulis)

9. Operator di Python

Operator Aritmatika

Operator aritmatika [8].
>>> 10 + 10
20
>>> 7 - 5
2
>>> 5 * 5
25
>>> 10 - 9.5
0.5
>>> 10 + 5.0
15.0
>>> 50 / 20
2.5
>>> 50 // 20
2
>>> 4 ** 2
16
>>> 4 ^ 2
6
>>> 15 % 5
0
>>> 10 % 7
3
>>> (25 - 10 + 5) ** 2
400

Operator Perbandingan

>>> 10 == 10
True
>>> 7 != 10
True
>>> 20 < 15
False
>>> 20 >= 30
False
>>> 5 == 'five'
False
>>> 5 <= 'five'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<=' not supported between instances of 'int' and 'str'
>>> 6 > 3 > 10
False
>>> 5 > 10 > 3
False

Operator Logika

>>> x = True
>>> y = False
>>> x and Y
False
>>> x or y
True
>>> not x
False
>>> 5 > 10 and 10 == 10
False
>>> (True and False) or (5 < 10)
True

Operator Bitwise

#0 | 1 = 1 
#1 | 0 = 1
#1 | 1 = 1
#0 | 0 = 0
>>> x = 2 #00000010
>>> y = 5 #00000101
>>> x | y
7 #00000111
#1 & 1 = 1
#0 & 0 = 0
#0 & 1 = 0
#1 & 0 = 0
>>> x = 3 #00000011
>>> y = 6 #00000110
>>> x & y
2 #00000010
Inverse point in ~ operator (Image by author)
>>> ~0 #00000000
-1 #-00000001
>>> ~1 #00000001
-2 #-00000010
#0 ^ 0 = 0
#1 ^ 1 = 0
#0 ^ 1 = 1
#1 ^ 0 = 1
>>> x = 4 #00000100
>>> y = 8 #00001000
>>> x ^ y
12 #00001100
>>> x = 4 #00000100
>>> x << 2
16
>>> y = 9 #00001001
>>> y << 2
2 #00000010

Operator Assignment

>>> count = 100
>>> count
100
>>> x = 10
>>> x += 5 #same as x = x + 5
>>> x
15
>>> y = 3
>>> y *= x
>>> y
45

Operator Membership

>>> letters = ['a', 'i', 'u', 'e', 'o']
>>> 'u' in letters
True
>>> 'a' not in letters
False
>>> num = 45
>>> 5 in num
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'int' is not iterable

Identity operators

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> c = a
>>> a is b
False
>>> a is c
True
>>> a is not b
True

Daftar Pustaka:

--

--

A place to share and learn about anything related to Data Science curated by Data Science Indonesia members for Data Science People.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dede Kurniawan

A writer who focuses on the topics of Python, Statistics, Machine Learning, and Deep Learning. LinkedIn: https://www.linkedin.com/in/dede-kurniawann/