Python Fundamentals for AI Enthusiasts
Python Basics
Getting Started with Python
Python is a popular programming language known for its clear and readable syntax. It's often recommended for beginners because it lets you write powerful programs with fewer lines of code than many other languages. Let's dive into the first steps of writing your own Python programs.
Setting Up Your Workspace
Before you can write code, you need a place to run it. Every computer needs a program called an interpreter to understand and execute Python commands. While you can install Python directly on your computer, the easiest way to start is by using an online Python editor. These websites provide a ready-to-use environment right in your browser, so you can start coding immediately without any setup.
Your First Program
A long-standing tradition in programming is to make your first program display the message "Hello, World!". It's a simple way to confirm that your setup is working correctly. In Python, this is incredibly straightforward.
print("Hello, World!")
This line of code uses Python's built-in print() function. A function is a reusable block of code that performs a specific action. The print() function's job is to display output to the screen. Whatever you put inside the parentheses will be printed. Here, we've given it a piece of text, which in programming is called a string.
In Python, strings of text must be enclosed in either single quotes ('...') or double quotes ("...").
Variables and Data Types
Programs need to store and manage information. We do this using variables. Think of a variable as a labeled box where you can keep a piece of data. You give the box a name and use the equals sign (=), known as the assignment operator, to place a value inside it.
# Create a variable named 'message'
message = "This is stored in a variable."
# Print the value of the variable
print(message)
In this example, we created a variable named message and assigned it the string value "This is stored in a variable.". When we pass the variable message to the print() function, Python looks up the value stored in that variable and displays it.
Note that when you print a variable, you don't put quotes around its name.
print(message)prints the value inside the variable, whileprint("message")would just print the word "message".
Variables can hold different kinds of data. These categories are called data types. For now, we'll look at the three most common ones.
| Data Type | Description | Example |
|---|---|---|
String (str) | A sequence of characters (text). | "Hello" or 'Python' |
Integer (int) | A whole number, without a decimal. | 10, -5, 2024 |
Float (float) | A number with a decimal point. | 3.14, -0.5, 100.0 |
Python automatically determines the data type when you assign a value to a variable. You can see this in action below.
# A string variable
user_name = "Alex"
# An integer variable
user_age = 25
# A float variable
user_score = 95.5
print(user_name)
print(user_age)
print(user_score)
Getting User Input
So far, our programs have been static. They do the same thing every time. To make them interactive, we can ask the user for information. Python's input() function allows us to do just that.
# Ask the user for their name
name = input("What is your name? ")
# Greet the user
print("Hello, " + name + "!")
When Python runs the input() function, it displays the message inside the parentheses (called a prompt) and then pauses, waiting for the user to type something and press Enter. Whatever the user types is captured as a string and stored in the name variable.
Notice the line print("Hello, " + name + "!"). The plus sign (+) can be used to join strings together. This is called concatenation.
The
input()function always returns the user's input as a string, even if they type a number. We'll explore how to convert this to other data types like integers or floats later on.
Which Python function is used to display output on the screen?
What is the primary purpose of a variable in Python?
You now have the basic building blocks for writing simple Python programs. You can display messages, store data in variables, and interact with users.