python basics (slides)

CSc 110 Python Basics

CS Buddy Mentor Program Sign-Up

sign-up QR Code

Announcements

Did you set up your workspace?

  • Did you download Python 3?
  • Did you download VS Code (or PyCharm, or Mu)?
  • If not, that’s fine, you can use vscode.dev

What is a program?

The print function

  • What does the print function do?

The print function

  • print() sends characters (strings) to the standard output
  • By default, the standard output of a python program goes to the console.
print("some characters")
some characters


print('some characters')
some characters

String operations

String concatenation:

  • What will the standard output display when the code below is run?
print("hello" + "world")

String operations

In your groups, try the following operations:

  • String repetition: "abc" * 4
  • String concatenation with + and ,. What is the difference?
  • Combine both repetition and concatenation to print the following:
They sang AaAaAaAaAaAaAaAaAaAa

String operations

solution 1:

print("They sang", "Aa" * 10)
They sang AaAaAaAaAaAaAaAaAaAa

solution 2:

print("They", "sang", "Aa" * 10)
They sang AaAaAaAaAaAaAaAaAaAa

solution 3:

print("They" + " "  + "sang" + " " + "Aa" * 10)
They sang AaAaAaAaAaAaAaAaAaAa

Multiple print statements

  • What will the standard output display when the code below is run?
print('Are')
print('You')
print('In')
print('College?')

Multiple print statements

  • By default, the print function moves the cursor to the next line after printing, unless you specify otherwise.
print('Are', end = ' ')
print('You', end = ' ')
print('In', end = ' ')
print('College?', end = ' ')
Are You In College? 

What other characters can you use for values for the end parameter?

Dealing with special characters

Write a simple program that prints the following output to the python console:


   He said, "What's up?" 
   Joe's friend didn't reply.

Dealing with special characters

double quotes:

print("He said, \"What's up?\"")
print("Joe's friend didn't reply.")
He said, "What's up?"
Joe's friend didn't reply.

single quotes:

print('He said, "What\'s up?"')
print('Joe\'s friend didn\'t reply.')
He said, "What's up?"
Joe's friend didn't reply.

Variables

Variables and assignment

  • Variables are names in a program that represent a stored value
  • We can assign names to particular values in our program
  • When we give a value a name, or update the value stored in a variable, this is called assigning a variable

Demonstration of variable assignment

Variable assignment does not produce any output to the console. Run the following code:

first_name = "Mary"
family_name = "Silva"

How would we print these variables?

Variables improve readability

Without variables:

print("The total price is", 100 * 1.08)
The total price is 108.0

With variables:

base_price = 100
tax_rate = 1.08
total_price = base_price * tax_rate
print("The total price is", total_price)
The total price is 108.0

Basic Types

  • string: "hello" or 'hello'
  • integer: 3
  • float: 3.14
  • bool: True or False

Use the function type() with each literal type.

Operations

  • + : plus

  • - : minus

  • * : multiple

  • / : divide

  • ** : power

  • // : divide and floor

  • % : modulos


Operations - examples

print(10 + 5 - 5) # addition and subtraction
print(10 * 5 / 4) # multiplication and division
print(25 ** 2) # square 
print(25 ** 0.5) # square root 
print(13 // 4) # division and round down to the nearest integer
print(13 % 4) # remainder 
10
12.5
625
5.0
3
1

Order of Operations

PEMDAS

  • What does PEMDAS stand for?
  • The operator precedence:
    • Parentheses
    • Exponentiation
    • Multiplication and Division (including // and %)
    • Addition and Subtraction

PEMDAS

What value will each of these variables take on? No computers!

a1 = 5 /  5 * 10  * 5
a2 = 5 / (5 * 10) * 5

b1 = 5 *  10 - 2
b2 = 5 * (10 - 2)

c = (3 // (4 // 5)) + 1

PEMDAS – answer

a1 = 5 /  5 * 10  * 5
a2 = 5 / (5 * 10) * 5

b1 = 5 *  10 - 2
b2 = 5 * (10 - 2)

# c = (3 // (4 // 5)) + 1 ERROR -- Zero Division

print(a1)
print(a2)
print(b1)
print(b2)
50.0
0.5
48
40

Note that the division operator returns a float even when both numerator and denominator are integers

Rounding numbers with built-in function round()

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

The number of digits (ndigits) is optional, but we will often round number to two decimals:

round(392.68750000000006, 2)
392.69

round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

Rounding is done toward the nearest even choice:

print(round(51.6))
print(round(51.4))
print(round(51.5))
print(round(50.5))
52
51
52
50

Operations and Variable Assignments

Variable assignments are not like math — they can appear in equations that make no mathematical sense.


x = 5
print(x)
x = x + 2
print(x)


x = x + 2 means “take the current value of x, add it with 2, and assign the resulting value (7) to x as its new value.”

Combining variables and operations

Write code to calculate the area of a circle when radius is 3:

  1. Assign integer 3 to radius
  2. Calculate the area (use \(\pi\) as 3.1415) and round it to two decimals
  3. Print the area

Comments

Comments are text that is meant to be read, but not executed. The purpose is to improve readability.

# assign a radius value
radius = 3
# compute the area of a circle and round to two decimals
area = round(3.1415 * radius ** 2, 2)
# print the area
print(area)
28.27

Questions

  • What if we want to get the area of circles when the radius is 1, 2, 3, 4 and 5?
  • Can we do it more efficiently?

Quiz 02

current time

You have 5 minutes to complete the quiz.