Notes on Python Basics

These notes explain some simple ideas in the Python computer language.

What is Python?

  • Python is a language that helps us talk to computers.
  • We write instructions (code) in Python, and the computer follows them.

Comments: Notes for Humans

  • Sometimes we want to write notes in our code just for humans to read.

  • The computer ignores these notes. They are called comments.

  • Single-Line Comment: Starts with #. The computer ignores everything after # on that line.

    # This is a note for me!
    print("Hello") # This part is also a note
  • Multi-Line Comment: Starts and ends with three quotes ("""). The computer ignores everything between them.

    """
    This is a longer note.
    It can span multiple lines.
    The computer will not read this.
    """
    print("Computer reads this!")

Input: Getting Information from the User

  • Sometimes we need the computer to ask the user a question and get an answer. This is called input.

  • We use the input() instruction for this.

  • Example: Asking for your name.

    # Ask the user for their name and store it
    your_name = input("What is your name? ")

    The computer will show “What is your name? ” and wait for you to type something.

Output: Showing Information to the User

  • We often want the computer to show us something on the screen. This is called output.

  • We use the print() instruction for this.

  • Example: Saying hello using the name we got from input.

    your_name = input("What is your name? ")
    print("Hello, ")
    print(your_name) # Shows the name you typed
    print("Nice to meet you!")

    If you typed “Alex”, the computer would show:

    Hello,
    Alex
    Nice to meet you!
    

Data Types: Different Kinds of Information

Computers need to know what kind of information they are working with. These kinds are called data types. Here are a few common ones:

  1. String: Text, like words or sentences. Always put quotes (" or ') around strings.

    • Examples: "Hello there", 'cats', "123" (this is text, not a number!)
    greeting = "Hi how are you"
    print(greeting)
  2. Integer: Whole numbers. No decimal points allowed. Can be positive, negative, or zero.

    • Examples: 10, -5, 0, 999
    my_age = 15
    print(my_age)
  3. Float: Numbers with a decimal point.

    • Examples: 3.14, -0.5, 10.0 (even if the decimal part is zero, the dot makes it a float)
    price = 19.99
    print(price)
  4. Boolean: Represents truth values. Can only be True or False. Think of it like “yes” (True) or “no” (False).

    • Examples: True, False
    is_raining = False
    print(is_raining)
  • We can ask Python what type a piece of data is using type().
    number = 5
    print(type(number)) # This will show <class 'int'> which means Integer
     
    message = "Good morning"
    print(type(message)) # This will show <class 'str'> which means String