-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit1_notes_variables.py
117 lines (93 loc) · 4.02 KB
/
unit1_notes_variables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Name:
Title: Math, Variables, User Input, & Types
Description: Working with fundametal data types and operations in Python. Storing data in variables. Interacting with a user.
"""
# VARIABLE - a user-defined name in a program, which provides a way to access information stored in a computer's memory.
# Follow all variable naming rules and conventions:
# Rules for legal variable names:
# - Contain only numbers, letters, and underscores
# - Can’t start with a number
# - Can’t be a reserved keyword in Python (print, input, type, etc.)
# Guidelines for good variable names:
# - Be descriptive of a variable's contents
# - Be consistent with formatting
# - Follow conventions:
# - names that begin with an underscore have a special meaning
# - don’t begin with a capital letter
# - Consider the trade-off of long names
# !!! Instructor note: review slides on variables
name = "Monty" # read as: assign the string value "Monty" to variable "name"
# Reassigning to a variable to update its value
name = "Python" # we can't get "Monty" back!
#print(name)
# ASSIGNMENT STATEMENT: assigns a value to a variable; creates (INITIALIZES) variable if necessary
# ASSIGNMENT OPERATOR: = (equals sign); assigns values from right to left ONLY
result = "0" # string
print(result)
result = 3 # int
print(result)
# !!! Instructor note: Do peer instrution question #1
# INPUT function - Pauses program execution and gets typed text from the user of our program. Returns it to you as a string.
# Only accepts 1 string argument, which should be a prompt to the user on what to type. User presses 'Enter" when done typing.
# Tip: Including a space at the end of your string argument makes the prompt more user-friendly.
first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
print("Printed with a comma:", first_name, last_name)
# CONCATENATION: Two strings combined into one using the plus sign.
name = first_name + " " + last_name
print("Printed with concatenation:", name)
# string repetition
print("happy" * 100)
# !!! Instructor note: Do Peer instruction 2.1 & 2.2
# expressions will always be evaluated before they are passed into a function
print("\nWelcome to the calculator program", name + "!")
# using a comma - separates arguments & inserts a space
# using concatenation - doesn't insert a space & combines strings
# Numbers in Python - 2 new TYPES:
# INTEGER(int) - whole numbers, no decimal point
# FLOATING POINT NUMBER (float) - decimal number
result = 0 # integer
result = 0.0 # float
print("\nThe starting result is:", result)
# Built-in functions to convert types:
# str() converts to a string
# int() converts to an int
# float() converts to a float
num1 = input("\nPlease enter your first number: ") # string
num1 = int(num1) # integer
# nesting functions: putting one function inside another
num2 = float(input("Please enter your second number: ")) # float
input("\n\nPress enter to process calculations...")
print("---------------------------------------")
num1 = 0
num2 = 0
# Math Operators:
# + , - , / , //, * , ** , %
# addition
result = num1 + num2
print(num1, "+", num2, "=", result)
# subtraction
result = num1 - num2
print(num1, "-", num2, "=", result)
# true (floating point) division
result = num1 / num2
print(num1, "/", num2, "=", result)
# integer division - leaves off remainder
result = num1 // num2
print(num1, "//", num2, "=", result)
# multiplication
result = num1 * num2
print(num1, "*", num2, "=", result)
# exponents
result = num1 ** num2
print(num1, "**", num2, "=", result)
# modulus - gives the remainder
result = num1 % num2
print(num1, "%", num2, "=", result)
# STATEMENT - a complete thought in a programming language (equivalent to one complete line of code)
# e.g. print(2+2)
# EXPRESSION - code that can be evaluated to a new value (like a math operation)
# e.g. 2+2
print("\nThanks for using the Python calculator. Re-run program for new numbers.")
# !!! Instructor note: Do Peer instruction 3.1 & 3.2; optionally do variable tracing programs