Week 0: Functions and Variables

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/66

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

67 Terms

1
New cards

What is the file extension for python?

.py - (filename.py)

2
New cards

How do you create a python file?

type: code.py

3
New cards

What is a function in python?

Verbs or actions that the computer will already know how to perform

4
New cards

What are arguments?

Arguments are inputs to a function that influence its behavior

5
New cards

What are bugs?

They are problems or errors during code.

6
New cards

How can bugs help you and how does compiler play a role?

They help you solve problems. The compiler can help locate a error.

7
New cards

What is an IDE

Integrated Development Environment (place where you write and run code, like vs code)

8
New cards

What are variables?

Container values within a program

9
New cards

How do you assign a variable?

variable_name = value

10
New cards

How do comments help?

Help explain to you and other programmers what segments of do.

11
New cards

What is psuedocode?

instructions written in english to help simplify the coding process.

12
New cards

What is a multi-function argument?

function call that allows for multiple arguments, like:

print(*string, *datatype value)

13
New cards

How to separate multiple arguments in strings?

with commas.

14
New cards

What does the + sign do in a string?

The + sign concatenates or join two or more strings together.

print(“hello, “ + name)

15
New cards

What are parameters?

Potential arguments that can be taken by a function

16
New cards

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.


17
New cards

How can we modify these arguments for print?

Modifying the sep and end arguments:

print("hello,", end="???")

18
New cards

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

19
New cards

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

20
New cards

What are methods?

Functions that belong to specific objects.

21
New cards

What is a string method

Function that runs ONLY on strings, like:

name = input(“Wassup: “)

name.strip()

22
New cards

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).

23
New cards

Can you do multiple methods on one string?

Yes you can:

name = name.strip().title()

24
New cards

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()

25
New cards

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.

26
New cards

What does split do?

Creates a space between two arguments

27
New cards

Example of using split

name = input("What's your name? ")

first, last = name.split(" ")

print(f"hello, {name}")

28
New cards

integer in python

(Int) numbers from 0 to infinity (and neg) not including decimals

29
New cards

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.

30
New cards

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)

31
New cards

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

32
New cards

What is running functions on functions

Using a function on a function, like int(input()

33
New cards

What is a floating point value?

Value that has a decimal point in it, such as 0.52

34
New cards

Example of using float with input

x = float(input("What's x? "))

y = float(input("What's y? "))

35
New cards

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)

36
New cards

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

37
New cards

How to round when dividing numbers:

z = round(x / y, 2)

This rounds the result of x divided by y to 2 decimal places.

38
New cards

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.

39
New cards

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.

40
New cards

What is scope

The context where variables can be used

41
New cards

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.

42
New cards

What are return values?

Values that a function sends back to the caller after execution.

43
New cards

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.

44
New cards
45
New cards
46
New cards
47
New cards
48
New cards
49
New cards
50
New cards
51
New cards
52
New cards
53
New cards
54
New cards
55
New cards
56
New cards
57
New cards
58
New cards
59
New cards
60
New cards
61
New cards
62
New cards
63
New cards
64
New cards
65
New cards
66
New cards
67
New cards