python logo

Iterators - While Loop

While loop can also be use for iteration but while loop is typically use when looping over some condition Example of While loop some_condition = True while some_condition: print("Hello World!") – This code will have an infinite loop, basically it will run forever until the computer crash. It will stop when I hit Ctrl + C to interrupt it, or if there is a break keyword somewhere in the code block....

April 10, 2022 · 1 min · Kei
python logo

Iterators - For Loop

I can use For loop and While loop for Iteration. They basically do about the same thing but the preferred method is using a For loop. For loop is typically use when iterating over a sequence of values (like a list or dictionary). Examples of Iteration using For loop: some_nums = [2, 4, 88, 99, 101] for num in some_nums: print(num) This will get the sum of all the integers value in a list :...

March 20, 2022 · 3 min · Kei
python logo

Sets

Sets using curly braces { } Example of a Set : mixed_set = {11, 200, 404, 30, "Python", "JavaScript", 99, "Ruby", "Rust"} Sets are unordered collection of elements, meaning that everytime I run the code the order of the items/elements will be print out randomly mixed_set = {11, 200, 404, 30, "Python", "JavaScript", 99, "Ruby", "Rust"} print(type(mixed_set)) print(mixed_set) Sets don’t allow for duplicate elements duplicates = {11, 11, "python", "ruby", 30, 9, 30, "python"} print(duplicates) If I have a List with some duplicates items I can cast it to a Set to remove them duplicate_list = ["python", "java", "ruby", "java", 300, 9, 9, 300, "python", 1, 10] print(duplicate_list) new_set = set(duplicate_list) print(new_set) Set is optimized for mainly to find information in them and for mathematical operations...

March 20, 2022 · 2 min · Kei
python logo

Tuples

Tuples using parentheses ( ) Example of Tuples mixed_tuple = ("mars", 11, 13, 99, "pluto", 81, 30, "venus") tup_str = ("pertama", "kedua", "ketiga") print(mixed_tuple) print(type(mixed_tuple)) print(tup_str) print(type(tup_str)) To list all the built-in methods and functions available for Tuple, I can use dir() mixed_tuple = ("mars", 11, 13, 99, "pluto", 81, 30, "venus") print(dir(mixed_tuple)) Tuple works similar like List, except that Tuple is immutable....

February 23, 2022 · 1 min · Kei
python logo

Dictionaries

Dictionaries using key-value pair for the elements Example of a Dictionary animals = { "marshmallow":"a black dog who likes to bark at things", "rudolph":"the very shiny red-nosed reindeer", "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future" } I can use dir function to list the built-in methods and functions I can use to work with Dictionaries animals = { "marshmallow":"a black dog who likes to bark at things", "rudolph":"the very shiny red-nosed reindeer", "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future" } print(dir(animals)) I can use keys method to see all the keys in a dictionary animals = { "marshmallow":"a black dog who likes to bark at things", "rudolph":"the very shiny red-nosed reindeer", "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future" } print(animals....

February 22, 2022 · 3 min · Kei
python logo

Importing Module

To use a Python module I can import it by typing import followed by the name of the module. This will import the module with all its functions and methods import string print(string.ascii_lowercase) I can just import a particular method or function in a module instead of the whole module from string import ascii_uppercase print(ascii_uppercase)

February 11, 2022 · 1 min · Kei
python logo

String Methods and Functions

Methods and Functions to use on Strings The Python Standard Library is the standard library that is distributed with Python. This library reference manual is available at Python Standard Library Some of the functions and methods I can use on string objects are: len(), type(), id(), capitalize(), upper(), lower(), strip(), find(), split(), join() Functions do not tied to the object. I have to type the function name followed by parentheses and pass in the object as an argument....

February 11, 2022 · 2 min · Kei