Python Basics (class slides)

CSc 110 Python Basics

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

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 sends the output cursor to the next line after printing
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.

Submit to gradescope

Submit your solution to the previous problem to gradescope

Name your file first.py

Also, like for the programming problems due next week, we will set up our code to be inside a main function (DEMO)

Remember that the print() function prints a line break (\n character) by default, you can change the default by specifying the parameter end to be an empty string

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?

Statements (vs expressions)

  • Expressions represent values or the computation of values
  • Statements represent executable steps in a program.
  • Statements often include expressions

Here’s a statement that invokes the print function with the expression 3 + 4 as an argument.

print(3 + 4)

Basic Types

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

Use the function type() with each literal type.

Operations

Try the following operators with the different types in python:


   +
   -
   *
   /
   //
   %
   **

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

Comments

Computer program can include comments, which are simply text that is meant to be read, but not executed.


# assign a radius value
radius = 3
# compute the area of a circle
area = 3.1415 * radius ** 2


The purpose of comments is to “document” the program so that subsequent readers can more easily understand it, or learn things about it (e.g., its original author).

Quiz 02

current time

You have 10 minutes to complete the quiz.

Your solution needs to print the exact characters shown in your quiz (there are different versions of the quiz) – changes to the characters (for example, not printing the quotes or apostrophe) will be considered as an incorrect solution (zero grade awarded).