Python for absolute beginners – Part4

DATA DICTIONARY

variable = {"key": "value"}

person = {“name”: “John”, “age”: 23, “city”: “Montreal”}

  • print(person[“age”])  ==> 23
  • print(person.get(“age”)) ==> 23
  • person[“country”] = “canada” print(person) ==> {‘name’: ‘john’, ‘age’: 23, ‘city’: ‘montreal’, ‘country’: ‘canada’}
  • print(person.get(“key”, “Key not found!”) ==> Key not found!

FUNCTION

def function_name( "param" ):

def print_some_text(message):
the_text = message
print(f”The message is: {the_text}”)
print_some_text(“a message for you”)

def getFullName(name, lastName):
return name+’ ‘+lastName
print( getFullName(lastName=”Smith”, name=”John”) )

CLASS

Class Point:
def __init__(self, name, age):
self.name = name
self.age = age

def draw(self)
print (f”Hi {self.name}”)

point = Point(“John”, 6)
print (point.name) ==> John
print (point.draw()) ==> Hi John

Leave a Reply

Your email address will not be published.