Popular Posts

Saturday, August 29, 2020

Python Beginner: Comments

 Hello, Developer!


Today, we will learn the "Comments". Comments are not execute in program but it is really  important to write comments in programs. Because comments make you easily understand codes, features and logic of program.


Comments Declaration and Examples

You can write comments in single line with starting "#" character. (i.e: # This is single line comment)

Also, you can start comments using "#" character in the command line.(i.e: print("This comment explanation line") # This is print out string

For multi-line comment, you can use " ''' " character at the start and end line. 

(i.e: '''

This 

is 

multi 

line 

comments

''')


In the Python programming, "#" character is used for comment officially. So we recommend you to use "#" instead " ''' " multi-line comment command.


Comment also can be used to except codes from running while you're debugging or have code test. If you add "#" character where you want to exclude program running, starting "#" lines won't be run. 


Here, I showed full exercise codes and running result as below.

# This program is cool
# This prints out a string

print("Comments are fun!")
'''
This multi-line comments
won't be run
Because
these lines
are
comments
print("This is comment")
'''

# print("Comment test message)

Can you see the same result? Congratulations! Now you can write comments in the Python program wherever you want.


Thank you for your reading this post and see you on next post, Python developer! 😊


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...