Unit 3 Sections 1 and 2
This is my solution to the homework that was assigned on 11/28/2022
Notes on Units 3.1 & 3.2
Variables
- Variables organize help organize data by naming it something related to the information it carries
- contains a name, value, and type
- NAMING:
- use general terms
- keep it concise
- use consistent notation (caps, etc...)
- don't make it too vague
- no spaces
- TYPES:
- Integer - a number
- Text/string - a word or string of characters
- Boolean - data that determines if something is true or false
- NAMING:
- A list of data can be stored as a variable
- more organized
- can be indexed
- contains a name, value, and type
Assignments
- Operators that allows a program to change the value represented by a variable
- Used to assign values to variables
- =
- Assigns value of the right side to the left side
- a=b
- Outcome: b
- +=
- Add right side operand with left side operand and then assign to left operand
- a+=b
- Outcome: a+b
- -=
- Subtract right operand from left operand and then assign to left operand: True if both operands are equal
- a-=b
- Outcome: a-b
- same thing for *= and /=
Data Abstraction
- Method used in coding to represent data in a useful form, by taking away aspects of data that aren't being used in the situation
- Variables and lists are primary tools in data abstraction
- Provides a separation between the abstract properties of a data type and the concrete details of its representation
- Lists and Strings
- List = ordered sequence of elements
- Element = individual value in a list that is assigned to a unique index
- Index = a way to reference the elements in a list or string using natural numbers; each element of a string is referenced by an index
- Example of a list with colors
Index | Element |
---|---|
1 | Green |
2 | Blue |
3 | Purple |
4 | Pink |
- At index 1, the element is green, at index 2, the element is blue, etc...
Managing Lists
3 Types of List Operations
- Assigning values to a list at certain indices
- Creating an empty list and assigning it to a variable
- Assigning a copy of one list to another list (setting one list equal to another list)
Managing complexity
- Helps improve readability
- Reduces the need for new variables as more data is collected
- Can easily update data
- Can easily convert data to different forms Practice
colorList=["green", "red", "pink", "purple", "blue", "brown"]
for i in colorList:
print(i)
originalAlphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # All 26 letters of the alphabet
secretAlphabet = [] # All letters of the alphabet after it has been shifted
print("Soham Kamat's Ceaser Cipher Encoder") # Message explaining what the code does
userShift = int(input("Please, tell me how much you'd like to shift the alphabet (1-25): ")) # Asks what shift you want
userMessage = input("Please, enter a message you'd like to encrypt: ") # Asks what message you want encrypted
userMessage = userMessage.lower() # Makes all letters lowercase
userMessage = list(userMessage.lower())
encrypted = []
while userShift not in range(1, 26): # Makes sure shift is in range 1-25
print("Please, enter a valid selection.")
userShift = int(input("Please, tell me how much you'd like to shift the alphabet (1-25): "))
for shift in originalAlphabet[0:userShift]: # Shifts the alphabet by the above value
secretAlphabet = originalAlphabet[userShift:] + originalAlphabet[0:userShift]
print(originalAlphabet) # Prints the actual Alphabet
print(secretAlphabet) # Prints the shifted Alphabet
for letter in userMessage: # repeats for every character in the inputted message
if letter in originalAlphabet:
idx = originalAlphabet.index(letter)
encrypted.append(secretAlphabet[idx])
else:
encrypted.append(letter)
print(userMessage)
print(''.join(encrypted))