Terus berlanjut, kita sampai ke Module 5 dari Alison Course dan pelajaran ke 7 (video ke 7/24) dari Dave Gray.
Module 5, Chapter 1: Modules, Packages, Strings, and Exceptions | Module & Package | Alison
Module & Package
Module:
- a module is a file containing Python code. It can define functions, classes, and variables, and may also include runnable code.
- If you want to create a module, simply create a
.py
file with your desired code and import it into your program using the import
statement.Module for string
# A module for string operations
def reverseString(string):
return string[::-1]
Import string
import strings
print(strings.reverseString('hello'))
Run Python File:
olleh
Which of these is used to call a module from another file into your program? Choose one.
- using the import function
---------------------------------------------
Module 5, Chapter 2: Modules, Packages, Strings, & Exceptions | The Guessing Game | Alison
Guessing Game
import random
score = 10
randomNumber = random.randint(1,10)
while True:
userNumberInput = int(input('Guess : '))
if userNumberInput == randomNumber:
print("congratulations your guessed right! your score is " + str(score))
break
else:
print('better luck next time')
score -= 1
Run Python File:
Guess : 1
better luck next time
Guess : 2
better luck next time
Guess : 3
congratulations your guessed right! your score is 8
What keyword is used to close operations in Python exceptions? Choose one.
- Finally keyword
------------------------------------------
Module 5, Chapter 3: Module, Packages, Strings, and Exceptions | Exceptions/Errors | Alison
Exceptions/Error
3 errors in Python:
1-Logic
2-Run Time
3-Compiling
------------------------------------------
Module 5, Chapter 4: Modules, Packages, Strings, and Exceptions | Errors Practice | Alison
Errors Practices
Coding dengan hasil benar
a = int(input('one : '))
b = int(input('two : '))
print(a + b)
Run Python File:
one : 1
two : 8
9
Sekarang, hasilnya salah, error
a = int(input('one : '))
b = int(input('two : '))
print(a + b)
Run Python File:
one : 1
two : b ...> error
ValueError: invalid literal for int() with base 10: 'b'
Error Correction
try:
a = int(input('one : '))
b = int(input('one : '))
print(a + b)
except ValueError:
print('Invalid Input')
Run Python File:
one : 1
one : b
Invalid Input
True or False: ZeroDivisionError is a type of error in Python.
- True
---------------------------------------------------
Module 5, Chapter 5: Modules, Packages, Strings, & Exceptions | String Operations | Alison
String Operations
>>> string = 'string'
>>> string.islower()
True
>>> string = 'STRING'
>>> string.islower()
False
>>> string = 'STRING'
>>> string.isupper()
True
>>> string = '29'
>>> string.isdigit()
True
>>> string = '29'
>>> string.isdigit()
True
>>> string.replace('2', 'Hello')
'Hello9'
a = 'ABC123DEF456'
def answer(string):
alphabets = ''
result = 0
for char in string:
if char.isdigit():
result += int(char)
else:
alphabets += char
return (alphabets, result)
print(answer(a))
Run Python File:
('ABCDEF', 21)
Which of these are examples of string operations in Python? Choose three.
- 'e' in string
- string.isdigit()
- string.swapcase()
The followings are the key points from this module:
A module is a single file consisting of Python code that can be imported into your program.
WHILE:
A package is basically a directory with Python files (i.e modules) and also can be imported into your program as well.
The Import function is used to import modules from another file into your code.
The following are the types of exceptions/errors mentioned:
- Compiling errors -- These are errors thrown when a program is being compiled or interpreted. e.g Extra space, function misspelling, missing colon or parentheses, etc.
- Logical errors -- These are errors in your logic. e.g Calculating and getting a wrong answer.
- Run-time errors -- This happens when something is done incorrectly on the part of the user. e.g User entering a string value when the application can only receive integers.
Some possible errors that you can come across in Python include
- ValueError
- ZeroDivisionError
- TypeError. etc.
The following are string operations that one can run on string values in Python:
- string.islower -- To check if a string in lower case.
- string.isupper -- To check if a string in upper case.
- string.isdigit -- To check availability of digits in strings.
- string.lower -- To change a string from upper to lower case.
- string.upper -- To change a string from lower to upper case.
- string.swapcase -- To swap the cases of the strings.
- 'e' in string -- Searches for the character "e" in strings.
Having completed this module, you will be able to:
- Discuss file handling and how to write to different types of files.
- Explain the basis of classes and subclasses.
- Identify the different types of variables and methods.
- Explain the concept of polymorphism.
Lanjutkan ke Module 6:
=======================================
=========================================
Dave Gray
7/24: Dictionaries and Set: Python Dictionaries and Sets for Beginners | Python tutorial - YouTube
Kita akan belajar dictionaries dan set.
Dictionaries
band = {
"vocals": "Plant",
"guitar": "Page"
}
band2 = dict(vocals="Plant", guitar="Page")
print(band)
print(band2)
Run Python File:
{'vocals': 'Plant', 'guitar':
'Page'}
{'vocals': 'Plant', 'guitar':
'Page'}
Print(type(band))
Run Python File:
<class 'dict'>
Sets
# sets
nums = {1, 2, 3, 4}
nums2 = set((1,2,3,4))
print(nums)
print(nums2)
print(type(nums))
print(len(nums))
Run Python File:
{1, 2, 3, 4}
{1, 2, 3, 4}
<class 'set'> ...Type data kita adalah 'set'
4 ....> ada 4 elemen di data kita (1, 2, 3, 4)
Add element to set
nums = {1, 2, 3, 4}
nums2 = set((1,2,3,4))
print(nums)
print(nums2)
print(type(nums))
print(len(nums))
# add element to set
nums.add(5)
print(nums)
Run Python File:
{1, 2, 3, 4, 5}
-------------------------------------------------------
Dave Gray (8/24): Python While Loops & For Loops | Python tutorial for Beginners - YouTube
Loops:
1) while
2) for
while, value += 1
value = 1
while value < 10:
print(value)
value += 1
Run Python File:
1
2
3
4
5
6
7
8
9
If value == 5, then break
value = 1
while value < 10:
print(value)
if value == 5:
break
value += 1
Run Python File:
1
2
3
4
5
we change position, and use continue and else
value = 1
while value <= 10:
value += 1
if value == 5:
continue
print(value)
else:
print("value is now equal to " + str(value))
Run Python File:
2
3
4
6
7
8
9
10
11
value is now equal to 11
for loop
names = ["Dave","Sara","John"]
for x in names:
print(x)
Run Python File:
Dave
Sara
Johnx
for x in "Florida"
for x in "Florida":
print(x)
Run Python File:
F
l
o
r
i
d
a
break
names = ["Dave","Sara","John"]
for x in names:
if x == "Sara":
break
print(x)
Run Python File:
Dave
Continue
names = ["Dave","Sara","John"]
for x in names:
if x == "Sara":
continue
print(x)
Run Python File:
Dave
John
Untuk game (rps.py) bisa dilihat di video 5: Python User Input & Control Flow | Python tutorial - YouTube
Pelajaran selanjutnya (9/24), Python Function: Python Functions for Beginners | Python tutorial - YouTube
JJJJJJJJJJ
No comments:
Post a Comment