💻

Lecture 2 of 6.01 and 6.00: Programming Basics in Python

Jul 9, 2024

Lecture 2 of 6.01 and 6.00: Programming Basics in Python

Overview

  • Recap of last lecture: computers do what they're told by programmers through written programs.
  • Introduction to a new object type: strings.
  • More aspects of programming: branching and loops.

Recap of Lecture 1

  • Computers only follow instructions given by programmers.
  • Simple objects discussed: integers, floats, booleans.

Strings

  • Definition: Sequences of characters (letters, digits, special characters, spaces).
  • Syntax: Enclosed in quotation marks (single or double).
  • Example: hi = "hello there"

Operations on Strings

  1. Concatenation: Combining strings using the + operator.
    • Example: hi + name results in hello thereAna (no space added automatically).
    • To add space: hi + " " + name results in hello there Ana.
  2. Repetition: Using the * operator to repeat strings.
    • Example: hi * 3 results in hello therehello therehello there.

Print Function

  • Usage: Displays output to the user.
  • Syntax:
    • Commas , insert spaces between items automatically.
    • Plus + concatenates items without spaces; all items must be strings.
  • Example: x = 1 print("My fav num is", x) # results in spaces print("My fav num is" + str(x)) # no spaces

User Input

  • Function: input(), prompts user and reads input as a string.
  • Example: text = input("Type anything: ") print(5 * text)
  • Casting: Convert user input to specific types (int, float).
    • Example: num = int(input("Give a number: "))

Comparison and Logic Operators

  • Comparison Operators: >, <, >=, <=, ==, != for integers, floats, strings.
    • Example: if i == j, if a != b.
  • Logic Operators: not, and, or for booleans.
    • Example: if not a if a and b if a or b

Branching

If Statements

  • Syntax: if condition: # code block
  • Executes code block if condition is true.

If-Else Statements

  • Syntax: if condition: # code block else: # another code block
  • Executes one of the two code blocks based on the condition.

If-Elif-Else Statements

  • Syntax: if condition1: # code block elif condition2: # another code block else: # fallback code block
  • Chooses between multiple conditions; executes first true condition's block.

Loops

While Loops

  • Usage: Repeatedly execute a block of code as long as condition is true.
  • Syntax: while condition: # code block
  • Example: Prompt user input until a condition is met. while user_input == "right": user_input = input("Go left or right: ")

For Loops

  • Usage: Iterate over a sequence of numbers or elements.
  • Syntax: for variable in range(stop): # code block
  • Example: Counting iterations. for i in range(5): print(i)
  • Range Variations:
    • range(stop): 0 to stop-1.
    • range(start, stop): start to stop-1.
    • range(start, stop, step): start to stop-1 with increments of step.

Breaking Loops

  • Break Statement: Exits loop prematurely when condition is met.
  • Syntax: while condition: # code block if break_condition: break

Comparing For and While Loops

  • For Loops: Use when number of iterations is known.
  • While Loops: Use for unpredictable iteration counts (e.g., user input).
  • Both support early termination with break statements.

Summary

  • Strings, print, input, casting, comparison, logic operators, branching, loops.
  • Practical examples and differences between loop types.
  • Importance of indentation in Python for readability and code block definition.