ECS 32A Lecture Notes - Lecture 2: Newline, Ascii Art

90 views2 pages
10 Jan 2019
Basic Input and Output
Output is important bc it allows Python scripts to interact with other programs
Python uses the print() function to print information to the console
Single or double quotes can be used; as long as they are the same
print(“Hello World”)
print() automatically ends in a newline, but you can change that with named parameter
“\t” is the special character with a tab
Print can accept almost anything
enrollment = 295
print(“the enrollment is: ”, enrollment)
To add comments, use the “#” character
print(“hello”) #printing hello
To get input from the user, use the input() function
num_pizza = input(“How many pizzas?\n”)
print(“only”, num_pizza, “pizzas?”)
To convert a string to an integer, use the int() function
Name error
Occurs when variable is not declared beforehand
The computer doesn’t know what the variable is
Type Error
Occurs when there is a mismatch of the tpe between two variables
I.e. adding an int to a string
num_pizza = 300”
num_pizza = num_pizza + 1
Will result in a type error bc num_pizza is a string as denoted by
the quotes
3 Main types of errors
Syntax error
Occurs when the syntax is not correct
Missing a comma, quote, etc..
Runtime error
Occurs when the interpreter crashes; it doesn’t know what to do
Adding a number to a string
Logic Error
The program works, but the results are wrong
print(Hello” + World”)
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.