What's new

Pro python help po.

_Big Boss_

Forum Veteran
Joined
May 20, 2020
Posts
699
Reaction
4,090
Points
740
Pa check naman po. Kala ko all goods napo. Kaya lang ayaw mag run. Salamat po.


# Python Keywords ---------------------------------------------
def and_keyword(Raw_Data1):
print("\n----And keyword")
print("• Example")
first = Raw_Data1[0]
last = Raw_Data1[-1]
Sum = first + last
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. first = Raw_Data1[0]")
print("3. last = Raw_Data1[-1]")
print("4. Sum = first + last")
print("5. if first == last and Sum > '10':")
print("6. print('True')")
print("7. else:")
print("8. print('False')")
print(" Output: ", end = ' ')
if first == last and Sum > "10":
print("True")
else:
print("False")
print("• Explanation")
print(" In our example we first took the first and last elements in our list and then add it to each other. Using the 'and' keyword we compared 2 conditions, the first condition is if 'first' has the same value as the 'last', the second condition is if 'sum' is greater than '10'. If both are true it will return true, if either one is false it will return false.")

def as_keyword():
print("\n----As keyword")
print("• Example")
print("1. import file as NewFile")
print("• Explanation")
print(" On the example we imported a file and using the keyword 'as' we gave it a name 'NewFile'so each time we're going to use the imported file we're going to call ot using the given name'NewFile'.")

def assert_keyword(Raw_Data1):
print("\n----Assert keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
try:
print("2. assert '5' in Raw_Data1")
assert "5" in Raw_Data1
except AssertionError:
print(" AssertionError: The condition returns false.")
print("• Explanation")
print(" Using the assert keyword we checked if a certain condition is true or false. if true nothing happens, if false it will raise an error.")

def break_keyword(Raw_Data1):
print("\n----Break keyword")
print("• Example")
for i in Raw_Data1:
if i > 5:
break
print("1. for i in Raw_Data1:")
print("2. if i > 5:")
print(f"3. print('{i} is greater than 5')")
print("4. break")
print(f' Output: {i} is greater than 5')
print("• Explanation")
print(" Using the break keyword we break out of the loop if a certain condition is met.")

def class_keyword():
print("\n----Class keyword")
print("• Example")
print("1. class Person:")
print(f"2. name = 'CAPSLOCK'")
print("3. age = 100")
class Person:
Name = "CAPSLOCK"
Age = 100
print("4. print(Person.Name)")
print(f" Output: {Person.Name}")
print("• Explanation")
print(" Using the class keyword we created a class named Person.")

def continue_keyword(Raw_Data1):
print("\n----Continue keyword")
print("• Example")
print("1. for i in Raw_Data1:")
print("2. if i > 5:")
print("3. continue")
for i in Raw_Data1:
if i > 5:
continue
print("• Explanation")
print(" Using the continue keyword we're simply telling the program that if the condition durring the current iteration in a loop is met then continue to the next iteration.")

def def_keyword(Raw_Data1):
print("\n----Def keyword")
print("• Example")
print("1. def My_function():")
print("2. print(Raw_Data1)")
print("3. My_function()")
def My_function():
print(Raw_Data1)
print(" Output:", end = ' ')
My_function()
print("• Explanation")
print(" Using the def keyword we created or defined a function called My_function. To execute the block of code inside the function we simply call it by typing the function name with a pair of open and close parenthesis.")

def del_keyword():
print("\n----Del keyword")
print("• Example")
print("1. class Person:")
print("2. Name = 'CAPSLOCK'")
print("3. del Person")
print("4. print(Person)")
class Person1:
Name = "CAPSLOCK"
del Person1
try:
print(Person1)
except UnboundLocalError:
print("\n UnboundLocalError: 'Person1' is not defined.")
print("• Explanation")
print(" In our example we first created a class called Person1, using the del keyword we deleted the class called Person1. Upon printing it should raise an error because the class Person1 is already deleted.")

def elif_keyword(Raw_Data1):
print("\n----Elif keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. Last_Element = Raw_Data1[-1]")
print("4. if Last_Element > 100:")
print("5. print(Last_Element, 'is greater than 100')")
print("6. elif Last_Element < 100:")
print("7. print(Last_Element, 'is lower than 100')")
print(" Output:", end = ' ')
Last_Element = Raw_Data1[-1]
if Last_Element > 100:
print(f"{Last_Element} is greater than 100")
elif Last_Element < 100:
print(f"{Last_Element} is lower than 100")
print("• Explanation")
print(" In our example we first took the last element in our list. Using the 'elif' keyword, we're basicaly telling the program that if all conditions above the elif is not met then try the 'elif'")

def else_keyword(Raw_Data1):
print("\n----Elif keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. Last_Element = Raw_Data1[-1]")
print("4. if Last_Element > 1000:")
print("5. print(Last_Element, 'is greater than 1000')")
print("6. elif Last_Element > 500:")
print("7. print(Last_Element, 'is greater than 500')")
print("8. else:")
print("9. print('No condition is met.')")
print(" Output:", end = ' ')
Last_Element = Raw_Data1[-1]
if Last_Element > 1000:
print(f"{Last_Element} is greater than 1000")
elif Last_Element > 500:
print(f"{Last_Element} is greater than 500")
else:
print("No condition is met.")
print("• Explanation")
print(" In our example we first took the last element in our list. Using the 'else' keyword, we're basicaly telling the program that if none of conditions above is met then execute the 'else' statement.")

def except_keyword(Raw_Data1):
print("\n----Except keyword")
print("• Example")
print("1. try:")
print("2. Last_Element = Raw_Data1[-1]")
print("3. word = 'Hello'")
print("4. Sum = Last_Element + word")
print("5. except TypeError:")
print("6. print('TypeError: cannot add String to integer.')")
try:
Last_Element = Raw_Data1[-1]
word = "Hello"
Sum = Last_Element + word
except TypeError:
print("TypeError: cannot add String to integer.")
print("• Explanation")
print(" In our example we tried to add the last element of the list which is an int value to a string value, this operation will raise an error because you cannot add string to int. Using the 'except' keyword we catch this error to make the error more readable for the user.")

def false_keyword(Raw_Data1):
print("\n----False keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. first = Raw_Data1[0]")
print("3. print(first > 100000)")
first = Raw_Data1[0]
print(" Output:", end=' ')
print(first > 100000)
print("• Explanation")
print(" In our example we compare the if first value of the list is greater than '100000' if not it should return false otherwise true.")

def finaly_keyword(Raw_Data1):
print("\n----Finally keyword")
print("• Example")
print("1. try:")
print("2. print(Raw_Data1)")
print("3. except:")
print("4. print('Something went wrong!')")
print("5. finally:")
print("6. print('The try...except block is finished!')")
print(" Output: ")
try:
print(f" Raw_Data1 = {Raw_Data1}")
except:
print(" Something went wrong!")
finally:
print(" The try,,except block is finished!")
print("• Explanation")
print(" Using the finally keyword we executed a print statement wether an error occur or not.")

def from_keyword():
print("\n----From keyword")
print("• Example")
print("1. from datetime import time")
print("• Explanation")
print(" Using the from keyword we imported a specific part of the module datetime.")

def for_keyword(Raw_Data1):
print("\n----For keyword")
print("• Example")
print("1. for val in Raw_Data1:")
print("2. print(val)")
print(" Output:", end = ' ')
for val in Raw_Data1:
print(val, end = ' ')
print("\n• Explanation")
print(" Using the for keyword we created a loop that will display all the elements in our list.")

def global_keyword():
print("\n----Global keyword")
print("• Example")
def global_var():
global x
x = "Hello World"
print("• Explanation")
print(" In our example we created a function called 'global_var' and inside the function we created a variable called 'x', using the global keyword we made the variable x accessible even outside the function.")

def if_keyword(Raw_Data1):
print("\n----Elif keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. Last_Element = Raw_Data1[-1]")
print("3. First_Element = Raw_Data1[0]")
print("4. if Last_Element > First_Element:")
print("5. print(Last_Element, 'is greater than ', First_Element)")
print(" Output:", end = ' ')
Last_Element = Raw_Data1[-1]
First_Element = Raw_Data1[0]
if Last_Element > First_Element:
print(f"{Last_Element} is greater than {First_Element}")
print("• Explanation")
print(" In our example we first took the last and first element in our list. Using the 'if' keyword, we compared the Last_Element to the First_Element, if condition is true it will execute the block of code inside the if statement.")


def import_keyword():
print("\n----Import keyword")
print("• Example")
print("1. import datetime")
print("• Explanation")
print(" in our example we used the keyword import to import a module called datetime")

def in_keyword(Raw_Data1):
print("\n----In keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. print(100 in Raw_Data1)")
print(" Output:", end = ' ')
print(100 in Raw_Data1)
print("• Explanation")
print(" The in keyword returns True or False. In our example we used the in keyword to check if the value '100' exist in our list.")

def is_keyword(Raw_Data1):
print("\n----Is keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. Raw_Data2 = Raw_Data1")
print("3. print(Raw_Data1 is Raw_Data2)")
Raw_Data2 = Raw_Data1
print(" Output:", end = ' ')
print(Raw_Data1 is Raw_Data2)
print("• Explanation")
print(" Using the is keyword we checked if 'Raw_Data1' is equal to 'Raw_Data2'.")

def lambda_keyword(Raw_Data1):
print("\n----Lambda keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. x = lambda a, b: a + b")
print("3. Last_Element = Raw_Data1[-1]")
print("4. First_Element = Raw_Data1[0]")
print("5. print(x(Last_Element,First_Element))")
print(" Output:", end = ' ')
x = lambda a, b: a + b
Last_Element = Raw_Data1[-1]
First_Element = Raw_Data1[0]
print(x(Last_Element,First_Element))
print("• Explanation")
print(" Using the lambda keyword we created a small function called 'x' which adds the variable 'a' to 'b'. To supply a value to 'a' and 'b' we simply type the function name with a pair of close and open parenthesis, inside the parenthesis is the matching value.")

def none_keyword():
print("\n----None keyword")
print("• Example")
print("1. x = None")
print("2. print(x)")
print(" Output:", end = ' ')
x = None
print(x)
print("• Explanation")
print(" Using the none keyword we created a variable 'x' with a null value or no value at all.")

def nonlocal_keyword():
print("\n----Nonlocal keyword")
print("• Example")
print("1. def functuin1():")
print("2. x = 'Hello'")
print("3. def functuin2():")
print("4. nonlocal x")
print("5. x = 'Hi HI HI'")
print("6. functuin2()")
print("7. functuin1()")
print(" Output:", end = ' ')
def functuin1():
x = "Hello"
def functuin2():
nonlocal x
x = "Hi HI HI"
functuin2()
functuin1()
print("• Explanation")
print(" In our example we created a function inside a function. Using the nonlocal keyword we created a nonlocal variable called 'x' with a value of 'HI HI HI'.")

def not_keyword():
print("\n----Not keyword")
print("• Example")
print("1. x = False")
print("2. print(not x)")
print(" Output:", end = ' ')
x = False
print(not x)
print("• Explanation")
print(" In our example we created a variable called 'x' with a value of 'False'. Using the not keyword we return the opposite of the value of 'x' which is 'True'.")

def or_keyword(Raw_Data1):
print("\n----Or keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. Last_Element = Raw_Data1[-1]")
print("3. First_Element = Raw_Data1[0]")
print("4. if Last_Element > 10 or First_Element > 10:")
print("5. print('One of he condition is ture.')")
print(" Output:", end = ' ')
Last_Element = Raw_Data1[-1]
First_Element = Raw_Data1[0]
if Last_Element > 10 or First_Element > 10:
print("One of he condition is ture.")
print("• Explanation")
print(" In our example we used the or keyword to checked if either the first or last value of the list is greater than '10'. If either one is true it will return true. The or keyword will only return False if both conditions are False.")

def pass_keyword():
print("\n----Pass keyword")
print("• Example")
print("1. for x in Raw_Data1:")
print("1. pass")
print("• Explanation")
print(" Empty codes is not allowed in loops, but in our example we use the 'pass' keyword to avoid getting an error since we did not put any code iside our loop.")

def raise_keyword(Raw_Data1):
print("\n----Raise keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. for x in Raw_Data1:")
print("3. if x < 100:")
print("4. raise Exception('Numbers ower than 100 is not allowed.')")
print("• Explanation")
print(" The raise keyword is use to raise an Exception. In our example we used the raise keyword to raise an Exception if a certain condition is met.")

def return_keyword(Raw_Data1):
print("\n---- keyword")
print("• Example")
print("1. def Return(Raw_Data1):")
print("2. Last_Element = Raw_Data1[-1]")
print("3. First_Element = Raw_Data1[0]")
print("4. return Last_Element + First_Element")
print("5. Return(Raw_Data1)")
print(" Output:", end = ' ')
def Return(Raw_Data1):
Last_Element = Raw_Data1[-1]
First_Element = Raw_Data1[0]
return Last_Element + First_Element
Return(Raw_Data1)
print("• Explanation")
print(" Here in our example we use the return keyword to return the sum of the first and last element in the list once we exit the function.")

def true_keyword(Raw_Data1):
print("\n----False keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. first = Raw_Data1[0]")
print("3. print(first > 1)")
first = Raw_Data1[0]
print(" Output:", end=' ')
print(first > 1)
print("• Explanation")
print(" In our example we compare the if first value of the list is greater than '1', if true it should return True otherwise False.")

def try_keyword(Raw_Data1):
print("\n---- keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. try:")
print("3. first = Raw_Data1[0]")
print("4. first > 1000000")
print("5. except:")
print("6. print('Something went wrong!')")
print(" Output:", end=' ')
try:
first = Raw_Data1[0]
first > 1000000
except:
print("Something went wrong!")
print("• Explanation")
print(" Here in our example we use try keyword to try a block of code if it has any error.")

def while_keyword(Raw_Data1):
print("\n---- keyword")
print("• Example")
print(f"1. Raw_Data1 = {(Raw_Data1)}")
print("2. x = 1")
print("3. First_Element = Raw_Data1[0]")
print("4. while x < First_Element:")
print("5. print(x, end=' ')")
print("6. x += 1")
print(" Output:", end=' ')
x = 1
First_Element = Raw_Data1[0]
while x < First_Element:
print(x, end=' ')
x += 1
print("• Explanation")
print(" Here in our example we use the while keyword to create a loop that will loop as long as 'x' is less than the first element of our list.")

def with_keyword():
print("\n---- keyword")
print("• Example")
print("1. with open('file_path', 'w') as file:")
print("• Explanation")
print(" Here in our example we use the with keyword to open a file. Using with keyword when opening a file means that you don't have to close it because it will be automaticaly closed.")

def yield_keyword(Raw_Data1):
print("\n---- keyword")
print("• Example")
print("1. def Yield():")
print("2. yield First_Element = Raw_Data1[0]")
print("3. yield Last_Element = Raw_Data1[-1]")
print("4. [print(value, end=' ') for value in Yield()]")
print(" Output:", end=' ')
def Yield():
yield Raw_Data1[0]
yield Raw_Data1[-1]
[print(value, end=' ') for value in Yield()]
print("\n• Explanation")
print(" The yield keyword is use to generate a values. Here in our example we use yield keyword to return multiple values or a sequence of values.")
 

Similar threads

Back
Top