0% found this document useful (0 votes)
15 views5 pages

Untitled Document

Uploaded by

rehaansamma18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Untitled Document

Uploaded by

rehaansamma18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS50P's Lesson 0: Functions introduces fundamental programming concepts in Python,

focusing on functions, variables, conditionals, user input, and basic formatting like f-strings.
Here’s a comprehensive summary with examples to help solidify each concept:

1. Introduction to Functions

● Functions are reusable blocks of code that perform a specific task.


● They help organize code, reduce repetition, and make it more readable.

The print() function is the most basic example:


python
Copy code
print("Hello, world!")
Output:
Copy code
Hello, world!


● The print() function displays text or variables on the screen.

2. Defining Functions
You can create your own functions using the def keyword:
python
Copy code
def greet():
print("Hello!")

Calling the function:


python
Copy code
greet()
Output:
Copy code
Hello!

Functions can take parameters to make them more dynamic:


python
Copy code
def greet(name):
print(f"Hello, {name}!")

Calling the function with an argument:


python
Copy code
greet("Alice")
Output:
Copy code
Hello, Alice!

3. Variables

● Variables store values that can be used and manipulated in the program.

Example of declaring a variable:


python
Copy code
name = "Alice"
age = 30

You can use these variables in functions or print them directly:


python
Copy code
print(f"My name is {name} and I am {age} years old.")
Output:
csharp
Copy code
My name is Alice and I am 30 years old.

4. User Input
Python allows you to gather user input with the input() function:
python
Copy code
name = input("What's your name? ")
print(f"Hello, {name}!")


○ When you run this, it asks the user for their name and greets them.

Input is always stored as a string, so if you need a number, you can convert it:
python
Copy code
age = int(input("How old are you? "))
print(f"Next year, you will be {age + 1} years old.")

5. Return Values
Functions can return values using the return statement:
python
Copy code
def square(x):
return x * x

Using the function:


python
Copy code
result = square(4)
print(result)
Output:
Copy code
16


● The square() function takes a number x, multiplies it by itself, and returns the result.

6. Conditionals
Conditionals are used to make decisions in your program with if, elif, and else:
python
Copy code
age = 18
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
Output:
sql
Copy code
You are an adult.


● Conditionals allow your code to behave differently depending on the values of variables.

7. F-strings (Formatted Strings)


F-strings provide a way to embed expressions inside strings:
python
Copy code
name = "Bob"
print(f"Hello, {name}. Welcome!")

F-strings can include expressions:


python
Copy code
x = 5
y = 7
print(f"The sum of {x} and {y} is {x + y}.")
Output:
python
Copy code
The sum of 5 and 7 is 12.

8. Example Program: A Simple Greeting


Putting all these concepts together, you can create a simple program that takes a user’s name
and age, and returns a customized greeting:
python
Copy code
def greet(name, age):
return f"Hello, {name}. You are {age} years old."

name = input("What's your name? ")


age = int(input("How old are you? "))
message = greet(name, age)
print(message)

Example interaction:
rust
Copy code
What's your name? Alice
How old are you? 30
Hello, Alice. You are 30 years old.

9. Best Practices

● Use meaningful variable names: Choose names that describe their purpose (e.g.,
name, age).
● Keep functions small: Each function should do one thing well, making them easier to
test and maintain.
● Test your functions: Use different inputs to ensure your functions behave as expected.
● Comment your code: Use comments (#) to explain complex parts of your code.

10. Summary

● Lesson 0 is foundational for understanding how Python programs are structured using
functions, variables, and conditionals.
● Mastering these basics will help with more advanced topics in future lessons.

By grasping these core ideas, you'll have a strong foundation to build more complex programs
as you progress through CS50P. Each concept is introduced to make Python easier and more
intuitive to use, especially as you start writing your own functions and programs.

4o
ChatGPT can make mistakes. Check important info.

You might also like