CS50 Python
{Visual Studio Code with CS50}
VS Code (Visual Studio Code) is a popular text editor and Integrated Development Environment
(IDE) used for coding. An IDE is a software application that helps programmers efficiently write
and run code. CS50 recommends using VS Code in the cloud via code.cs50.io, which runs on
GitHub Codespaces, a virtual environment that eliminates the need for local installation. Inside
VS Code, the File Explorer (top left) displays project files, the Editor Tabs (top center) allow for
writing code, and the Terminal (bottom) provides a Command Line Interface (CLI), which
enables direct interaction with the computer using text-based commands. The CLI is essential
for navigating files, running programs, and managing the system efficiently. Linux, a widely
used operating system for programming, powers the cloud environment. Some key terminal
commands include code filename.py to create a file, python filename.py to run a
Python program, ls to list files in a directory, cp to copy files, mv to rename or move them, and
rm to delete them. Folders can be managed using mkdir to create a directory, cd to enter it, cd
.. to move back, type clear in the terminal to clear it, and rmdir to remove an empty directory.
The terminal also supports shortcuts like Tab for autocomplete and the Up Arrow to recall
previous commands. Mastering these basics allows for efficient coding in VS Code for CS50
without relying solely on a graphical interface.
{Functions}
A program is essentially a set of instructions that a computer follows to perform a task. In this
case, we use Python, a programming language that provides tools such as conditionals,
variables, loops, and functions. A function is a block of code that takes an input and produces
an output, similar to a verb in a sentence. One of Python’s built-in functions is print(), which
takes text as input and displays it as output in the terminal. To demonstrate, we create a file
named hello.py and write a simple program using the print() function to display "Hello,
world!" and "This is CS50P." Functions in Python are identified by their name followed by
parentheses, which may or may not contain input values. To execute a Python program, we run
python hello.py, executing the code from top to bottom. Additionally, programmers can
define their own functions using the def keyword, followed by the function name, parentheses,
and a colon. Inside the function, indented lines specify the code to be executed. In our example,
we define a function main() and move our print() statements inside it. However, defining a
function is different from calling it—Python does not execute a function until explicitly called. We
call the main() function by adding main() two spaces away. Many built-in functions, like
print(), are predefined by Python developers, and as programmers, we can create our own
functions to make code more modular and efficient.