Basic Python

Variables & Types

Numbers

# This will get executed each time the exercise gets initialized b = 6 #ปริ้นเลข 7 ผ่านตัวแปร myint myint = #ปริ้นเลข 7 แบบทศนิยม myfloat1 = #ปริ้นเลข 7 แบบทศนิยม myfloat2 = myint = 7 print(myint) myfloat1 = 7.0 print(myfloat1) myfloat2 = float(7) print(myfloat2) test_object("myint") test_object("myfloat1") test_object("myfloat2") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

String

# This will get executed each time the exercise gets initialized b = 6 # ปริ้น hello โดยใช้ ' ' mystring1 = # ปริ้น hello โดยใช้ " " mystring2 = # ปริ้น hello world แบบมีช่องว่างตรงกลาง hello = "hello" world = "world" helloworld = mystring1 = 'hello' print(mystring1) mystring2 = "hello" print(mystring2) hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld) test_object("mystring1") test_object("mystring2") test_object("helloworld") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

List

# This will get executed each time the exercise gets initialized b = 6 # สร้าง list เปล่าๆ ด้วยตัวแปร mylist mylist = # เติมเลข 1 ใน list mylist. # เติมเลข 2 ใน list mylist. # ปริ้น list # ปริ้นเฉพาะเลข 1 # ปริ้นเฉพาะเลข 2 mylist = [ ] mylist.append(1) mylist.append(2) print(mylist) print(mylist[0]) print(mylist[1]) test_object("mylist") test_object("mylist[0]") test_object("mylist[1]") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

Basic Operation

# This will get executed each time the exercise gets initialized b = 6 # สร้างตัวแปร number โดยคำนวณ 1 บวก 2 คูณ 3 หาร 4.0 number = print(number) # หาทศนิยมจาก 11 หาร 3 remainder print(remainder) # หาพื้นที่สี่เหลี่ยม แต่ละด้านมีค่าเท่ากับ 7 squared = print(squared) # หาปริมาตรลูกบาศก์ แต่ละด้านมีค่าเท่ากับ 2 cubed = print(cubed) number = 1 + 2 * 3 / 4.0 print(number) remainder = 11 % 3 print(remainder) squared = 7 ** 2 cubed = 2 ** 3 print(squared) print(cubed) test_object("number") test_object("remainder") test_object("squared") test_object("cubed") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

Basic String Operation

# This will get executed each time the exercise gets initialized b = 6 # Create a variable a, equal to 5 # Print out a astring = "Hello world!" string1 = len(astring) print(string1) astring = "Hello world!" string2 = astring.index("o") print(string2) astring = "Hello world!" string3 = astring.count("l") print(string3) astring = "Hello world!" string4 = astring[3:7] print(string4) astring = "Hello world!" string5 = astring[3:7:2] print(string5) astring = "Hello world!" string6 = astring.upper() string7 = astring.lower() print(string6) print(string7) test_object("string1") test_object("string2") test_object("string3") test_object("string4") test_object("string5") test_object("string6") test_object("string7") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

Dictionaries

# This will get executed each time the exercise gets initialized b = 6 # Create a variable a, equal to 5 # Print out a phonebook = {} phonebook["John"] = 938477566 phonebook["Jack"] = 938377264 phonebook["Jill"] = 947662781 print(phonebook) phonebook1 = { "John" : 938477566, "Jack" : 938377264, "Jill" : 947662781 } print(phonebook1) test_object("phonebook") test_object("phonebook1") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

Iterating over dictionaries

# This will get executed each time the exercise gets initialized b = 6 # Create a variable a, equal to 5 # Print out a phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781} for name, number in phonebook.items(): print("Phone number of %s is %d" % (name, number)) test_object("phonebook") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

Numpy Arrays

# This will get executed each time the exercise gets initialized b = 6 # สร้าง List ใช้ 2 ตัวแปรคือ height กับ weight # height มีเลข 1.87, 1.87, 1.82, 1.91, 1.90, 1.85 # weight มีเลข 81.65, 97.52, 95.25, 92.98, 86.18, 88.45 height = weight = # อย่าลืม import numpy มาด้วยนะ import numpy as np # ทำให้ height กับ weight อยู่ในรูป Numpy Arrays np_height = np_weight = print(height) print(weight) # Create 2 new lists height and weight height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85] weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45] # Import the numpy package as np import numpy as np # Create 2 numpy arrays from height and weight np_height = np.array(height) np_weight = np.array(weight) print(np_height) print(np_weight) test_object("np_height") test_object("np_weight") test_object("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

List Comprehension

# This will get executed each time the exercise gets initialized b = 6 # Create a for loop h_letter = for letter in 'human': h_letter.append() print(h_letter) # Create a for loop by list comprehension h_letters = print(h_letters) #For loop h_letter = [] for letter in 'human': h_letter.append(letter) h_letter #For loop with list comprehension h_letters = [letter for letter in 'human'] print(h_letters) test_object("h_letter") test_object("h_letters") test_function("print") success_msg("Great job!")
Use the assignment operator (<-) to create the variable a.

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑

%d bloggers like this: