Section two - first app Flashcards
How do you show the type of something?
print(type(var))
Show the amount of characters in something
len(variable)
add an element to a list
What is this called?
list.append(“append with this”)
Method, it’s like a function but it’s attached to a data type (list, variable, etc)
Capitalize the first lettter of the string variable
Capitalize the first letter in every word
These are the methods you attach
print(todo.capitalize())
todo.title()
continuously print “Hello”
while True:
print(“Hello”)
Create a program to enter a password
password = input(“Enter a password”)
while password != “pass123”
password = input(“Enter a password”)
print(“Correct”)
Show all methods for objects
For instance, how do I find out how to capitalize a string?
Show more info on the method
CMD
python3
dir(str)
Methods are after the underscored stuff
help(str.capitalize)
You’ll notice here that it will output “capitalize()” which shows it takes not arguments.
List functions like “print”
CMD
python3
import builtins
dir(builtins)
Lowercase are all functions
help(input)
Create a case program
match user_action:
case “add”:
todo = input(“Enter a todo”)
todos.append(todo)
case “show”:
print(todos)
Print list elements per line
for item in list:
print(item)
If I want remove an ending space, what method would we use?
user_action.strip()
What is python’s interpreter
cpython
python download
It’s an interpreter
interprets python to machine language
you have created the below list
list = [‘a’, ‘b’, ‘c’]
you want to find the index of ‘b’
What command do you use?
list.index(‘b’)
Strings are immutable so you can’t change them with
list[0][1] = “-“
instead, what would you use to change a period to a “-“
This will replace the string
list[1] = list.replace(“.”,”-“,1)
one means only change the first occurence of the dot.
What is the immutable version of a list
tuple = (‘this’, ‘that’, ‘other’)
how do you read the below explanation for a list method
pop(self, index=-1, /)
remove and return item at index (default last).
You put in an index number as the arguement, it will return the removed item.
It will show you what’s in slot in a list
-1 = default value of the argument
So if you enter nothing, it will be -1, the last item. This means you can actually go backwards through a list like this.
Sort a list alphabetically
list.sort()
dir(list)
help(list.sort())
word = “hello”
Replace the o for a period
word.replace(“o”, “.”)
Store info from your program in a text file
What is the issue with this and how do we solve it?
Create a text file in the same directory as your program.
todos = []
todos.append(“cook”) + \n
Create a file object to write to “r” would be read.
file = open(‘todos.txt’, ‘w’)
file.writelines(todos) < - add the whole list “todos”
You might want to add a “+ \n” after your input to put a space in the file.
THIS WILL CREATE A NEW FILE EVERYTIME SO IT WILL DELETE THE OLD INFO WITH EVERY NEW ITERATION.
Instead add this prior to your “w” command and delete your list altogether
todo = input(‘enter a todo: ‘)
file = open(‘todos.txt’, ‘r’)
todos = file.readlines()
REMEMBER TO CLOSE FILE
file.close()
todos.append(todo)
Write a simple string to a file instead of a list
file = open(‘text.txt’, ‘w’)
file.write(‘Hey this is your text!’)
file.read() <- instead of read lines if you want list form.
What does it mean to exhaust file.read()
There is a CURSOR in a file that starts at the first item/object in the text file.
When you file.read(), it goes to the end, so if you try to file.read() again, it will output nothing.
When adding an absolute path in windows, why do we need to add the “r” prior to the path?
r - raw
The reason for this is because certain symbols, like “" in python call a certain thing to happen. Like “\t” giving you a tab. To make sure it only reads it in text form, we add “r”
\n < - things like this are known as escape sequences
We have content in one list, and text files in a another, how do we add the content to each of the text files
contents = [‘one’, ‘two’, ‘three’]
files = [‘one.txt’, ‘two.txt’, ‘three.txt’]
for content, files in zip(contents, filenames):
file = open(filename, ‘r’)
file.write()
THIS WON’T PRINT ON IT’S OWN
The structure would look like
[(‘one’, ‘one.txt’),(‘two’, ‘two.txt’),(‘three’, ‘three.txt’)]
If you were doing the below
x = zip(contents, filenames)
you could print this only by listing it’s elements
list(x)
Create a multiline string in a variable
a = “this is a really” \
“I swear it is”
a = “"”This that
and the other”””