1/66
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the file extension for python?
.py - (filename.py)
How do you create a python file?
type: code.py
What is a function in python?
Verbs or actions that the computer will already know how to perform
What are arguments?
Arguments are inputs to a function that influence its behavior
What are bugs?
They are problems or errors during code.
How can bugs help you and how does compiler play a role?
They help you solve problems. The compiler can help locate a error.
What is an IDE
Integrated Development Environment (place where you write and run code, like vs code)
What are variables?
Container values within a program
How do you assign a variable?
variable_name = value
How do comments help?
Help explain to you and other programmers what segments of do.
What is psuedocode?
instructions written in english to help simplify the coding process.
What is a multi-function argument?
function call that allows for multiple arguments, like:
print(*string, *datatype value)
How to separate multiple arguments in strings?
with commas.
What does the + sign do in a string?
The + sign concatenates or join two or more strings together.
print(“hello, “ + name)
What are parameters?
Potential arguments that can be taken by a function
What are the arguments for print?
*objects means that the print function can take many functions
*sep = ‘ ‘, means separator. This means the print function automatically puts a single space between arguments
end= ‘\n’ creates a new line for text, so this here means that by default, all print functions end at a new line.
How can we modify these arguments for print?
Modifying the sep and end arguments:
print("hello,", end="???")
What if you want to print quotation marks inside of quotation marks? (Escaping Characters)
Use single spaces and double spaces:
print('hello, "friend"')
or backslashes:
print("hello, \"friend\"")
The backslashes tell the interpreter the following character should be a quotation mark
What are format strings?
Determines how a string should be formatted:
name = input(“What’s your name? “)
print(f"hello, {name}")
This prints: “hello, Josh”
The brackets embed expressions like variables inside the string
What are methods?
Functions that belong to specific objects.
What is a string method
Function that runs ONLY on strings, like:
name = input(“Wassup: “)
name.strip()
Common string methods:
str.lower() – Converts all characters to lowercase.
str.upper() – Converts all characters to uppercase.
str.capitalize() – Capitalizes the first character.
str.title() – Capitalizes the first character of each word.
str.swapcase() – Swaps case (upper ⇄ lower).
Can you do multiple methods on one string?
Yes you can:
name = name.strip().title()
Can you put code in one line?
Yes you can put everything in python in one line:
name = input("What's your name? ").strip().title()
vs this:
name = input("What's your name? ").strip().title()
Is less cleaner than:
name = input("What's your name? ")
# Remove whitespace from the str
name = name.strip()
# Capitalize the first letter of each word
name = name.title()
What looks cleaner? One line, or multiple lines?
In this case, there is no real right answer, and it really depends on who you are writing your code for.
It is really important to be consistent and confident no matter what style you are writing it in.
What does split do?
Creates a space between two arguments
Example of using split
name = input("What's your name? ")
first, last = name.split(" ")
print(f"hello, {name}")
integer in python
(Int) numbers from 0 to infinity (and neg) not including decimals
Interactive Mode Python
Type: python (alone in the terminal) (ctrl-z to undo)
to enter the interactive programming environment where you can execute Python code line by line.
How do you treat input from the user as an integer?
Use int() in front of the input:
x = int(input("What's x? "))
y = int(input("What's y? "))
print(x + y)
Why doesn’t this work?
x = input("What's x? ")
y = input("What's y? ")
z = x + y
Because it treats the input as strings, not as integers. You must use: int() in front of input
What is running functions on functions
Using a function on a function, like int(input()
What is a floating point value?
Value that has a decimal point in it, such as 0.52
Example of using float with input
x = float(input("What's x? "))
y = float(input("What's y? "))
How do you round numbers to the nearest int when dealing with decimals in python?
Using round(number, ndigits) - ndigts (number of decimal places to round to, is optional. Rounds to nearest int if not given.
x = float(input("What's x? "))
y = float(input("What's y? "))
z = round(x + y)
print(z)
How to format long numbers:
You can using fstrings:
print(f"{z:,}")
This will creates commas for long values like: 10,000
Quite cryptic, you do not need to know how to use this
How to round when dividing numbers:
z = round(x / y, 2)
This rounds the result of x divided by y to 2 decimal places.
How do you define a function?
def function_name():
def is the keyword to define the function. You can then give the name then end with parenthesis and a colon to start the function body.
Example of a function
def hello(to="world"):
print("hello,", to)
name = input("What's your name? ")
hello(name)
The to is an argument. The computer passes name into the function as to.
What is scope
The context where variables can be used
Where do you define your functions?
You would have to define them at the top, but you can use main() to stop that:
def main():
hello()
def hello(to):
— Code
main():
This allows for the computer to run main() first, so hello will already be defined by the time its used.
What are return values?
Values that a function sends back to the caller after execution.
Example of return value:
def main():
x = int(input("What's x? "))
print("x squared is", square(x))
def square(n):
return n * n
main()
Here, the result of two numbers squared is returned.