Popular Posts

Thursday, September 3, 2020

Python Beginner: Try & Except

 Hello, Developer!


Today, we will learn "Try and Except". "Try and Except" feature enables you to catch an error and to handle it. You can identify each specific errors and execute designated codes by specific error. 


Try and Except command form 

try:

      codes # Indented codes will be examined and if it has an error, it will execute "Except" codes. 

except: # If you want to handle whole errors in "Try" codes


try: 

      codes

except ZeroDivisionError as error: # If "Try" codes has "ZeroDivisionError", following codes will be executed.

      codes  

except ValueError: # If "Try" codes has "ValueError", following codes will be executed.

      codes

  

I showed full "Try and Except" exercise codes and execution result as following.

try:
answer = 10 / 0
except ZeroDivisionError as err:
print(err)

try:
number = int(input("Enter a number: "))
print(number)
except ValueError:
print("Invalid Input")

Can you see the same result on your PC? Great job! Now you can handle error & exceptional cases.


See you in the next post, Python developer! 😊

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