Friday, May 10, 2024

Python Beginner - Module 3 - Dave & Alison 01

 Kita sudah membuat summary untuk Module 1 dan 2 dari Alison Course dan video 1, 2, 3 dan 4 dari Dave Gray.


Image of Python for Beginners (credit to Google)


Boolean
True: 1
False: 0

and
0   0 ...> 0
0   1 ...> 0
1   0 ...> 0
1   1 ...> 1

or
0   0 ...> 0
0   1 ...> 1
1   0 ...> 1
1   1 ...> 1

XOR ---> Exclusive OR
0   0 ...> 0
0   1 ...> 1
1   0 ...> 1
1   1 ...> 0

True or False: Boolean variable values can only be either true or false.
- True
----------------------------------------------

Module 3 - Chapter 2

Ini adalah chapter tentang "conditions" di Python.

== ...> equal to
!= ...> not equal to
>...> greater than
< .... less than
>= ... greater than equal to

minMarks = 30
studentMarks = float(input('enter your mark: '))
 
if studentMarks > minMarks:
    print("you are eligible")
else:
  print("you are not eligible")
Run Python File:
enter your mark: 40
you are eligible

enter your mark: 12
you are not eligible


elif studentMarks >= 25:
minMarks = 30
studentMarks = float(input('enter your mark: '))
 
if studentMarks > minMarks:
    print("you are eligible")
elif studentMarks >= 25:
   print("you have been put in waiting list")25
else:
  print("you are not eligible")
Run Python File:
enter your mark: 25
you have been put in waiting list

enter your mark: 31
you are eligible

urutan:
if
elif
else

Which of these are conditional operators in Python? Choose three.
- equal to
- greater than
- less or equal to
----------------------------------------------

Module 3, Chapter 3

Ada dua type Loops:
1) Iterative
- doing over and over or repetition.
- for

2) Conditional
- while

# Iteration / for loop
for i in 'string':
    print(i)

# conditional / while loop
i = 1
while i <= 50:
    print(i)
    i += 1 # i = i + 1
Run Python File:
s
t
r
i
n
g
1
2
3
.
.
.
49
50

Which of these is used to denote a list in Python? Choose one.
- [ ]
-------------------------------------------------------

Module 3, Chapter 4

List adalah connection of similar objects

print(fruits)
fruits = [
    'apple', # 0
    'banana', # 1
    'pear', # 2
    'strawberries', # 3
]
print(fruits)
Run Python File:
['apple', 'banana', 'pear', 'strawberries']

print(fruits[2])
fruits = [
    'apple', # 0
    'banana', # 1
    'pear', # 2
    'strawberries', # 3
]
print(fruits[2])
Run Python File:
pear

print(fruits[:2])
fruits = [
    'apple', # 0
    'banana', # 1
    'pear', # 2
    'strawberries', # 3
]
print(fruits[:2])
Run Python File:
['apple', 'banana']
-----------------------------------------

Module 3, Chapter 5

Mari kita mempelajari List operation

We can change value from 'd' to 'c'
characters = ['a', 'b', 'd', 'd']
print(characters)
characters [2] = 'c'
print(characters)
Run Python File:
['a', 'b', 'd', 'd']...> there are two 'd'

['a', 'b', 'c', 'd']...> 'd' is replaced by 'c'

>>> characters = ['a', 'b', 'd', 'd']
>>> len(characters)
4

>>> characters.append('f')
>>> characters
['a', 'b', 'd', 'd', 'f']
>>> characters.remove('f')       
>>> characters
['a', 'b', 'd', 'd']

['a', 'b', 'd', 'd']
>>> characters.pop(3)
'd'

>>> 'b' in characters
True
>>> 'z' in characters
False

Which of these is not one of the list operations in Python? Drag the correct answer into the space provided.
- characters.insert()
-----------------------------------------------------------------------

Module 3, Chapter 6
Bubble Sort:

Bubble sort adalah simple sorting algorithm

list = [8, 3, 1, 4]
print("Original list:", list)

length = len(list)
for i in range(length):
    for j in range(0, length - i - 1):
        if list[j] > list[j + 1]:
            list[j], list[j + 1] = list[j + 1], list[j]

print("Sorted list (ascending order):", list)
Run Python File:
Original list: [8, 3, 1, 4]
Sorted list (ascending order): [1, 3, 4, 8]
-----------------------------------------------

Module 3, chapter 7

Beberapa operators akan kita pelajari
>>> a = 12
>>> if (a > 6) and (a < 20):
...     print(a)
... 
12

Q1: cari binary angka 13
- bagi angka 13 dengan 2: 13/2= 6 ...> reminder 1....> first binary.
- then, 6/2 = 3 ...> reminder 0 ...> second binary
- then, 3/2 = 1 ...> reminder 1 ...> third binary
- kemudian, 1/2 = 0 ...> `reminder 1 ...> fourth binary
Untuk mengetahui binary angka 13, dilihat dari bawah (belakang) atau dari langkah ke 4 (fourth binary): 1101

>>> bin(13)
'0b1101' ...> 1101


Q2: cari angka berapa dari binary 0b1100 atau 1100?
- mulai dari sebelah kanan, yaitu 0
- decimal equivalent 0 = 0 x 2^0 = 0
-  decimal equivalent 0 = 0 x 2^1 = 0
decimal equivalent 1 = 1 x 2^2 = 4
decimal equivalent 1 = 1 x 2^3 = 8
Then, 0 + 0 + 4 + 8 = 12

Q3: 12|13 ...> 12 or 13
binary 12 = 1100
binary 13 = 1101
-------------------------------
                = 1101
Convert to value from right: 1(2*0) + 0(2*1) + 1(2*2) + 1(2*3) = 1+0+4+8 = 13
proved by VSC: 
>>> 12|13
13

Q4, Now find 12&13
binary 12 = 1100
binary 13 = 1101
--------------------------
                = 1100
convert to value from right: 0(2*0) + 0(2*1) + 1(2*2) + 1(2*3) = 0 + 0 + 4 + 8 =12
proved by VSC:
>>> 12 & 13
12

Q5, find 12 xor 13
>>> 12 ^ 13
1

True or False: A bubble sort consists of three types of loop.
- False
- True: one type of loop.
 
The followings are the key points from this module:
Boolean logic refers to the idea that all values are either true or false. 

Some boolean operations that were examined include:
and
or
Xor

Some conditional operators that were mentioned include:
Equals to which is written as ==. 
Not equals to which is written as !=. 
Greater than and Less than which are written as > and < respectively. 
Greater than or equals to which is written as >=. 
Less than or equals to which is written as <=. 

The following are the two types of Loops examined in this module: 
Iterative loop (For loop). 
Conditional loop (While loop). 

Lists are collections of similar objects and are denoted with "[  ]". 

The different operations that can be run on lists include: 
Changing character(s) of a list. 
Len (characters) -- To check the length of a list. 
character.append ('e') -- To add the character "e" to the end of a list.
character.reverse -- To reverse the order of characters in a list.

Bubble Sort refers to a simple algorithm that goes over an array and sorts it into the correct order. 



==============================================
==============================================

Dave Gray

Lesson 5/24


Kita akan mempelajari user input dan control flow

import random

print("Rock, Paper, Scissors Game")
print("1 for Rock, 2 for Paper, or 3 for Scissors")

playerchoice = input("Enter your choice (1/2/3): ")
player = int(playerchoice)

if player < 1 or player > 3:
    sys.exit("You must enter 1, 2, or 3.")

computerchoice = random.choice(["1", "2", "3"])
computer = int(computerchoice)

print("\nYou chose:", playerchoice)
print("Python chose:", computerchoice)
Run Start Debugging:

Rock, Paper, Scissors Game
1 for Rock, 2 for Paper, or 3 for Scissors
Enter your choice (1/2/3): 1

You chose: 1
Python chose: 3







JJJJJJJJJJJJJJJJJJ


JJJJJJJJJJJJJJJJJJ

No comments:

Post a Comment