Curriculum
Now that Python and PyCharm are installed on your computer, it’s time to write and execute your first Python program using PyCharm.
In this section, you will learn:
print()
functionBy the end of this lesson, you will have successfully written and executed your first Python program using PyCharm.
While Python does have an interactive shell (REPL), we will primarily use PyCharm as our environment for writing and running code. PyCharm comes with an integrated Python console if you still want to run one-off commands, but let’s start with the basics of managing an entire Python file in a project.
PyCharm will create a new project folder with default settings.
A Python script is a file with the extension .py
that contains Python code. You’ll create this in PyCharm.
hello.py
.Once created, PyCharm will open the file in the editor window. Add the following code:
# This is my first Python program
print("Hello, World!")
Output:
Hello, World!
Congratulations! You have successfully written and executed your first Python program in PyCharm. Next, lets explore the print() function.
print()
FunctionIn Python, the print()
function is one of the most commonly used tools for communicating information back to you (the developer) and others who might read your program’s output. Anything you pass to print()
—such as text, variables, or expressions—will be displayed in the console. In PyCharm, that’s typically the Run or Console tab.
print()
?print()
is a quick way to see the results of computations or the values of variables, making it invaluable for testing and debugging.print()
statements to trace the flow of execution, see intermediate values, and locate potential issues in your code.# Printing a string
print("Welcome to Python!")
Output:
Welcome to Python!
# Printing numbers directly
print(5)
# Printing the result of an expression
print(10 + 20)
Output:
5
30
print("My name is", "Alice", "and I am", 25, "years old.")
Output:
My name is Alice and I am 25 years old.
print()
Featuressep
parameter (for example, print("A", "B", "C", sep="-")
will output A-B-C
).print()
ends with a newline. You can change this using the end
parameter (for example, print("Hello", end="")
will continue printing on the same line).print()
function is essential for both displaying information to the user and aiding in debugging.Comments are lines or blocks of text in your code that Python completely ignores during execution. Despite being ignored by the interpreter, comments are crucial for making your code understandable to you and others—both now and in the future.
#
)You can insert a single-line comment by starting the line (or the end of a line) with the hash symbol (#
). It’s often good practice to leave a space after the #
for better readability.
# This is a comment
print("Hello, World!") # This prints a message
######### Section 1 #########
).""" """
or ''' '''
)Multi-line comments allow you to create blocks of descriptive text without prefixing each line with #
. They’re handy when you need to add explanations or instructions that span multiple lines.
"""
This is a multi-line comment.
It is useful for adding long explanations
or describing complex logic.
"""
print("This is a Python program.")
"""
or '''
).#
). This makes your code more readable and professional.By thoughtfully incorporating single-line and multi-line comments, your programs will remain clear, maintainable, and easy to navigate—even as they grow more complex.
Variables are a fundamental concept in any programming language, including Python. They act as containers that hold information which can change over time or remain constant throughout the program, depending on how they are used. Variables make your code flexible, allowing you to store, manipulate, and reference data as your program runs.
age
is more descriptive than using a raw number.name = "Alice"
age = 25
print("Hello, my name is", name, "and I am", age, "years old.")
name
and age
are variables that store a string ("Alice"
) and an integer (25
), respectively.name
and age
inside the print()
function.Output:
Hello, my name is Alice and I am 25 years old.
name = input("Enter your name: ")
print("Hello,", name, "welcome to Python programming!")
input()
prompts the user for text, storing whatever is typed into the variable name
.print()
function then uses the value in name
to display a personalized greeting.When you Run this script in PyCharm, the console will prompt for your name, then print a greeting. This simple example demonstrates how variables enable your program to respond dynamically to user input.
Key Takeaways:
user_name
, total_cost
, is_logged_in
) to make your code self-explanatory.Programming errors happen to everyone, especially when you’re starting out. Knowing how to spot and fix them quickly is an essential skill for any developer. In Python, one of the most common types of errors is the syntax error, which indicates that you’ve written code in a way that Python doesn’t understand.
(
has a matching )
, and similarly for [ ]
or { }
.print("Hello, World!) # Missing closing quote
When you run this, PyCharm (or Python in general) will return an error:
SyntaxError: EOL while scanning string literal
What Does This Mean?
EOL
stands for end of line. Python reached the end of the line (or file) and was still looking for the matching quote." "
or ' '
).print("Hello, World!") # Corrected version with matching quotes
Tip: If multiple errors appear, fix them one at a time, re-running your code after each fix to confirm you’ve resolved the issue.
Debugging means identifying why the error is happening and applying a suitable fix. As you gain more experience, you’ll become quicker at spotting these pitfalls and preventing them in the first place.
In this lesson, we have covered the following:
.py
) and execute it directly within PyCharm.print()
function displays text and variables in the PyCharm console.#
) help document your code for clarity.input()
.The next section is an exercise – 1.6 Exercise. There you will practice writing your own Python script using what you’ve learned in PyCharm!
Not a member yet? Register now
Are you a member? Login now