Business & Computer Studies Teacher



ExercisesCan use trinket or pythonroom or python anywhere websites to createWatch Youtube for Coding Principles print("Hello, World!")METHODS: Return the number of items in a list:mylist = ["apple",?"banana",?"cherry"]x =?len(mylist)Upper case the first letter in this sentence:txt =?"hello, and welcome to my world."x = txt.capitalize()print?(x)Print the word "banana", taking up the space of 20 characters, with "banana" in the middle:txt =?"banana"x = txt.center(20)print(x)Copy the?fruits?list:fruits = ['apple',?'banana',?'cherry',?'orange']x = fruits.copy()Sort the list alphabetically:cars = ['Ford',?'BMW',?'Volvo']cars.sort()Return True if both statements are True:x = (5?>?3?and?5?<?10)print(x)Substring. Get the characters from position 2 to position 5 (not included):b =?"Hello, World!"print(b[2:5])The split() method splits the string into substrings if it finds instances of the separator:a =?"Hello, World!"print(a.split(","))?# returns ['Hello', ' World!']The replace() method replaces a string with another string:a =?"Hello, World!"print(a.replace("H",?"J"))Using the?and?keyword in an?if?statement:if?5?>?3?and?5?<?10:??print("Both statements are True")else:??print("At least one of the statements are False")End the loop if i is larger than 3:for?i?in?range(9):??if?i >?3:????break??print(i)Get the value of the "model" item:car = {??"brand":?"Ford",??"model":?"Mustang",??"year":?1964}x = car.get("model")print(x)Remove "model" from the dictionary:car = {??"brand":?"Ford",??"model":?"Mustang",??"year":?1964}car.pop("model")print(car)Create and execute a FUNCTION:def?my_function():??print("Hello from a function")my_function()Print "YES" if the variable i is a positive number, print "WHATEVER" if i is 0, otherwise print "NO":for?i?in?range(-5,?5):??if?i >?0:????print("YES")??elif?i ==?0:????print("WHATEVER")??else:????print("NO")Other comparisons that returns False:print(5?>?6)print(4?in?[1,2,3])print("hello"?is?"goodbye")print(5?==?6)print(5?==?6?or?6?==?7)print(5?==?6?and?6?==?7)print("hello"?is not?"hello")print(not(5?==?5))print(3?not?in?[1,2,3])Loop through all items in a list:fruits = ["apple",?"banana",?"cherry"]for?x?in?fruits:??print(x)Import only the?time?section from the datetime module, and print the time as if it was 15:00:from?datetime?import?timex = time(hour=15)print(x)Print x as long as x is less than 9:x =?0while?x <?9:??print(x)? x = x +?1Create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):class?MyNumbers:??def?__iter__(self):??? self.a =?1????return?self??def?__next__(self):????x = self.a??? self.a +=?1????return?xmyclass = MyNumbers()myiter = iter(myclass)print(next(myiter))print(next(myiter))print(next(myiter))print(next(myiter))print(next(myiter))The module named?mymodule?has one function and one dictionary:def?greeting(name):??print("Hello, "?+ name)person1?= {??"name":?"John",??"age":?36,??"country":?"Norway"}Create a class named Person, use the __init__() function to assign values for name and age:class?Person:??def?__init__(self, name, age):????self.name = name??? self.age = agep1 = Person("John",?36)print(p1.name)print(p1.age)Use the words?mysillyobject?and?abc?instead of?self:class?Person:??def?__init__(mysillyobject, name, age):????mysillyobject.name = name??? mysillyobject.age = age??def?myfunc(abc):????print("Hello my name is "?+ abc.name)p1 = Person("John",?36)p1.myfunc()Create a date object:import?datetimex = datetime.datetime(2020,?5,?17)print(x)Display the name of the month:import?datetimex = datetime.datetime(2018,?6,?1)print(x.strftime("%B")) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download