Statement If dengan Operator or Untuk Multiple Conditions di Python

Statement If dengan Operator or Untuk Multiple Conditions di Python

WritingSkills.web.id - Program berikut ini menggunakan operator or yang digunakan untuk conditions alternatif (alternative conditions), seperti mengaktifkan tombol "Apply" bila setidaknya salah satu dari kedua condition berikut terpenuhi:
Contoh Program Menggunakan Statement If dengan Operator or di Python
Contoh Program Menggunakan Statement If dengan Operator or di Python

Operator or digunakan untuk menjalankan blok kode bila salah satu dari conditions yang ada memiliki nilai True. Contohnya adalah barisan kode berikut:
nilai_rata_rata = "A"
skor_akhir = 1400

if nilai_rata_rata == "A" or skor_akhir >= 1300:
  print("Sertifikat diperoleh!")
  
Output dari kode di atas adalah:
Sertifikat diperoleh!
Contoh Statement If dengan Operator or di Python

Dengan operator or, eksekusi blok kode akan dilewatkan hanya bila semua conditions bernilai False, seperti pada kode nilai_rata_rata = "A" dan skor_akhir = 1500.
nilai_rata_rata = "B"
skor_akhir = 1400

if nilai_rata_rata == "A" or skor_akhir >= 1500:
  print("Sertifikat diperoleh!")
  
Sedangkan pada barisan kode berikut, skor_akhir >= 1500 bernilai False, tetapi blok kode tetap dieksekusi karena nilai_rata_rata == "A" bernilai True
nilai_rata_rata = "A"
skor_akhir = 1400

if nilai_rata_rata == "A" or skor_akhir >= 1500:
  print("Sertifikat diperoleh!")
  
Output dari kode di atas adalah:
Sertifikat diperoleh!
Contoh Statement If dengan Operator or di Python

Kita juga bisa menggunakan operator or untuk menambahkan condition sebanyak yang kita inginkan, seperti menambahkan variabel menang_kompetisi.
nilai_rata_rata = "B"
skor_akhir = 1400
menang_kompetisi = True

if nilai_rata_rata == "A" or skor_akhir >= 1500 or menang_kompetisi :
  print("Sertifikat diperoleh!")
  
Output dari kode di atas adalah:
Sertifikat diperoleh!
Contoh Statement If dengan Operator or di Python

Dari contoh di atas, dapat diketahui kalau operator or dalam statement if berfungsi untuk menggabungkan kondisi-kondisi alternatif.

Contoh-contoh lainnya adalah sebagai berikut:

Contoh 1:
akhir_pekan = False
sedang_liburan = True

if akhir_pekan or sedang_liburan :
  print("Pergi jalan-jalan")
  
Output dari kode di atas adalah:
Pergi jalan-jalan
Contoh Statement If dengan Operator or di Python

Contoh 2:
mobile_internet = False
wifi = False

if mobile_internet or wifi :
  print("Memuat kotak masuk ...")
  
Blok kode tidak dieksekusi karena semua variabel memiliki nilai False. 

Contoh 3:
skor_tertinggi = 100
skor = 70
level = 5

if skor > skor_tertinggi or level == 5 :
  print("Kamu menang!")
  
Output dari kode di atas adalah:
Kamu menang!
Contoh Statement If dengan Operator or di Python

Cek tutorial sebelumnya untuk penjelasan tentang operator and, yaitu Statement If dengan Operator and Untuk Multiple Conditions di Python.

COMMENTS

Nama

American British English Differences,3,Belajar C#,6,Belajar Coding,50,Belajar HTML,11,Belajar JavaScript,9,Belajar PHP,1,Belajar Python,30,Business Letter,21,Business Letter Style,8,Business Letter Style Organization,3,Business Letter Style Outlook,2,Business Letter Style Tone,3,Business Memo,3,Chord Gitar,2,Dasar-Dasar C#,6,Dasar-Dasar HTML,11,Dasar-Dasar JavaScript,7,Dasar-Dasar PHP,1,Download,1,English Grammar,13,French Tutorial,3,Info Unik,12,Internet,2,Layout Formats,2,Letter Examples,6,List of Expressions,13,Main Gitar,9,Memo Examples,3,MS. Word Tutorial,4,Operator Perbandingan Python,4,Proyek JavaScript,2,Proyek Python,9,Psikotes,1,Redundancy,2,Sample of Letters,1,Statement if Python,6,While Loops Python,3,Writing Tutorial,25,
ltr
item
Writing Skills: Statement If dengan Operator or Untuk Multiple Conditions di Python
Statement If dengan Operator or Untuk Multiple Conditions di Python
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV5R42tYPyAFtOcJKs4TtEaEz-ldcDQTB0m2XghuN2UiDF98AFP3ftH6fOfK7hNcyTB0p2DrmfpwR7pOuJ55QvNhVU6OOO4HLS3gYWvbz8Vl91H520wdzgE8EdQBEDSPJtvDBHYlluS0XuYjpqBzaYQdiiPHPE9yjbdWaLSGyg2kOW7PGrlSTcOZa6sdhO/w640-h320/Statement-if-Operator-or-Multiple-Conditions-Python.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV5R42tYPyAFtOcJKs4TtEaEz-ldcDQTB0m2XghuN2UiDF98AFP3ftH6fOfK7hNcyTB0p2DrmfpwR7pOuJ55QvNhVU6OOO4HLS3gYWvbz8Vl91H520wdzgE8EdQBEDSPJtvDBHYlluS0XuYjpqBzaYQdiiPHPE9yjbdWaLSGyg2kOW7PGrlSTcOZa6sdhO/s72-w640-c-h320/Statement-if-Operator-or-Multiple-Conditions-Python.jpg
Writing Skills
https://www.writingskills.web.id/2023/08/statement-if-or-multiple-condition.html
https://www.writingskills.web.id/
https://www.writingskills.web.id/
https://www.writingskills.web.id/2023/08/statement-if-or-multiple-condition.html
true
3599370990183318253
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content