Popular Posts

Thursday, August 27, 2020

Python Beginner: If Statements & Comparisions

 Hello, developer!


Today, we will learn value comparison values in condition of "If Statements".

We can use  number value comparison as below.


Number Value Comparisons

value_1 == value_2 # value_1 and value_2 are same value

value_1 > value_2 # value_1 is greater than value_2

value_1 >=  value_2 # value_1 and value_2 are same or value_1 is greater than value_2

value_1 != value_2 # value_1 is not same value_2


We will use these comparisons in the condition of if statements.


I showed the "If Statements & Comparisons" exercise codes and execution result as following.

def max_num(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3

print(max_num(300,4,5))

def max_num2(num1, num2, num3):
if num1 == num2 and num1 != num3:
return num1
elif num1 != num2 and num2 == num3:
return num2
else:
return num3

print(max_num2(5,10,15))


Can you see the same result on your PC? Great job! Now you can use number value comparisons in if statements.


See you on next post, developer! 😊



Python Beginner: If Statements

 Hello, Python developer!


Hope you handle values through functions.


Today, we will learn "If Statements". The "If statements" enables you to make decision through comparing conditions can be confirmed as True or False. It executes designated codes when the condition is True. We can program input value from user and check condition by input value and response by executing guided codes.

Here is an example the "If Statements" in our real life. We can assume a lot of decision making cases in life can be exchanged "if" & "otherwise" statements. 


I wake up 

if I'm hungry

    I eat breakfast


I leave my house

if it's cloudy

    I bring an umbrella

otherwise

    I bring sunglasses


I'm at a restaurant

if I want meat

    I order a steak

otherwise if I want pasta

    I order spagetti & meatballs

otherwise 

    I order a salad


I showed "If Statements" definition and examples.

If statements example

If condition(the result should be Boolean type(True or False))

    print("This is If Statements example!") # This line was indented

else:

    print("This is not If Statements example!") # This line was indented


If you make sub if statements in if statements, you can use "Else if" command as below.


Else If statements command

elif condition  # Should be located under If statement line without indent


In "If statements", you can use "And", "Or" and "Not" command for condition comparing as below.


Additional If Statements commands

if condition_1 and condition_2 # Execute indented codes in case, all condition_1 and condition_2 are "True"

if condition_1 or condition_2 # Execute indented codes in cases, all condition_1 and condition_2 are "True" or one of condition(condition_1 or condition_2) is "True" 

if not(condition_1) # Execute indented codes in case, condition_1 is "False"

if condition_1 and not(condition_2) (combination of condition commands) # Execute indented codes in cases, condition_1 is "True" and condition_2 is "False"


Is it clear for understanding "If statements commands"? If you're good, let's start coding!


I showed full exercise codes and execution result as following.

is_male = True
is_tall = False

if is_male and is_tall:
print("You are a tall male.")
elif is_male and not(is_tall):
print("You are a short male.")
elif not(is_male) and is_tall:
print("You are not a male but are tall")
else:
print("You are either not male or not tall or both.")

Can you see the same result? Great job! Now you can use if statements in Python programming!


See you on next post, Python developer! 😊


Wednesday, August 26, 2020

Python Beginner: Return Statements

 Hello, Python developer!


I hope you have good ideas to utilize functions on your Python programming. 


Today, we will learn the "Return Statements". When you input values into functions through "parameters" and if you want to get calculated values from functions, you can use "Return" command for returning values from function.


Here is definition and example of return command.


Return command example

return function_value # return command should be in the function


I showed full exercise codes and execution result as following.

def cube(num):
num*num*num # There is no return value
print(cube(3))

def cube2(num):
return num * num * num
print(cube2(3))

def cube3(num):
a = num * num * num + 10
b = a * 2
return b

result = cube3(4)
print(result)


Can you see same result? Great! Now you can input and output values in the function.

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