Popular Posts

Friday, August 28, 2020

Python Beginner: Build a Translator

 Hello, Developer!


I wish you didn't experience while understand multi-dimension lists and nested loop feature. Today, we will build a "Translator". "Translator" will get input strings or phrases from user and change target alphabets into mapped characters. For instance, user input some strings like "Apple" and we want to exchange vowels(a, e, i, o, u)  into "x" from input strings.


Here is translation process example for your understanding.

Input String : Apple # Check each characters of input string and exchange vowels into "x" when it find vowels 

Translated String: xpplx


Additionally, we also adding feature this translator checks "Upper" & "Lower" character and output Upper X and Lower x in case. For this feature, we can use simply matching upper or lower character vowels or we can convert each character into lower character and compare it is lower vowel and if it is vowel, once again check it is upper character, if it is upper character, exchange character into upper target character(i.e. "X") or it is lower character, exchange it into lower target character(i.e. "x")


Here, I showed full exercise codes and execution result as following.

def translate(phrase):
translation =""
for letter in phrase:
if letter in "AEIOUaeiou":
translation = translation + "x"
else:
translation = translation + letter
return translation

print(translate(input("Enter your phrase: ")))

def translate(phrase):
translation =""
for letter in phrase:
if letter in "AEIOU":
translation = translation + "X"
elif letter in "aeiou":
translation = translation + "x"
else:
translation = translation + letter
return translation

print(translate(input("Enter your phrase 2: ")))

def translate(phrase):
translation =""
for letter in phrase:
if letter.lower() in "aeiou":
if letter.isupper():
translation = translation + "X"
else:
translation = translation + "x"
else:
translation = translation + letter
return translation

print(translate(input("Enter your phrase 3: ")))


Can you see the same result on your PC with your coding? Great job! Now you can apply loop, if statements and function feature as well.


Alright, see you on next post, Developer! 😊

Python Beginner: 2D Lists & Nested Loops

 Hello, Developer!


Today, we will learn "2D Lists" and "Nested Loops".

Let's look into 2D Lists first. We can simply tell the 2 dimensional(2D) Lists are grids or arrays which consist of rows and columns. Here are some examples of 2D Lists.


Examples of 2 & 3 dimensional(2D & 3D) Lists 

list_2D_1 = [ [a, b, c], [d, e, f], [g, h, i]]

list_2D_2 = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]

list_3D_1 = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]


Example of Calling values from 2D & 3D Lists

list_2D([row index 1][column index 2]) # Example: list_2D_1([0] [0]) = a, list_2D_1([0] [1]) = b, list_2D_1([2] [2]) = 8

list_3D([1st index][2nd index][3rd index]0 # Example: list_3D_1([0][0][0]])=1, list_3D_1([1][1][0]])=7


Similar way, Nested loops can be used to calling values from multi dimension(2D, 3D or higher dimension) lists. Here are normal loop and nested loop examples for understanding through comparing of each loop process.


Normal Loop calling values from Multi-dimension Lists example

for value in multi-dimension list:

      print(value) # In this case, you will call value by list type like [a,b,c] or [1,2,3]


Nested Loop calling values from Multi-dimension List example

for value_1 in multi-dimension list:

      for value_2 in value_1 # In this case, you will call value by individual values type like a, b, c,... or 1, 2, 3... 

 

Here, I showed full exercise codes and execution result as following.

number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
print(number_grid[0][1])

list_2D_2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list_2D_2)
print(list_2D_2[0][0])

list_3D_1 = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
print(list_3D_1[0][1][1])


for row in number_grid:
print(row) # If you want to print out all values in list as list type like [1,2,3],[[4,5,6],...

for row in number_grid:
for col in row: # If you want to print out all values as individual values as 1,2,3...
print(col)


Can you see the same result in your PC and your coding? Great job! Now you can use "Multi Dimension Lists" and "Nested Loops" in your Python program.


Alright, see you on next post! 😊 

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...