Programming lesson
Linux Command Line and Python Basics: A Hands-On Lab Tutorial for CSCI 141
Master the Linux command line, Python type conversions, operators, print formatting, and error handling with this step-by-step lab tutorial. Perfect for CSCI 141 students and beginners.
Introduction: Why Linux and the Command Line Matter in 2026
In the world of programming, the command line is like the backstage pass to a concert. While graphical interfaces (like Windows or macOS) are the stage where most users interact, the command line gives you direct control over the system. This lab, originally from CSCI 141 Spring 2019, remains foundational for understanding how operating systems work under the hood. Today, with the rise of cloud computing, AI development, and DevOps, knowing Linux commands is more valuable than ever. Whether you're deploying a machine learning model on a remote server or automating tasks on your laptop, the command line is your best friend.
In this tutorial, you'll learn to:
- Log into a Linux system (Ubuntu)
- Navigate the file system using terminal commands
- Run Python scripts from the command line
- Understand type conversions, operators, and print formatting
- Identify and fix common errors
1. Logging into Linux
Linux is an operating system that powers everything from smartphones to supercomputers. The lab machines are dual-boot, meaning you can choose between Windows and Ubuntu (a popular Linux distribution) at startup. When the boot menu appears, select Ubuntu and press Enter. You'll see a login screen similar to Figure 1. Enter your CS account username and password (the same ones you used for Windows in Lab 1). Once logged in, you can customize your desktop environment by clicking the gear icon near the login button.
To access system settings, click the power icon in the upper right and select System Settings. To find Thonny (a Python IDE), click the Ubuntu icon (top-left) and type Thonny in the search bar. Thonny should look very similar to the Windows version you used last week.
2. Linux Command Line Basics
All operating systems provide a graphical interface, but they also offer a Command Line Interface (CLI). While the CLI has a steeper learning curve, it is more powerful for tasks like file manipulation, automation, and remote server management. In this lab, you'll learn the basics of the Linux command line and how to run Python code without an IDE.
Opening a Terminal
Click the Ubuntu icon (top-left), type terminal, and click the Terminal icon. A window with a $ prompt will appear. The $ is called the command line, and the program that interprets your commands is called a shell (the default is bash). The prompt shows your username, the computer name, and the current directory: for example, username@linux-11:~$.
Basic Commands
Let's start with a simple command: whoami. Type it and press Enter. What does it output? It prints your username.
Commands can take arguments, similar to function arguments in Python, but without parentheses. To create a new directory (folder), use mkdir followed by the directory name:
mkdir Lab2To verify, list the contents of the current directory with ls:
lsYou should see Lab2 listed. To navigate into that directory, use cd (change directory):
cd Lab2Launching Thonny from the Command Line
Now, launch Thonny from the terminal:
thonny &The & allows you to continue using the terminal after launching Thonny. In Thonny, create a new file named helloWorld.py with a single line:
print("hello world")Save it in your Lab2 folder. Return to the terminal and run ls to confirm the file exists. Then, run the program using the Python3 interpreter:
python3 helloWorld.pyYou should see hello world printed in the terminal. This demonstrates how to run Python scripts without an IDE—a skill essential for working on remote servers or automated pipelines.
3. Type Conversions, Operators, and Operands
Python is a dynamically typed language, but understanding type conversions is crucial to avoid bugs. In this section, we'll explore implicit and explicit conversions, operators, and how they interact.
Implicit vs. Explicit Conversion
When you mix types, Python sometimes converts automatically (implicit) but often raises an error. For example:
print(10 + 5.5) # Implicit conversion: int to float → 15.5
print("10" + 5) # TypeError: can only concatenate str (not "int") to strTo fix the second line, you need explicit conversion:
print(int("10") + 5) # 15Common Operators
- Arithmetic:
+,-,*,/,//(floor division),%(modulus),**(exponentiation) - Comparison:
==,!=,<,>,<=,>= - Logical:
and,or,not
Operators have precedence rules. For example, multiplication happens before addition: 2 + 3 * 4 yields 14, not 20. Use parentheses to control order: (2 + 3) * 4 → 20.
Operands and Data Types
Operands are the values that operators act on. Their type matters. For instance, the + operator works for both numbers and strings, but with different meanings: addition vs. concatenation. Mixing incompatible types leads to errors. Always check your types using type():
print(type(42)) # <class 'int'>
print(type("42")) # <class 'str'>4. Print Formatting: Making Output Pretty
Python's print() function is versatile. You can control the separator between items using the sep argument, and the end character using end.
The sep Parameter
By default, print() separates multiple arguments with a space. You can change that:
print("Hello", "World", sep="-") # Hello-WorldThis is useful for formatting CSV-like output or custom messages.
The end Parameter
By default, print() ends with a newline. You can change it:
print("Hello", end=" ") # No newline, just space
print("World") # Output: Hello Worldf-Strings (Modern Formatting)
Python 3.6+ introduced f-strings, which are concise and readable:
name = "Alice"
age = 20
print(f"{name} is {age} years old.") # Alice is 20 years old.You can also format numbers: f"{value:.2f}" for two decimal places.
5. Errors: Your New Best Friends
Errors are inevitable. The key is to read them carefully. Python errors come in three main types:
- SyntaxError: You broke Python's grammar rules (e.g., missing colon).
- NameError: You used a variable that doesn't exist.
- TypeError: You performed an operation on incompatible types.
For example, download the file badCode.py from the course website and save it in your Lab2 folder. Its contents might include intentional bugs. Try running it and fix each error. The error message tells you the line number and the problem. For instance:
Traceback (most recent call last):
File "badCode.py", line 3, in <module>
print("Hello" + 5)
TypeError: can only concatenate str (not "int") to strTo fix, convert the integer to a string: print("Hello" + str(5)).
6. Connecting to Trends: Why This Matters Now
In 2026, the command line is more relevant than ever. With the explosion of AI and machine learning, developers often run models on remote Linux servers. Knowing how to navigate the file system, run scripts, and handle errors is essential. For example, when fine-tuning a large language model like GPT-5, you might use the command line to manage datasets, run training scripts, and parse logs. Similarly, DevOps and cloud computing rely on shell commands for automation. Even in gaming, Linux powers platforms like Steam Deck, where users might tweak system settings via terminal.
Understanding type conversions and operators also applies to financial calculations in apps like Robinhood or Coinbase. For instance, calculating portfolio returns requires careful handling of floats and integers. And print formatting is used in data science to present results in Jupyter notebooks or reports.
7. Practice Exercises
Try these challenges to reinforce your learning:
- Create a directory called
Lab2_Practiceand navigate into it. - Write a Python script that prints your name, age, and favorite hobby using f-strings.
- Write a script that converts a temperature from Celsius to Fahrenheit and prints the result with two decimal places.
- Intentionally introduce a bug (e.g., typo in a variable name) and fix it using the error message.
Conclusion
You've now completed a hands-on introduction to Linux commands, Python type conversions, operators, print formatting, and error handling. These skills form the foundation for more advanced programming and system administration. Remember, the command line is not just a tool—it's a superpower. Keep practicing, and soon you'll be navigating Linux like a pro.
For further reading, check out the Linux command cheat sheet on the course website. Happy coding!