JJJJJJJJJJJJJJJJJJJJ
Friday, May 24, 2024
Diploma Python Programming - Module 02 - Alison 03
JJJJJJJJJJJJJJJJJJJJ
Thursday, May 23, 2024
7 Fungsi List yang Harus Dikuasai Python Programmers
my_list = [1, 2, 3, "hello", "rice", "code", 1.2]
count = 0
for i in my_list:
count += 1
The append() function adds a new item to the end of your list. A good use of this is to add an item to your list after your code fulfills a certain condition. Here's a simple example:
my_list = [1, 2, 3, "hello", "rice", "code", 1.2]
question = input("Do you love Python?: ").lower()
if question == "yes":
my_list.append("Python is the best!!") # use of the append() function
else:
my_list.append("You should try Python") # use of the append() function
print(my_list)
This example uses an if statement to add a certain sentence to the initial list based on the user's input.
The append() function can add only one item at a time to your list. Instead of the append function, you can use an operator instead:
my_list = [1, 2, 3, "hello", "rice", "code", 1.2]
my_list += ["Python is the best!!"]
Using the addition operator will ultimately be less efficient because it doesn't modify your initial list. Instead, it creates a new list in memory and adds a new item to it. The append() function modifies your initial list directly.
3) The extend() Function
The extend() function is a built-in function that adds several items to an existing list at once. It takes in the new items as an argument and modifies your existing list with the argument. Here's how to use the extend() function:
my_list.extend(["item", "muo", 350])
print(my_list)
# prints [1, 2, 3, 'hello', 'rice', 'code', 1.2, 'item', 'muo', 350]
The extend() function can only take one argument, so you should add all your items to a list like the code above.
4. The reverse() Function
The reverse function simply rewrites your list in the reverse order. Here's an example of the reverse function in use:
my_list = [1, 2, 3, "hello", "rice", "code"]
my_list.reverse()
print(my_list) # prints ['code', 'rice', 'hello', 3, 2, 1]
To reverse a list without using the reverse() function, you would need to slice your list. Here's an example:
my_list = [1, 2, 3, "hello", "rice", "code"]
reversed_list = my_list[::-1]
print(reversed_list) # prints ['code', 'rice', 'hello', 3, 2, 1]
In the above example, my_list[::-1] creates a reversed copy of the original list. This means you'll have two lists in memory, which is not the best approach if you simply want to reverse the original list.
JJJJJJJ
Wednesday, May 22, 2024
Python Beginner - Module 6 - Dave & Alison 01
write file
- Instance variable.
- Static variable.
JJJJJJJJJJJJJJJJJJJJJJ
Wednesday, May 15, 2024
Diploma Python Programming - Module 01 - Alison 03
Lesson Summary:
The key points from this module are:
- The Basic principle of programming is that it is problem solving.
- Python is one of the easier programming languages to learn, and many of its tools are free to download and use.
- One you learn how to code in one programming language it will be easier to learn other programming languages, as the principles are the same.
- There are a lot of different variations of Python, IronPython, Ipython, CPython to name a few. Most of python’s variations only have some syntax differences but not too many.
- Comments are very useful in your code to help you or someone else understand:- What your program does,- What a particular line or section of code does;- Why you chose to do something a particular way;- Anything that might be helpful to know if you or someone else looking at the code later and trying to understand it.
- The # is the symbol to indicate using comments in python.
After completing this module, you will be able to:
- Identify how to use the print() function to display text.
- Recognize the different methods of using quotes to contain your text in the print function.
- Identify process of making mistakes in programming and different solutions to help find and resolve mistakes.
Python Beginner - Module 5 - Dave & Alison 01
.py file with your desired code and import it into your program using the import statement.- 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.
- ValueError
- ZeroDivisionError
- TypeError. etc.
- 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.
- 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.