Introduction to Python
Python Basics
Getting Started with Python
Python is a popular programming language known for its straightforward, readable syntax. It's like writing in a simplified version of English, which makes it an excellent choice for beginners.
To start writing Python, you need a place to type and run your code. While you can install Python directly on your computer, the easiest way to begin is by using an online Integrated Development Environment, or IDE. These are websites that provide a code editor and a console to see your program's output, all within your browser. This lets you focus on learning to code without worrying about installation.
Writing Your First Code
Every programming language has rules about how to write commands. This is called syntax. Python's syntax is minimal and clean. One of its most important and unique rules is about indentation. The spaces at the beginning of a line are not just for looks; they structure the code. For now, just make sure all your code starts at the very beginning of the line, with no leading spaces.
Let's write a program. The most basic action is displaying information. In Python, we use the print() function to do this. Whatever you put inside the parentheses will be shown on the screen.
print("Hello, World!")
When you run this code, the text Hello, World! appears. The quotation marks tell Python that this is a piece of text, also known as a string.
Storing Information
Programs need to remember information. We store data in variables, which are like labeled boxes where you can keep values. To create a variable, you just need to pick a name and use the equals sign (=) to assign it a value.
# Create a variable named 'greeting' and store a message in it
greeting = "Welcome to Python!"
# Print the value that is stored in the variable
print(greeting)
Variable names can contain letters, numbers, and underscores, but they cannot start with a number. They are also case-sensitive, so
nameandNamewould be two different variables.
Once you store a value in a variable, you can use it, change it, and refer to it throughout your program. You can also reassign a new value to an existing variable.
year = 2023
print(year)
# The value can be updated
year = 2024
print(year)
Basic Data Types
Variables can hold different kinds of data. Python automatically understands the type of data you assign. The most common basic types are strings, integers, and floating-point numbers.
string
noun
A sequence of characters, such as text. In Python, strings are enclosed in either single quotes ('...') or double quotes ("...").
An integer (int) is a whole number, positive or negative, without any decimal points. A floating-point number, or float, is a number that has a decimal point.
| Data Type | Description | Example Code |
|---|---|---|
String (str) | Textual data | message = "Python is fun" |
Integer (int) | Whole numbers | user_age = 25 |
Float (float) | Numbers with decimals | item_price = 19.99 |
To see what type of data a variable holds, you can use the type() function.
user_age = 25
item_price = 19.99
print(type(user_age))
print(type(item_price))
Running this code would output <class 'int'> and <class 'float'>.
Interacting with the User
So far, our programs have only displayed predefined information. To make them interactive, we can ask the user for input. The input() function pauses the program, displays a message to the user, and waits for them to type something and press Enter.
# The text in the parentheses is the prompt shown to the user
user_name = input("Please enter your name: ")
# Greet the user with the name they provided
print("Hello, " + user_name + "!")
Whatever the user types is captured as a string and stored in the user_name variable. In the print statement, the + sign is used to combine, or concatenate, strings together.
Important: The
input()function always returns the user's entry as a string, even if they type numbers. If you need to perform math with the input, you must first convert it to a numeric type likeintorfloat.
age_input = input("Enter your age: ")
# Convert the string from input() into an integer
age_number = int(age_input)
# Now you can do math with it
years_until_100 = 100 - age_number
# We need to convert the number back to a string to print it with other text
print("You will be 100 in " + str(years_until_100) + " years.")
Notice how int() converts a string to an integer, and str() converts a number back into a string so it can be combined with other text for printing.
Now let's check your understanding of these core concepts.
What is the output of the following Python code?
print("Hello, World!")
After the following line of code is executed, what is the data type of the user_age variable?
user_age = 25
You've just taken your first steps in Python. You now know how to display messages, store information in variables, recognize basic data types, and interact with a user. This foundation is the starting point for building any program.
