Skip to main content

Command Palette

Search for a command to run...

3- ๐ŸŒŸ Pykid Functions ๐ŸŒŸ

Updated
โ€ข3 min readโ€ขView as Markdown
3- ๐ŸŒŸ Pykid Functions ๐ŸŒŸ
A
I am an Assistant Professor of Remote Sensing in the Department of Geography at the Soran University. My research interests include remote sensing, GIS, climate and environment. More specifically, I am interested in applying geospatial big data, programming and cloud computing (e.i. Google Earth Engine) to study environmental change, especially land use land cover and climate monitoring. More information about my research and teaching can be found at https://www.linkedin.com/in/azad-rasul-1860abb1/.

Think of a function as a super-cool gadget that can do a special job for you, just like a robot helper!

Let's create a gadget called "my_function":

def my_function(): # Inside the gadget, we write the instructions for its job:
     print("๐Ÿ‘‹ Hello from Pykid!")

Now, we can use the gadget by calling its name, like pressing a button:

my_function() # This makes the gadget do its job, and we see the message!
# Output: ๐Ÿ‘‹ Hello from Pykid!

We can make our gadgets even more awesome by giving them special tools to work with!

For example, let's give a gadget a name to use in a greeting:

def greet_friend(name): # Now the gadget knows to use the name we give it:
     print(f" Hello, {name}! How are you today?")

Let's try it out!

greet_friend("Aisha")
# Output: Hello, Aisha! How are you today?
greet_friend("Anas")
# Output: Hello, Anas! How are you today?

Cool, right? The gadget can do different things depending on the tools we give it! It's like having a whole toolbox of gadgets!

Remember, functions are like our trusty robot helpers in coding, making our programs easier and more fun to build!

Creating a Function to Introduce Animals:

  • Imagine a special machine called "animal_sounds" that knows about different animals and their sounds.

  • This machine takes the name of an animal as its input.

  • It has a set of instructions for each animal it knows:

    • If you tell it "cat", it prints "๐Ÿฑ A cat says: Meow!"

    • If you tell it "dog", it prints "๐Ÿถ A dog says: Woof!"

    • And so on for cows, ducks, and more!

  • If you give it an animal it doesn't know, it politely says "Sorry, I don't know that animal!"

def animal_sounds(animal):
    if animal == 'cat':
        print("๐Ÿฑ A cat says: Meow!")
    elif animal == 'dog':
        print("๐Ÿถ A dog says: Woof!")
    elif animal == 'cow':
        print("๐Ÿฎ A cow says: Moo!")
    elif animal == 'duck':
        print("๐Ÿฆ† A duck says: Quack!")
    else:
        print("Sorry, I don't know that animal!")

# Introducing different animals and their sounds
animal_sounds('cat')
# Output: ๐Ÿฑ A cat says: Meow!

animal_sounds('dog')
# Output: ๐Ÿถ A dog says: Woof!

animal_sounds('cow')
# Output: ๐Ÿฎ A cow says: Moo!

animal_sounds('duck')
# Output: ๐Ÿฆ† A duck says: Quack!

animal_sounds('elephant')
# Output: Sorry, I don't know that animal!

Playing Animal Sounds:

  • We can make this machine even cooler by giving it the ability to play real animal sounds!
  • We import a special tool called "playsound" that can play audio files.
  • Now, instead of just printing the sound, the machine can play it out loud!
  • We give it the location of each sound file, like a map that helps the machine find the right sound.

Calling the Function:

  • To use the machine, we call it by its name, "animal_sounds", and give it the name of the animal we want to hear.
  • For example, animal_sounds('cat') plays the cat's meow.
  • We can call it many times to hear different animals!

Note: install the tool first with pip install playsound.

from playsound import playsound

# Function to play animal sounds
def animal_sounds(animal):
    if animal == 'cat':
        playsound('https://www.myinstants.com/media/sounds/meow_3.mp3')
    elif animal == 'dog':
        playsound('https://www.myinstants.com/media/sounds/dog-barking-sound-effect.mp3')
    elif animal == 'cow':
        playsound('https://www.myinstants.com/media/sounds/cow1.mp3')
    elif animal == 'duck':
        playsound('https://www.myinstants.com/media/sounds/duck.mp3')
    else:
        print("Sorry, I don't know that animal's sound!")

# Calling the function to play animal sounds
animal_sounds('cat')
animal_sounds('dog')
animal_sounds('duck')
animal_sounds('cow')

Handling Unknown Animals:

animal_sounds('elephant')
# Output: Sorry, I don't know that animal's sound!

More from this blog

SmartRS โ€“ Remote Sensing, GIS & Data Science Tutorials

127 posts

Practical, step-by-step tutorials on remote sensing, GIS, climate data and data science with R, Python and JavaScript: satellite image analysis, spatial data processing, and data visualization. Written by Dr. Azad Rasul, Assistant Professor of Remote Sensing and GIS.