Curriculum
Now that you’ve learned the basics of Python and have installed PyCharm, it is time to create and run your first Python program. This exercise will help reinforce your understanding of printing messages, storing user input in variables, and writing comments.
Goal | Details |
---|---|
File Name | my_first_program.py |
IDE | PyCharm |
Core Concepts | print() function, input() function, variables, comments |
Objective | Prompt the user for their name and age, then display a personalized message. |
File
> New Project
).my_first_program.py
.my_first_program.py
(PyCharm will open it by default if you just created it).# 1) Print a welcome message
print("Welcome to My First Python Program!")
# 2) Ask for the user's name
name = input("What is your name? ")
# 3) Ask for the user's age
age = input("How old are you? ")
# 4) Print a personalized greeting
print("Hello,", name + "! You are", age, "years old. Have a great day!")
#
) to describe each section of the code.my_first_program
if prompted.Although PyCharm is the recommended IDE, you can also run your script from the command line or terminal.
Operating System | Steps |
---|---|
Windows | 1. Open Command Prompt (cmd) 2. Navigate (cd ) to your project folder 3. Run: python my_first_program.py |
Mac/Linux | 1. Open Terminal 2. Navigate (cd ) to your project folder 3. Run: python3 my_first_program.py |
Use these bonus tasks to further develop your Python skills.
print(f"Hello, {name}! You are {age} years old. Have a great day!")
int()
to allow numerical operations:
age = int(input("How old are you? "))
print(f"In 5 years, you will be {age + 5} years old!")
if name == "":
print("You didn't enter a name!")
else:
print(f"Hello, {name}!")
input()
return, and how is it different from print()
?age
to an integer change the possibilities for your program?(Reflect on these questions before checking the solutions!)
Below is a sample solution for the main exercise requirements. Compare this with your code to identify any differences and see possible improvements.
# my_first_program.py
# 1) Print a welcome message
print("Welcome to My First Python Program!")
# 2) Ask for the user's name
name = input("What is your name? ")
# 3) Ask for the user's age
age = input("How old are you? ")
# 4) Print a personalized greeting
print("Hello,", name + "! You are", age, "years old. Have a great day!")
input()
vs. print()
input()
prompts and collects data from the user (returns a string by default).print()
displays data to the console.age
to an Integer
age + 5
) and validate numeric input.Congratulations! You have successfully created and run your first Python program in PyCharm. You have also explored ways to enhance your code using formatting, integer conversion, and input validation. In the next chapter, you’ll dive deeper into Variables and Data Types, learning how to handle strings, numbers, booleans, and more advanced data operations.
Not a member yet? Register now
Are you a member? Login now