CS50P - Lecture 0 - Functions, Variables
Table of Contents
Introduction
This tutorial serves as an introduction to programming with Python, focusing on functions, variables, and basic programming concepts. Throughout this guide, you'll learn how to write simple Python programs, understand the syntax, and debug common errors. By the end, you will have a foundational understanding of how to create interactive programs that can handle user input and manipulate data.
Chapter 1: Setting Up Your Environment
-
Choose a Code Editor
- Download and install a text editor like Visual Studio Code (VS Code). Other options include Atom, Sublime Text, or even simpler ones like Notepad.
- You can also use online editors like Google Colab or Jupyter Notebook.
-
Create a New Python File
- Open your code editor and create a new file named
hello.py
. - Save the file with the
.py
extension, indicating it's a Python script.
- Open your code editor and create a new file named
-
Open a Terminal
- Use the built-in terminal in your code editor or open a command line interface (CLI) on your computer.
-
Run Your First Program
- In your
hello.py
file, type the following code:print("Hello, world!")
- Save the file and run it in the terminal by typing:
python hello.py
- You should see the output:
Hello, world!
- In your
Chapter 2: Understanding Functions and Arguments
-
What are Functions?
- Functions are predefined actions in programming that take input and produce output. In Python, you can use built-in functions like
print()
.
- Functions are predefined actions in programming that take input and produce output. In Python, you can use built-in functions like
-
Using the Print Function
- The
print()
function can take multiple arguments:print("Hello,", "David")
- This will output:
Hello, David
.
- The
-
Creating Your Own Function
- Functions can be defined using the
def
keyword:def greet(name): print("Hello,", name)
- Call the function:
greet("David")
- Functions can be defined using the
Chapter 3: Getting User Input
-
Using the Input Function
- To get input from the user, use the
input()
function:name = input("What's your name? ")
- This will prompt the user for their name and store it in the variable
name
.
- To get input from the user, use the
-
Combining Input and Functions
- Modify your previous function to greet the user with their name:
def greet(): name = input("What's your name? ") print("Hello,", name)
- Modify your previous function to greet the user with their name:
Chapter 4: Debugging Common Errors
-
Understanding Syntax Errors
- If you forget a closing parenthesis or a colon, Python will raise a syntax error.
- Example of a syntax error:
print("Hello, world" # Missing closing parenthesis
-
Handling Runtime Errors
- When using functions, ensure that variables exist in the correct scope. If you try to access a variable outside its defined scope, you'll get a
NameError
.
- When using functions, ensure that variables exist in the correct scope. If you try to access a variable outside its defined scope, you'll get a
Chapter 5: Advanced Function Techniques
-
Default Parameters
- You can set default values for function parameters:
def greet(name="world"): print("Hello,", name)
- You can set default values for function parameters:
-
Return Values
- Functions can also return values:
def square(n): return n * n
- Use the return value:
result = square(4) print(result) # Outputs: 16
- Functions can also return values:
-
Nested Functions
- You can define functions within other functions, but ensure that the inner function is defined before it is called.
Chapter 6: Working with Numbers
-
Basic Arithmetic
- Python can handle basic arithmetic operations:
x = 5 y = 3 print(x + y) # Outputs: 8
- Python can handle basic arithmetic operations:
-
Floating Point Numbers
- To handle decimal numbers, use floats:
a = 1.5 b = 2.5 print(a + b) # Outputs: 4.0
- To handle decimal numbers, use floats:
-
Type Conversion
- Convert strings to numbers using
int()
orfloat()
:num = float(input("Enter a number: "))
- Convert strings to numbers using
Conclusion
In this tutorial, you learned how to set up your Python environment, create functions, handle user input, and debug common errors. You also explored basic arithmetic and the use of return values in functions. As a next step, consider practicing by creating more complex programs that incorporate conditionals, loops, and error handling. This will further solidify your understanding of Python programming.