Python Programming Basics: Functions & Variables

Aug 10, 2024

CS50's Introduction to Programming with Python - Week on Functions and Variables

Overview

  • Lecture by David Malan
  • Focus on basics of programming and Python.

Getting Started with Python

  • Use Visual Studio Code (VS Code) for writing code.
  • Code is ultimately just text; any text editor can be used to write it.
  • Python file names typically end with .py.

Writing Your First Program

  • Example: print("hello, world")
    • This program prints "hello, world" to the screen.
    • Running a program is done via command line using python hello.py.

Functions in Programming

  • Functions are actions or verbs that allow the programmer to perform tasks.
  • Example function: print()
    • Takes arguments which influence its behavior.
    • Arguments are inputs to functions, influencing output.
  • Output can be a side effect, like displaying text on the screen.

Common Errors in Programming

  • Syntax errors occur when code is not written correctly (e.g., missing parentheses).
  • Bugs are mistakes in the program; debugging involves identifying and fixing these issues.

User Input

  • Using input() to prompt users for input (e.g., "What's your name?").
  • Input is always returned as a string.
  • To use the input value further, store it in a variable.

Variables

  • Variables are containers for storing values (e.g., name).
  • Assignment uses = to store values from right to left.
  • Example: name = input("What's your name?")

Comments and Code Documentation

  • Comments in code are written using # and are ignored by the interpreter.
  • Useful for explaining code and intentions.

Improving the Program

  • Making the greeting more dynamic by using user input rather than hard-coding the name into the program.
  • Example: print(f"hello, {name}") using f-strings for cleaner output.

String Manipulation

  • Built-in string functions/methods allow for cleaning input (e.g., strip(), capitalize(), title()).
  • split() can break strings into smaller substrings.
  • Example: first, last = name.split() to separate first and last names.

Working with Numbers

  • Python supports integers (int) and floating-point numbers (float).
  • Arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/).
  • The modulo operator (%) gives the remainder of the division.

Interactive Python

  • Python supports interactive mode for immediate code execution and feedback.
  • Use as a calculator for simple math.

Creating a Simple Calculator

  • Building a basic calculator program using user input and arithmetic operations:
    x = float(input("What's x? "))
    y = float(input("What's y? "))
    print(x + y)
    
  • Handling potential errors when user inputs non-numeric values.

Defining and Using Functions

  • Custom functions can be defined using def.
  • Example: def hello(name): to say hello to a specific user.
  • Functions can take parameters and return values using return keyword.

Scope of Variables

  • Variables defined in functions only exist within that function's scope.
  • Passing variables between functions demonstrates scope (e.g., name variable).

Final Notes

  • Building programs incrementally using functions helps with organization and readability.
  • Use of main function to organize code and manage execution flow.
  • Python allows defining default parameter values in functions for flexibility.

Conclusion

  • The lecture covered fundamental programming concepts with Python, focusing on functions, variables, and user input.
  • Encourages hands-on practice and experimentation with code.