Python Notes 2

I still need to go through and organize these and make them nice, but I am hoping to eventually write a bunch of helpful guides, these are just notes of my learning

Lets say we have an Amazon page and we want to show their user and what they have in the courtideally we can have something dynamical, its a profile page and we just use the name variable for the user’s name which would be kept in a db or somewhere
name = ‘Johnny’
and we can use formatted strings to custommizeage = 55
print(‘hi ‘ + name + ‘. You are’ + int(age) + ‘years old’)
but there is a better way to do thiswe add an f to make it a formatted stringprint(f’hi {name}. You are {age{ years old’)by adding f to the beginning it knows it is a formatted string, relatively new feature, before python 3 you didn’t have this, python 2 you had something different to accomplish, there was .format
string indexing works like it is an array starting at 0for example, the stringstr = ‘this string’str[0] will relate to the ‘t’ character
selfish =  ‘me me me’
print(selfish[7])you will get ‘e’

when you use square brackets the first number is the start number[start:stop:stepover] for example with the same variableprint(selfish[0:2])you will get ‘me ‘
and finally step overprint(selfish[0:8:2])  it steps over by two, the tetiary arguement is the stepover.
in python the negative index goes through backwards[::-1] will get you the reverse of the string, you go back through the string by minus 1.

immutability mean, strings in python are immutable they can not be change now that the variable ‘selfish’ was define,, I can’t selfish[5]= 5 change the index of 5 like that, you will have to assign the whole string.  Selfish = ‘seflfish’+8but now the original selfish does not exist.
methods are similar to functions but they are owned by something, for example strings have specific methods. it usually has a dot infront of it.
quote = “to be or not to be”print(quote.upper()) 

^the above will make it all capitalized

comments best practice

#addingyou can have list in brackets and the use methods for example insert to insert new data into the list. it extends the list.new_list = basket.extend([100])

#removingbacket.pop()print(basket)pop, pop off whatever is at the end of the list.if you do basket.pop(0)it will remove what is at the 0 indexand there is also remove, but with remove we give it the value we want removed, not the index.pop return what you just removed, remove will return the list with what you wanted remove.clear is the last method he wants to show usclear removes whatever is in the list, completely clears it.
basket = [‘a’,’b’,’c’,’d’,’e’]print(basket.index(‘d’))it will return 3
what if we are looking for something and we are not sure if it is in the listthis requires a python keyword
print(‘d’ in basket)you will get a boolean backso you can use it to search through strings as well.
print(basket.count(‘d’))it will return 1 as it counts how many of that character are in the list.
basket.sort())print(basket) it will return the list in alphabetical order
sorted(basket) does not change or modify the array, it creates a new copy of the array sort of like
new_basket = basket[:]new_basket.sort()basket.reverse()does what you think it would, switching the indexes, 1 is now -1 and so on.

Leave a Reply

Test Your email address will not be published. Required fields are marked *