Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LECTURE 0
- >Creating Code with Python<
- *VS Code - Special type of text editor, a compiler
- - Top: Text Editor
- - Bottom: Terminal
- *In terminal: Execute ``code hello.py`` to start coding
- *In text editor: Type ``print("hello world")`` - Famous canonical program that nearly all coders write during the learning process
- *In terminal window: Can execute commands
- To run the program, cursor needs to be moved to the bottom of the screen. Type ``python hello.py`` next to the dollar sign to type a second command in the terminal and press the enter key
- *Result of running ``python hello.py`` program: ``hello, world``
- >Functions<
- *Functions - Verbs or actions that the computer will already know how to perform
- *``Print`` function - Knows how to print to the terminal program
- - Takes arguments
- >Bugs<
- *Bugs - Natural part of coding
- >Improving Your First Python Program<
- *``input`` - Function that takes a prompt as an argument
- input("What's your name? ")
- print("hello, world")
- >Variables<
- *Variable - Container for a value within your own program
- *To introduce the variable in the program:
- name = input("What's your name? ")
- print ("hello, world")
- ``=`` - Assigns what is on the right to what is on the left
- - Value returned by ``input("What's your name? ")`` is assigned to ``name``
- *Further editing of the code:
- name = input("What's your name? ")
- print("hello,")
- print(name)
- >Result of the terminal window:
- What's your name? |David|
- |hello|
- |David|
- >Comments<
- *Comments - Way for programmers to track what they're doing in their programs; Notes
- # Insert comment here
- >Pseudocode<
- *Pseudocode - Important type of comment that becomes a special type of to-do list, especially when you don't understand how to accomplish a coding task
- # Ask the user for their name
- name = input("What's your name? ")
- # Print hello
- print("hello,")
- # Print the name inputted
- print(name)
- >Further Improving Your First Python Program<
- *Can further edit the code as follows:
- # Ask the user for their name
- name = input("What's your name? ")
- # Print hello and the inputted name
- print("hello, " + name)
- *Can use a comma ``,`` to pass in multiple arguments by editing the code as follows:
- # Ask the user for their name
- name = input("What's your name? ")
- # Print hello and the inputted name
- print("hello, ", name)
- >Strings and Parameters<
- *String ``str`` - Sequence of text
- *``print`` function - Automatically include a piece of code ``end='\n'``
- - Takes an argument called ``end`` and the default is to create a new line
- *``\n`` - Indicates that the print function will automatically create a line break when run
- *Can also provide an argument for ``end`` such that a new line isn't created
- *Can modify the code as follows:
- # Ask the user for their name
- name = input("What's your name? ")
- print("hello, ", end="")
- *``end=""`` - Over-writes the default value of ``end``, making it not create a new line after this first print statement
- *Parameters - Arguments that can be taken by a function
- >A Small Problem with Quotation Marks<
- *Adding quotation marks as part of the string is challenging as ``print("hello,"friend"")`` won't work and compiler will throw an error
- >2 Approaches to Fix the Issue:
- 1. Change the quotes to single quote marks
- 2. Code as ``print("hello, \"friend\"")``
- ->Backslashes tell the compiler that the following character should be considered a quotation mark in the string
- >Formatting Strings<
- *Possibly the most elegant way to use strings would be as follows:
- # Ask the user for their name
- name = input("What's your name? ")
- print(f"hello, {name}")
- ->``f`` - Special indicator to Python to treat this string a special way, different than previous approaches
- >More on Strings<
- *Built into strings is the ability to remove whitespace from a string
- *Method ``strip`` on ``name`` as ``name = name.strip()`` - Strips all the whitespace on the left and right of the user's input:
- # Ask the user for their name
- name = input("What's your name? ")
- # Remove whitespace from the str
- name = name.strip()
- # Print the output
- print(f"hello, {name}")
- *``title`` method - Title case the user's name:
- # Ask the user for their name
- name = input("What's your name? ")
- # Remove whitespace from the str
- name = name.strip()
- # Capitalize the first letter of each word
- name = name.title()
- # Print the output
- print(f"hello, {name}")
- *Can up arrow to recall the most recent terminal commands that was made
- *Code can be more efficient:
- # Ask the user for their name
- name = input("What's your name? ")
- # Remove whitespace from the str and capitalize the first letter of each word
- name = name.strip().title()
- # Print the output
- print(f"hello, {name}")
- *An even more efficient way:
- # Ask the user for their name, remove whitespace from the str, and capitalize the first letter of each word
- name = input("What's your name? ").strip().title()
- # Print the output
- print(f"hello, {name}")
- >Intergers or int<
- *``int`` - Interger
- x = 1
- y = 2
- z = x + y
- print(z)
- *Can make it more interactive using the ``input`` function:
- x = input("What's x? ")
- y = input("What's y? ")
- z = x + y
- print(z)
- *Needs to convert this input from a string into an integer:
- x = input("What's x? ")
- y = input("What's y? ")
- z = int(x) + int(y)
- print(z)
- *Using int(x) is "Casting" - Where a value is temporarily changed from one type of variable [string] to another [interger]
- *Can further improve the program:
- x = int(input("What's x? "))
- y = int(input("What's y? "))
- print (x + y)
- *Most inner function is run first, then the outer one is run
- >Float Basics<
- *Floating Point Value - Real number that has a decimal point in it
- *Can change the code to support floats:
- x = float(input("What's x? "))
- y = float(input("What's y? "))
- print (x + y)
- *``round`` - Available argument: ``round(number[n, digits])``
- - Square brackets indicate that something optional can be specified
- - Could do ``round(n)`` to round a digit to its nearest interger
- *Alternatively, code can be as follows:
- # Get the user's input
- x = float(input("What's x? "))
- y = float(input("What's y? "))
- # Create a rounded result
- z = round(x + y)
- # Print the result
- print(z)
- *Formatting the output of long numbers, from 1000 to 1,000:
- # Get the user's input
- x = float(input("What's x? "))
- y = float(input("What's y? "))
- # Create a rounded result
- z = round(x + y)
- # Print the result
- print(f"{z:,}")
- >More on Floats<
- *To round floating point values:
- # Get the user's input
- x = float(input("What's x? "))
- y = float(input("What's y? "))
- # Create a rounded result [to the nearest 2 decimal points]
- z = (x / y, 2)
- # Print the result
- print(z)
- *Can use ``fstring`` to format the output below:
- # Get the user's input
- x = float(input("What's x? "))
- y = float(input("What's y? "))
- # Calculate the result
- z = x / y
- # Print the result
- print(f"{z:.2f}")
- *``fstring`` - Displays the same as the prior strategy
- >Def<
- *Starting from scratch:
- name = input("What's your name? ")
- hello()
- print(name)
- >Compiler will error because ``hello`` has no defined function
- *Creating own function called ``hello``:
- def hello():
- print("hello")
- name = input("What's your name? ")
- hello()
- print(name)
- >Python is an idented language - Uses idention to understand what is part of the above function
- - Everything in the ``hello`` function must be idented
- *Can further improve code:
- # Create own function
- def hello(to):
- print("hello,", to)
- # Output using own function
- name = input("What's your name? ")
- hello(name)
- >First lines: Creating ``hello` function; however, compiler is being told that this function takes a single parameter: a variable called ``to``
- -> How values are passed into functions
- *Can add a default value to ``hello``:
- # Create or own function
- def hello():
- print("hello,", to)
- # Output using own function
- name = input("What's your name? ")
- hello(name)
- # Output without passing expected arguments
- hello()
- *Functions don't need to be at the start of the program; they can be moved down but compiler needs to be told that there's a ``main`` function and a separate ``hello`` function:
- def main():
- # Output using own function
- name = input("What's your name? ")
- hello (name)
- # Output without passing expected arguments
- hello ()
- # Create own function
- def hello(to="world"):
- print("hello,",to)
- >However, this alone will create an error as nothing in the code is actually running the ``main`` function
- *To call the main function:
- def main():
- # Output using own function
- name = input("What's your name? ")
- hello (name)
- # Output without passing expected arguments
- hello ()
- # Create own function
- def hello(to="world"):
- print("hello,",to)
- main()
- >Returning Values<
- *``return`` value - "Passing back" of a value
- - Function to return the calculation's value back to another part of the program
- *Calculator.py; rework the code as follows:
- def main():
- x = int(input("What's x? "))
- print("x squared is", square(x))
- def square(n):
- return n * n
- main()
- >``x`` is passed to ``square``; Calculation of ``x * x`` is returned back to the main function
Advertisement
Add Comment
Please, Sign In to add comment