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

Lists

Lists using square brackets [] Example of Lists int_list = [1, 20, 77, 800, 39, 21, 5] str_list = ["python", "javascript", "java", "rust", "go", "dart"] print(int_list) print(type(int_list)) print(str_list) print(type(str_list)) Some of the things that I could do with lists: Sort the values in ascending or descending order Find values in list or details about the list Insert or remove values from the list Get a sub-list from the list Iterate through the list and perform functions or checks on each list item Some functions that could help me work with lists:...

February 22, 2022 · 4 min · Kei
python logo

Compound Data Types

Compound Data Type is a data made from collection of various data types like strings, integers, etc. Compound Data Types in Python can be represented by List, Dictionary, Tuple, Set. Tools I can use to work with Compound Data Types are Iterators, For loops, While loops, and other Functions List Using square brackets [ ] The elements are separated by comma Maintain order of the elements Using index to access the elements Mutable, the elements can be change [1, 2, 3, 1, True, "Kei", "Mars", 0....

February 19, 2022 · 1 min · Kei
python logo

If, Elif, Else

With branching using If, Elif, or Else statements I can control the execution of the code. The code block will only be executed if the condition is True. if condition: run this code elif other_condition_occured: run this code over here not the one above else: run this code for anything else that don't meet the conditions above Example: option = input("Choose 1 to Multiply numbers, Choose 2 to Divide: ") num1 = int(input("Enter the first number --> ")) num2 = int(input("Enter the second number --> ")) if option == "1": print(f"{num1}multiplied by {num2}is {num1 * num2}") elif option == "2": print(f"{num1}divided by {num2}is {num1 / num2}") else: print("That's an invalid selection") The usage of if, elif, and else in the code above works fine if the user choose either option 1 or 2, but it will still prompt the user to enter the first number even when the user entered something else other than 1 or 2 To fix this I can use Nested If Statements...

February 19, 2022 · 2 min · Kei
python logo

Booleans

Booleans evaluate to True or False based on conditions: Greater than > Greater than or equal to >= Less than < Less than or equal to <= Equal to == Not equal to != A Truth table is used to carry out logical operations

February 12, 2022 · 1 min · Kei
python logo

Input Function

The Input function is used to take input from the user. user = input("Please enter your name: ") print(50 * "=") print(f"Hello {user}, Good Morning!") first_num = input("Enter a number to multiply: ") second_num = input("Enter a second number to multiply: ") result = int(first_num) * int(second_num) print(f"{first_num}* {second_num}= {result}") Because Python runs from top down, it will not execute the next line of code until I typed the input

February 12, 2022 · 1 min · Kei