Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Pertemuan 3

Fungsi, Operasi Aritmetika, Tipe Data, dan List Data


Fungsi

• Dalam python, fungsi juga merupakan obyek. Fungsi adalah obyek yang
mewakili coding yang bertujuan untuk melaksanakan tugas atau
perintah tertentu.
Python Functions Math Library
Method Description Method Description
math.acos() Returns the arc cosine of a number math.gamma() Returns the gamma function at x
math.acosh() Returns the inverse hyperbolic cosine of a number math.gcd() Returns the greatest common divisor of two integers
math.asin() Returns the arc sine of a number math.hypot() Returns the Euclidean norm
math.asinh() Returns the inverse hyperbolic sine of a number math.isclose() Checks whether two values are close to each other, or not
math.atan() math.isfinite() Checks whether a number is finite or not
Returns the arc tangent of a number in radians
math.atan2() math.isinf() Checks whether a number is infinite or not
Returns the arc tangent of y/x in radians
math.atanh() math.isnan() Checks whether a value is NaN (not a number) or not
Returns the inverse hyperbolic tangent of a number
math.ceil() math.isqrt() Rounds a square root number downwards to the nearest integer
Rounds a number up to the nearest integer
math.comb() math.ldexp() Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i
Returns the number of ways to choose k items from n items without repetition
and order math.lgamma() Returns the log gamma value of x
math.copysign() Returns a float consisting of the value of the first parameter and the sign of the math.log() Returns the natural logarithm of a number, or the logarithm of number to base
second parameter
math.log10() Returns the base-10 logarithm of x
math.cos() Returns the cosine of a number
math.log1p() Returns the natural logarithm of 1+x
math.cosh() Returns the hyperbolic cosine of a number
math.log2() Returns the base-2 logarithm of x
math.degrees() Converts an angle from radians to degrees
math.perm() Returns the number of ways to choose k items from n items with order and without repetition
math.dist() Returns the Euclidean distance between two points (p and q), where p and q are
the coordinates of that point
math.pow() Returns the value of x to the power of y
math.erf() Returns the error function of a number
math.prod() Returns the product of all the elements in an iterable
math.erfc() Returns the complementary error function of a number
math.radians() Converts a degree value into radians
math.exp() Returns E raised to the power of x
math.remainder() Returns the closest value that can make numerator completely divisible by the denominator
math.expm1() Returns Ex - 1
math.fabs() Returns the absolute value of a number math.sin() Returns the sine of a number
math.factorial() Returns the factorial of a number math.sinh() Returns the hyperbolic sine of a number
math.floor() Rounds a number down to the nearest integer math.sqrt() Returns the square root of a number
math.fmod() Returns the remainder of x/y math.tan() Returns the tangent of a number
math.frexp() Returns the mantissa and the exponent, of a specified number math.tanh() Returns the hyperbolic tangent of a number
math.fsum() Returns the sum of all items in any iterable (tuples, arrays, lists, etc.) math.trunc() Returns the truncated integer parts of a number
Latihan

• Bulatkan ke atas: 80.3, 27.8, dan 30.9 (math.ceil)


• Bulatkan ke bawah: 80.3, 27.8, dan 30.9 (math.floor)
• Kombinasi 5 dari 8 (math.comb)
• Permutasi 5 dari 8 (math.perm)
• Berapa sisa 100 dibagi 6 (math.fmod)
• Diketahui data d = [10,10,15,15,20,20,10], berapa jumlah seluruh data d
(math.fsum)
Operator Aritmatika
• Operator aritmatika adalah operator yang digunakan untuk melaksanakan perhitungan
aritmatika.
Operator khusus Python
Operator Example Same As
= x=5 x=5
Operator Aritmatika
+= x += 3 x=x+3
Operator Name Example
-= x -= 3 x=x-3
+ Addition x+y
*= x *= 3 x=x*3
- Subtraction x-y /= x /= 3 x=x/3
* Multiplication x*y %= x %= 3 x=x%3

/ //= x //= 3 x = x // 3
Division x/y
**= x **= 3 x = x ** 3
% Modulus x%y
&= x &= 3 x=x&3
** Exponentiation x ** y |= x |= 3 x=x|3
// Floor division x // y ^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


Latihan
Mengubah Tipe Data

• Mengubah tipe data menjadi penting jika data yang dimasukkan berasal
dari data yang lebih general seperti alfanumerik misalnya. Sehingga
untuk dapat dilakukan operasi aritmatika perlu dilakukan perubahan
tipe data.
Memasukkan Data Via Keyboard
• Untuk lebih interaktif, suatu program atau aplikasi harus dapat berinteraksi dengan
pengguna secara langsung. Data yang dimasukkan dapat berubah sehingga lebih
bermanfaat secara realtime bagi user.
• Untuk dapat menyediakan menu input bagi user dapat dilakukan dengan perintah =
input([prompt])
Membuat List
List adalah set atau sekumpulan data dalam satu variabel
Indeks pada Python
Indeks pada python dimulai dari 0 sampai ke n-1 dimana n adalah jumlah data
Mengakses List
Print item kedua dari kiri Print item empat dari kanan kecuali item pertama dari kanan

Print n item dari kiri, dan item setelah item ke n dari kiri Mengecek item ada di dalam list atau tidak
Modifikasi List
Mengganti item ke-4 dalam list Menambahkan item di urutan ke-3

Mengapus item secara spesifik


Mengganti item ke-2 sampai ke-3 dalam list

Mengganti item ke-2 menjadi 2 item baru

Mengapus item berdasarkan indeks


Loop List
Instruksi yang diberikan kepada komputer agar ia mau melakukan sesuatu entah itu
memproses data, menampilkan data, atau yang lainnya secara berulang

Looping atau pengulangan seluruh item, satu per satu Looping atau pengulangan menggunakan while loop

Looping atau pengulangan seluruh item, dengan indeks item


Membuat List baru dengan syarat
tertentu
Membuat list baru dengan item dalam list berasal dari Membuat list baru dari list lama dengan kondisi selain
list lama dengan syarat ada huruf a nya item tertentu

Membuat list baru dengan kondisi pada rentang


Membuat list baru dengan item dalam list berasal dari tertentu (angka).
list lama dengan syarat ada huruf i nya

Membuat list baru dari list lama dengan item huruf


besar semua
Membuat List Baru dari Data
Lama
Membuat list baru mengubah seluruh item

Membuat list baru mengubah 1 item menjadi item lain


Mengurutkan List (Sort)
Sorting list berisi item alphanumerical Sort data berdasarkan dekat dengan
angka tertentu (angka 70)

Sorting list berisi item numeric

Sorting list descending


Copy List

• Mengkopi list tidak bisa dengan menggunakan rumus data2 = data1,


karena jika seperti ini perubahan pada data1 akan mengubah juga
data2.
Mengkopi list sama dengan list baru Dengan ada copy (data2), maka modifikasi di data1 atau
data2 tidak akan mempengaruhi yang lain
Menggabungkan List
Method Description
Menggabungkan dua list menjadi list baru append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
Menambah suatu list tertentu dengan list lain. List tertentu insert() Adds an element at the specified position
tersebut bertambah datanya bukan menjadi list baru pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Tugas
1 4

5
2

3 6
Tugas (Lanjutan)
7

You might also like