Ahaan

Big Idea 3.1 Lesson Plan

  • Numbers can be set as strings but must also be included in “ “ or using the str() function.
  • These can be exteremely usesful in if statements.
  • Hacks
  • Experimenting with the + operator on string types, integer types, and float types.
  • Homework Problems on Variables
  • Variables

    Variables act as a storage containers, which you can use to store information/code to call at any point in your program.

    For Example

    example = 3
    starter = "Hello world"
    value= "variable"
    

    Then.. we can do

    print(starter)
    print(example)
    print(value)
    
    Hello world
    3
    variable
    

    We use variables because it allows us to have more efficency when creating our code. There are many ways to excute and code a program, however we always want to find the most efficent way. Let’s look at this example

    print("Hello World, Welcome")
    print("Hello World, Welcome")
    print("Hello World, Welcome")
    print("Hello World, Welcome")
    print("Hello World, Welcome")
    
    Hello World, Welcome
    Hello World, Welcome
    Hello World, Welcome
    Hello World, Welcome
    Hello World, Welcome
    

    This is really inefficent and time consuming, also repetive instead we can make a variable assignment,.

    hi= "Hello World, Welcome"
    
    print(hi)
    print(hi)
    print(hi)
    print(hi)
    print(hi)
    
    
    Hello World, Welcome
    Hello World, Welcome
    Hello World, Welcome
    Hello World, Welcome
    Hello World, Welcome
    

    This is way less effort you need to put in, and also, much more efficent code than typing hello world 5 times. There is an even more efficent way to set this code up with For Loops, but that is for a later lesson,

    Variable Naming:

    There are 3 Important Coding Practices to follow when it comes to naming variables

    SnakeCase

    SnakeCase is where you replace spaces in the words in a variable names to an underscore. This is the standard naming convention for variables in Python.

    PascalCase

    PascalCase is where you capitialize every word in your variable, but keep it all as one singluar phrase with no spaces. Altough this is shown in example, it should not be used for varialbes in Python. This is reserved for class names.

    CamelCase

    CamelCase is where you captalize the second and subsequent words in the variable name. This is not normally used in Python conventions.

    
    

    Popcorn Hack # 1 25%

    Try making your own PascalCase variable and set the variable to a string.

    Popcorn Hack # 2 25%

    Now try making your own CamelCase variable and set the variable equal to a integer.

    
    

    Variable Types

    In Python there are many variables Some include…

    Integers

    Integers are numerical values such as 1, 2, 3, 4 or -1. There are no decimals in an integer. ‘’’

    These values must be whole digit numbers

    int_var = 10
    

    Strings

    Strings are a chain of text, numbers or charcters, all inside of “ “ ‘’’

    Numbers can be set as strings but must also be included in “ “ or using the str() function.

    # Numbers can be set as strings but must also be included in " " or using the str() function.
    str_var = "Hello World"
    
    # Convert any value into a string using the str() function
    int_var = 104
    str_var = str(int_var) # Converted 104 to "104"
    
    # You can access different parts of the string using brackets
    letter_one = str_var[0]
    
    # You can also get groups of letters using brackets
    half = str_var[0:5]
    
    # To split the string into parts you can choose which parts to split at and then it will convert into a list
    str_var.split(" ") # Split at space making str_var = ["Hello","World"]
    
    # To rejoin split strings, use the join() function
    rejoin_str = ","
    rejoin_str.join(str_var) # the join() function joins a list using a rejoin string, in this example a commmad
    
    '1,0,4'
    

    Boolean

    Booleans are True or False, they are used for condtional statements (Usually in if statments or while loops). ‘’’

    These can be exteremely usesful in if statements.

    bool_var = True 
    
    if bool_var:
        int_var = 10
    else:
        str_var = "Hello World"
    

    Float

    Floats are numbers that can include decimals.

    # Floats can be whole numbers to but must include a .0 after the whole number to be a float number.
    float_var = 0.5
    

    Lists

    Lists are ordered collections of items in Python. They can contain a mix of different data types, including integers, floats, strings, and more. However, it is more common to have list contain the same data type.

    
    
    ```python
    # Lists are very useful to store data for a later use, its like a bunch of variables compacted into one.
    lst = ["s", "i", "g", "m", "a"]
    
    # To retrieve something from a list, you take the index or the list number like this.
    lst[0]
    
    lst[0:3] # You can grab values from one index to another like in strings
    
    # Index can also be found with the list.index() function
    lst.index("g")
    
    # To add a value to the list, you can use the list.append() function
    lst.append("69")
    
    Output: lst = ["s", "i", "g", "m", "a", 69] # Lists can include different kinds of values (int, str, bool, float, list)
    
    # To remove a value from the list, you can use the list.remove() function
    lst.remove("s")
    
    Output: lst = [ "i", "g", "m", "a", 69]
    
    
    

    Popcorn Hack # 3 25%

    Try making your own set of 3 variable variables.

    It can be anything. Use your creativity!

    Try a String or a Float

    This an example of variables in Javascript.

    %%js
    
    
    console.log("Function Definition");
    
    // Function: logIt
    // Parameter: msg
    // Description: The parameter is "msg" is output to console, jupyter and "output" element in HTML
    
    function logIt(input) {
        console.log(input); 
        element.append(input);
        document.getElementById("output").textContent = input;
    };
    
    // Using function to add message to console log
    logIt(msg);
    
    // sequence of code build logIt parameter using concatenation
    var msg = "Hello, Students!"; // replaces content of variable
    
    logIt(msg); // This will print "Hello, Students!" to the browser's console
    
    
    
    
    <IPython.core.display.Javascript object>
    

    Hacks

    Review each of the sections above and produce a python program that stores:

    • Name as a string
    • Age as a integer
    • Favorite food as a string
    • Mix the name, age, food into a List and a Dictionary
    • Be sure to follow Snake case convention for your variables
    • Build your own code cell(s) that define each variable types
    • Experiment with the + operator on string types, integer types, and float types. What operations can the + operator perform?
    • Provide comments and outputs in the cell that are easy to follow
    ### Store as a Dictionary
    
    my_dict = {
        "name": "Ahaan",
        "age": 15,
        "Favorite Food": "Pizza"
        "status": "Not as cool as spencer"
    }
    
    ### Store as regular variables which can be called at any time
    
    favorite_food = "Pizza"
    my_name = "Ahaan Vaidyanathan"
    my_age = 15
    status = "Not as cool as spencer"
    
    ### Store as a List
    
    my_list = ["Ahaan", 15, "Pizza"]
    
     
    
    Hi I am 15 years old
    Congrats for Learning 3.1
    

    Experimenting with the + operator on string types, integer types, and float types.

    There are multiple examples, here for this example. Remember, we have to use the str conversion when combining a interger or float with a string. You can also use commas to add a space and combine text.

    Here is both examples:

    print("Hi" + " "+ "I am"+ " "+str(15)+ " "+ "years old")
    print("Congrats for Learning 3.1")
    
    # With commas
    print("Hi", "how", "are", "you" + "?") # Can be useful when printing out separate strings without combining them.
    
    # Or even better
    code = "code "
    print(code * 3) # The asterisk can be used to print a variable multiple times
    

    Be creative

    Homework Problems on Variables

    Question 1: Variable Naming Conventions (SnakeCase, PascalCase, CamelCase)

    Assign a value to a variable using the proper naming conventions.

    Part A: Create a variable using snake case called my_favorite_color and set its value to “blue”.

    my_favorite_color = "blue"
    

    Part B: Now, create a variable using PascalCase called MyFavoriteColor and set its value to “green”.

    MyFavoriteColor = "green"
    

    Part C: Finally, create a variable using CamelCase called myFavoriteColor and set its value to “red”.

    myFavoriteColor = "red"
    

    Part D: Explain why the PascalCase variable name should not be used in Python for regular variables.

    PascalCase is reserved for class names in Python, not regular variables, which should use SnakeCase.

    Question 2: Types of Variables

    Part A: Assign the following values to variables:

    # 1. An integer my_age with the value 21.
    my_age = 21
    
    # 2. A float pi_value with the value 3.14159.
    pi_value = 3.14159
    
    # 3. A boolean is_student with the value True.
    is_student = True
    
    # 4. A string greeting_message with the value "Hello, World!".
    greeting_message = "Hello, World!"
    

    Part B: Print each of these variables to see the output.

    # Integer Value
    print(my_age)
    
    # Float Value
    print(pi_value)
    
    # Boolean Value
    print(is_student)
    
    # String value
    print(greeting_message)
    

    Question 3: How Variables Work

    Part A: Assign the string “Python Programming” to a variable called course_name.

    course_name = "Python Programming"
    

    Then, print the following sentence using this variable:

    print("I am learning " + course_name)
    

    Part B: Assign the integer 10 to a variable called num_items (more commonly shortened to just “n”).

    num_items = 10
    

    Then, print a sentence using this variable:

    # Remember to use str() to convert the integer to a string when printing.
    print("You have " + str(num_items) + " items in your cart.")
    

    Part C: Why do we need to use the str() function when printing the num_items variable in Part B?

    Explanation: We need to use str() because Python cannot concatenate strings with integers directly. The str() function converts the integer into a string, allowing us to print it alongside other strings.

    Scroll to top