Lesson 2.1: The Programming Environment, Variables and Data Types
Introduction
Welcome to Lesson 2.1 of Foundation Computing! In this lesson, we will delve into the basics of programming with Python. Our objectives today are to help you:
- Write, save, and run a Python program.
- Understand the interpreter, the REPL, and an IDE/editor.
- Learn about variables, assignment, and naming conventions.
- Explore core data types: integer, float, string, Boolean, and how Python infers type.
- Read user input and produce formatted output.
- Write, save, and execute a simple Python program that reads input and generates output.
Let's hook into the programming world! 💻
The Programming Environment
Before we can write code, we need to know where to write it! The programming environment is where we write, test, and debug our Python programs.
What is an Interpreter?
An interpreter is a program that reads your code and executes it line by line. This means you can see the results of your code quickly. Python comes with a built-in interpreter that you can use directly in the command line or terminal.
REPL: Read-Eval-Print Loop
REPL stands for Read-Eval-Print Loop. It’s an interactive programming environment that takes your inputs (your code), evaluates them (executes), and then prints the output. You can use the REPL to try out small snippets of code quickly! Here’s how to start it:
- Open your terminal or command prompt.
- Type
pythonorpython3and hit Enter. - Now you can type Python code, and it will execute immediately! For example:
>>> print("Hello, students!")
Hello, students!
IDEs and Code Editors
Integrated Development Environments (IDEs) are software applications that provide comprehensive facilities to programmers. Examples include PyCharm, VS Code, and Anaconda. These tools come with features like syntax highlighting, debugging support, and project management. They make coding in Python easier and more efficient.
Variables in Python
A variable is a way to store information in your program. It acts as a label for a value stored in memory. Naming conventions for variables are important to make your code readable.
Naming Conventions
When you name a variable in Python, follow these rules:
- Variable names can include letters (a-z, A-Z), numbers (0-9), and underscores (_).
- Variable names must start with a letter or an underscore.
- Avoid using spaces and special characters.
- Use descriptive names like
student_age,total_price, oris_onlineto clearly indicate what the variable represents.
Assigning Values
You can assign a value to a variable using the equals sign (=). For example:
age = 17
name = "students"
In this example, age is a variable that holds the integer value $17$, and name holds the string value "students".
Core Data Types
Python has several built-in data types that you will regularly use:
1. Integers
These are whole numbers (positive, negative, or zero). For example:
number_of_students = 30
An integer value like $30$ can be stored in the variable number_of_students.
2. Floats
Floats are numbers that have a decimal point. For instance:
average_grade = 85.5
Here, average_grade stores the float value $85.5$.
3. Strings
Strings are used to represent text. You can create a string by enclosing text within quotes:
course_name = "Foundation Computing"
In this case, course_name holds the string "Foundation Computing".
4. Booleans
Booleans represent truth values: true or false. For example:
is_student_active = True
This indicates that is_student_active is set to true.
Type Inference
Python is a dynamically typed language, which means you don’t have to explicitly declare the type of a variable. Python infers the variable type based on the value assigned to it. For example:
x = 5 # x is inferred as an integer
y = 5.0 # y is inferred as a float
z = "Hello" # z is inferred as a string
Input and Output
In programming, we often need to interact with users. We can collect user input and display output using Python's built-in functions.
Reading Input
To read input from a user, we use the input() function. For example:
user_name = input("Enter your name: ")
This code will prompt the user to enter their name and store it in the variable user_name.
Producing Output
To produce output, use the print() function. You can also format your output to make it cleaner. For example:
print(f"Hello, {user_name}!")
This line will greet the user with their entered name!
Writing Your First Python Program
Now, let’s put everything together and write a simple Python program that reads user input and prints a formatted message!
Example Program
# This program greets the user
user_name = input("Enter your name: ")
print(f"Hello, {user_name}! Welcome to Foundation Computing.")
Here’s what happens:
- The program prompts the user to enter their name.
- It captures that input in the variable
user_name. - Finally, it prints a greeting message using the entered name!
Saving and Running the Program
- Open your code editor or IDE.
- Type or paste the code into the editor.
- Save the file with a
.pyextension, such asgreet.py. - Run the program through the terminal or your IDE's run function.
Conclusion
In this lesson, we learned about the programming environment, how to define and use variables, the core data types in Python, reading user input, and producing output. By understanding these basics, you're now ready to start writing simple Python programs! 🎉
Study Notes
- An interpreter is a program that executes your Python code.
- REPL allows for quick interactive coding.
- Variables store information in memory using descriptive names.
- Python has several core data types: integer, float, string, and Boolean.
- Use
input()to read user input andprint()to display output. - Save Python programs with a
.pyextension and run them with an interpreter.
