Skip to main content

Posts

Dynamic Lists

 Dynamic Lists  Dynamic lists are ways of using a blank list and adding or removing items to it as we go                Blank lists  EXAMPLE :   MYAGENDA = []  Append a list .append will let us add whatever is in () to the list.  EXAMPLE:  myAgenda = [] def printList():     for item in myAgenda:     print(item) while True:   item = input("What's next on the Agenda?: ")   myAgenda.append(item)   printList()  Removing Items from a List how using  .remove will remove what is inside the (). EXAMPLE: myAgenda = [] def printList():   print()    for item in myAgenda:     print(item)   print()  while True:   menu = input("add or remove?: ")   if menu == "add":     item = input("What's next on the Agenda?: ")     myAgenda.append(item)   elif menu == "remove":     item = input("What do you want to re...

LISTS

 LISTS As far as Python is concerned, this is a list. Notice we start counting the first item at 0 (instead of 1) Any piece of data from any data type can go into a list. We can extract, remove, or change lists  EXAMPLE: timetable = ["Computer Science", "Math", "English", "Art", "Sport"] print(timetable[1])  Lists and Loops: EXAMPLE: timetable = ["Computer Science", "Math", "English", "Art", "Watch TV"] for lesson in timetable:   print(lesson) 

ALL ABOUT f- string

  f-strings f-strings (format strings) are the best way to combine variables and text together.  EXAMPLE : name = "Katie" age = "28" pronouns = "she/her" print("This is {}, using {} pronouns, and is {} years old.".format(name, pronouns, age))  Local Variables : EXAMPLE name = "Katie" age = "28" pronouns = "she/her" print("This is {name}, using {pronouns} pronouns, and is {age} years old. Hello, {name}. How are you? Have you been having a great {age} years so far".format(name=name, pronouns=pronouns, age=age))  The Power of f... EXAMPLE: name = "Katie" age = "28" pronouns = "she/her" response = f"This is {name}, using {pronouns} pronouns, and is {age} years old. Hello, {name}. How are you? Have you been having a great {age} years so far" print(response) Alignment: left = <, right = >, center = ^  EXAMPLE: for i in range(1, 31):   print(f"Day {i: <2} of 30...

MORE ABOUT print

 end  EXAMPLE:  for i in range(0, 100):   print(i, end=" ") NEW LINE : #new line for i in range(0, 100):   print(i, end="\n")                                                 TAB INDENT: #tab indent for i in range(0, 100):   print(i, end="\t")                                         VERTICAL TAB: #vertical tab for i in range(0, 100):   print(i, end="\v") sep  print("If you put ", "\033[33m", "nothing as the ", "\033[35m", "end character ", "\033[32m", "then you don't ", "\033[0m", "get odd gaps ", sep="")  Cursor EXAMPLE import os, time print('\033[?25l', end="") for i in range(1, 101):   print(i)   time.sleep(0.2)   os.system("clear")

OS LIBRARY , TIME LIBRARY

  What is the os library? It allows us to "talk" to the console. One of the most powerful things we can do with this library is allow it to clear the console EXAMPLE: import os print("Welcome") print("to") print("Replit") os.system("clear") username = input("Username: ")  Time Library We can import a second library by placing a  ,  after the name of the first library. EXAMPLE: import os, time print("Welcome") print("to") print("Replit") time.sleep(10) os.system("clear") username = input("Username: ")  NOTE:  from replit import audio import os, time def play():   source = audio.play_file('audio.wav')   source.paused = False # unpause the playback   while True:     stop_playback = int(input("Press 2 anytime to stop playback and go back to the menu : ")) # giving the user the option to stop playback     if stop_playback == 2:       source.paused = True # let'...

Return Command

  Return Command The  return  command sends some information back to the part of the code that called it. This means the function call is replaced with whatever was returned  EXAMPLE : def area_square(side1, side2):   area = side1 * side2  return area area = area_square(4, 12) print(area)

Parameters , Call the Subroutine , Adding More Arguments

  Parameters In a subroutine, the  ()  are for the argument (FYI argument is another word for parameter). These are the pieces of information we pass to the code. These can be variable names that are made up for the first time within the argument  () . EXAMPLE : def whichCake(ingredient):   if ingredient == "chocolate":     print("Mmm, chocolate cake is amazing")   elif ingredient == "broccoli":     print("You what mate?!")   else:      print("Yeah, that's great I suppose...") How do we call the subroutine? We call it in the same way as before. However, instead of leaving the  ()  blank, we send the code a message.   EXAMPLE : def whichCake("chocolate"):   if ingredient == "chocolate":     print("Mmm, chocolate cake is amazing")   elif ingredient == "broccoli":     print("You what mate?!")   else:      print("Yeah, that's great I suppose...")  Addin...