My Python Journey - Day 8: Function Parameters and the Caesar Cipher Project
Introduction
Hey there, fellow coding enthusiasts! Welcome back to my 100-day programming journey. Today marks Day 8 of my exciting adventure into the world of Python, and boy, have I learned a ton! Today, we're going to delve deep into the fascinating world of Function Parameters and embark on a mini-project that puts our newfound Python skills to the test - the Caesar Cipher Project. So, grab your code editor and let's dive right in!
Functions with Inputs
Our journey into Function Parameters begins with understanding how to create Python functions that can accept inputs. Functions are like little code ninjas that perform specific tasks, and inputs are the data they need to execute those tasks.
- Here's a simple function that takes an input and prints it:
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
In this example, name
is the input parameter, and when we call greet("Alice")
, it prints "Hello, Alice!"
- A simple function that takes an input and converts it into title case:
def title_case(name):
return name.title()
user_input = "john doe"
formatted_name = title_case(user_input)
print(f"Formatted name: {formatted_name}")
In this example, name
is the input parameter, and when we call title_case(user_input)
, it converts "john doe" into "John Doe."
Now, let's take things up a notch with another example:
- Calculating the Area of a Rectangle:
def rectangle_area(length, width):
area = length * width
return area
length = 5
width = 3
print(f"The area of the rectangle with length {length} and width {width} is {rectangle_area(length, width)}.")
In this example, we calculate the area of a rectangle by accepting the length and width as input parameters.
Parameters and Arguments
Let's clarify the terms "parameters" and "arguments" and highlight their key differences:
Parameters:
Parameters are placeholders in a function definition.
They are listed within the parentheses of a function.
Parameters define what data the function expects to receive.
Arguments:
Arguments are the actual values provided when calling a function.
They are the values passed to the function when it's invoked.
Arguments match the parameters based on their position or using keywords.
Here are three examples to illustrate the difference:
# Parameter: x
def square(x):
return x * x
# Argument: 5
result = square(5)
# Parameter: name, age
def introduce(name, age):
print(f"Hi, I'm {name}, and I'm {age} years old!")
# Arguments: "Bob", 30
introduce("Bob", 30)
# Parameter: items
def display_items(items):
for item in items:
print(item)
# Argument: [1, 2, 3]
display_items([1, 2, 3])
In these examples, x
, name
, and age
are parameters, while 5
, "Bob"
, 30
, and [1, 2, 3]
are arguments.
Positional vs Keyword Arguments
Now, let's talk about positional and keyword arguments. When we call a function, Python matches arguments based on their position or using keywords.
- Positional Arguments: The order of arguments matters.
def divide(x, y):
return x / y
result = divide(10, 2) # x=10, y=2
- Keyword Arguments: We explicitly specify which argument corresponds to which parameter.
result = divide(x=10, y=2) # x=10, y=2
- Mixing Positional and Keyword Arguments: You can use a mix of both.
result = divide(10, y=2) # x=10, y=2
Let's explore more examples of positional arguments:
- Positional Arguments with Default Values:
def power(base, exponent=2):
return base ** exponent
result1 = power(2) # Uses the default exponent of 2
result2 = power(2, 3) # Provides a custom exponent of 3
print(result1) # 4
print(result2) # 8
In this example, if we don't provide an exponent, it defaults to 2.
Caesar Cipher Project
After mastering function parameters, I took on the Caesar Cipher Project. This project involves creating a Python program that can encrypt and decrypt messages using the Caesar cipher technique. It's like sending secret messages, just like spies in the movies!
I won't bore you with all the code here, but here's a snippet to whet your appetite:
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
if char in alphabet:
position = alphabet.index(char)
new_position = position + shift_amount
end_text += alphabet[new_position]
else:
end_text += char
print(f"Here's the {cipher_direction}d result: {end_text}")
from art import logo
print(logo)
should_end = False
while not should_end:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
shift = shift % 26
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
restart = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if restart == "no":
should_end = True
print("Goodbye")
In this snippet, we define a function caesar
that takes inputs and either encrypt or decrypt the messages .
Wrapping Up
Day 8 was an exhilarating journey into function parameters and the world of the Caesar cipher. We've learned how to create functions that take inputs, clarified parameters vs. arguments, and grasped the concepts of positional and keyword arguments.
Now, armed with this knowledge, I'm ready to tackle more complex Python projects in the days ahead. Stay tuned for Day 9, where we'll explore even more exciting Python concepts. Until then, happy coding, my fellow Python adventurers! ๐๐