Python First Program
In this page:
The print() Function
print() is the built-in function that writes output to the screen — pass it text, a number, or a variable, and it displays the result followed by a newline. It's the fastest way to see what your code is actually doing while you're learning.
Example: The print() Function
print("Hello, World!")
Basic Strings
Python treats text wrapped in either single or "double" quotes identically as a str value, so you can pick whichever avoids awkward escaping (for example, using double quotes for a string that itself contains an apostrophe).
Both forms are functionally interchangeable.
Example: Basic Strings
print('Hello') # single quotes work for strings
print("Hello") # double quotes work the same way
print("It's a good day") # double quotes avoid escaping the apostrophe
Standard Calculations
Because print() evaluates its argument before displaying it, you can write print(2 + 3) directly and Python will compute 5 and show that, without a separate calculation step first.
This is a quick way to sanity-check arithmetic while experimenting.
Example: Standard Calculations
print(2 + 3)
Variables in Action
A variable is created the moment you assign it a value with =, and passing that variable's name to print() displays whatever it currently holds.
Because Python is dynamically typed, the same variable can be reassigned to a different type later without any special syntax.
Example: Variables in Action
value = 10 # value starts as an integer
print(value)
value = "ten" # same variable reassigned to a string
print(value)
Running the Program
Once your first script runs cleanly, it's worth deliberately introducing a typo (like a missing parenthesis) to see what a Python error message looks like — recognizing common errors early makes debugging far less intimidating once your programs grow.
Example: Running the Program
# Missing closing parenthesis -- try running this to see the error
print("Hello"
- Forgetting the parentheses in
print, as inprint "Hello", which is aSyntaxErrorin Python 3. - Leaving a string unquoted, as in
print(Hello), so Python looks for a variable calledHelloand raisesNameError. - Mixing quote types such as
print("Hello'), which leaves the string unterminated and gives aSyntaxError.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: