Hello everyone! 👋
Welcome to my Python learning journey! I’m here to share my progress with you as I learn more about programming, specifically Python. Today is the first day I officially start to learn Python, and today I’ve learned: the input()
function, the print()
function, and the basics of using variables.
Let’s get started!
What I Learned Today
1. The print()
Function
The print()
function in python is one of the most basic yet essential tools in Python. It allows you to display text or values on the screen. Think of it as your way of communicating with the user or debugging your code.
Example:
print("Hello, World!")
This code will output:
Hello, World!
You can also print variables or multiple items by separating them with commas:
name = "Alice"
age = 25
print("Name:", name, "Age:", age)
Output:
Name: Alice Age: 25
Task to solve today:
Write a program that prints your favorite quote on the screen. Make sure to include the author's name in the output.
print("The only limit to our realization of tomorrow is our doubts of today. - Franklin D. Roosevelt")
Print the result of a simple math calculation (e.g., 5 + 7 * 3
) using the print()
function.
print(5 + 7 * 3) # Output: 26
2. The input()
Function
The input()
function in python is used to take user input. It pauses the program and waits for the user to type something. Whatever the user types is returned as a string, which you can store in a variable.
Example:
name = input("Enter your name: ")
print("Hello,", name)
If the user enters "John", the output will be:
Enter your name: John
Hello, John
Write a program that asks the user for their favorite color and then prints a message saying, "Your favorite color is [color]."
color = input("What is your favorite color? ")
print("Your favorite color is", color + ".")
Ask the user for two numbers, add them together, and print the result.
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
result = int(num1) + int(num2)
print("The sum is:", result)
3. Basic Use of Variables
Variables are like containers that store data. You can assign values to variables using the =
operator. Variables can hold different types of data, such as strings, numbers, or even more complex data structures.
Example:
# Assigning values to variables
fruit = "apple"
quantity = 5
price = 1.25
# Using variables in a print statement
print("I bought", quantity, fruit + "s for $", price * quantity)
Output:
I bought 5 apples for $ 6.25
Create two variables, one for your name and one for your age. Print a message that says, "My name is [name], and I am [age] years old."
name = "Alice"
age = 25
print("My name is", name + ", and I am", age, "years old.")
Create a variable to store the price of an item and another variable to store the quantity. Calculate the total cost and print it.
price = 10.50
quantity = 3
total_cost = price * quantity
print("The total cost is $", total_cost)
Putting It All Together
Here’s a simple program that combines everything I learned today:
# Ask the user for their name and age
name = input("What is your name? ")
age = input("How old are you? ")
# Print a personalized message
print("Nice to meet you,", name + "!")
print("In 10 years, you will be", int(age) + 10, "years old.")
Example Output:
What is your name? Sarah
How old are you? 22
Nice to meet you, Sarah!
In 10 years, you will be 32 years old.
FAQ Section
1. What is the difference between input()
and print()
?
input()
is used to take user input (whay user typed), whileprint()
is used to display output on the screen
2. Can I use numbers with input()
?
- Yes, but
input()
always returns a string. If you need to perform calculations, you’ll need to convert the input to an integer or float usingint()
orfloat()
.
3. How do I create a variable name?
- Variable names can contain letters, numbers, and underscores, but they cannot start with a number. They should also be descriptive and meaningful.
4. Can I change the value of a variable in Python?
- Yes, you can reassign a new value to a variable at any time.
That’s it for today! I will keep learning python and If you’re just starting out like me, I hope this post was helpful.
Feel free to leave a comment or share your own experiences. Let’s learn & grow together!