เพิ่งสมัคร TechJam ไป คือ สมัครอีกแล้ว ปีที่แล้วก็สมัครแล้วก็ไม่ได้เข้าร่วม ปีนี้ก็ยังดึงดันจะสมัคร ปัญหาหลักคือหาทีมไม่ได้ โปรแกรมใหม่ๆ ก็ไม่ได้เขียนมานานมากกกกกก (อยากใส่ ก.ไก่ สักล้านตัว) แต่ก็สมัครแล้วหละนะ ลองพยายามสักหน่อย ก็เลยกะว่าจะลอง python ให้จริงจังขึ้น ลงไว้ในเครื่องแล้วหละ (ถึงแม้ตอนนี้จะจำไม่ได้แล้วว่าลงยังไง) ก็เลยมาหาทวนไวยากรณ์พื้นๆ ของมันซะหน่อย
แต่ถ้าไม่ได้ลง python ก็ไปลองเขียนโปรแกรมเล่นได้ที่ https://coderpad.io/
ส่วนที่หัดเล่น เป็นโปรแกรมสอนชื่อ pycharm-edu สามารถหาได้ที่ https://www.jetbrains.com/pycharm-edu/
คำสั่งที่เรียนได้
1) แสดงข้อความ
print('Hello World')
2) ใส่ comment ขึ้นต้นบรรทัดด้วย #
ใช้ ctrl / เพื่อเติม # หรือเอาออก
3) ตัวแปร
variable = 1
print(variable)
greetings = "greetings"
print("greetings = " + str(greetings))
a = 2
print("a = " + str(a))
4) เปลี่ยนชนิดตัวแปร
int(number)
float()
str()
5) ตัวดำเนินการคณิตศาสตร์
addition ( +),
subtraction ( -)
multiplication ( *)
division ( /)
power ( **)
modulo ( %)
+=
-=
6) ตัวดำเนินการเปรียบเทียบ
==
>
<
>=
<=
!=
or
and
not
7) ต่อข้อความ
+ ต่อข้อความ
8) พิมพ์ hello 10 ครั้ง
hello = "hello"
ten_of_hellos = hello * 10
print(ten_of_hellos)
9) index เริ่มที่ 0
python = "Python"
p_letter = python[0]
print(p_letter)
ถ้า index ติดลบจะนับจากด้านหลังมาให้
long_string = "This is a very long string!"
exclamation = long_string[-1]
10) ตัดบางส่วนในสตริง
monty_python = "Monty Python"
monty = monty_python[:5]
print(monty)
python = monty_python[6:]
print(python)
str[start:end] # items start through end-1
str[start:] # items start through the rest of the array
str[:end] # items from the beginning through end-1
str[:] # a copy of the whole array
11) ใช้ in ตรวจสอบ substring
ice_cream = "ice cream"
print("cream" in ice_cream)
contains = "ice" in ice_cream
print(contains)
12) ฟังก์ชันหาความยาว
len()
13) พิมพ์ “ หรือ ’ ให้ใช้ \ นำหน้า
14) เปลี่ยนเป็นตัวเล็กหรือตัวใหญ่
print(monty_python.lower())
print(monty_python.upper())
15) %s, %d พิมพ์ค่าจากตัวแปร
name = "John"
print("Hello, PyCharm! My name is %s!" % name)
years = 12
print("I'm %d years old" % years)
16) Add item to list
animals = ['elephant', 'lion', 'tiger', "giraffe"] # create new list
print(animals)
animals += ["monkey", 'dog'] # add two items to the list
print(animals)
animals.append("dino") # add one more item to the list using append() method
print(animals)
animals[6] = 'dinosaur'
print(animals)
17) clear list
animals = ['elephant', 'lion', 'tiger', "giraffe", "monkey", 'dog']
print(animals)
animals[1:3] = ['cat']
print(animals)
animals[1:3] = []
print(animals)
animals[:] = []
print(animals)
18) create list with our index
phone_book = {"John": 123, "Jane": 234, "Jerard": 345}
print(phone_book)
phone_book["Jill"] = 345
print(phone_book)
del phone_book['John']
print(phone_book["Jane"])
phone_book = {"John": 123, "Jane": 234, "Jerard": 345}
print(phone_book)
phone_book["Jill"] = 456
print(phone_book)
print(phone_book.keys())
print(phone_book.values())
19) ประโยคเงื่อนไข if ,elif ,else
name = "John"
age = 17
if name == "John" or age == 17:
print("name is John")
print("John is 17 years old")
tasks = ['task1', 'task2']
if len(tasks) == 0:
print("empty")
if x < 0:
print('x < 0')
elif x == 0:
print('x is zero')
elif x == 1:
print('x == 1')
else:
print('non of the above is true')
20) การวนรอบ (loop) for , while
for i in range(5):
print(i)
primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
while square <= 10:
print(square)
square += 1
while True:
print(count)
count += 1
if count >= 5:
break
21) การสร้าง Function โดยใช้ def
def fun():
print('I want to be a function')
for i in range(3):
fun()
def square(x):
print(x ** 2)
square(4)
square(8)
def sum_two_numbers(a, b):
return a + b
c = sum_two_numbers(3, 12)
def multiply_by(a, b=2): #default of b is 2
return a * b
print(multiply_by(3, 47))
print(multiply_by(3))
22) สร้าง Class และ Object
class MyClass:
variable1 = 1
variable2 = 2
def foo(self):
print("Hello from function foo")
my_object = MyClass()
my_object1 = MyClass()
my_object.variable2 = 3
print(my_object.variable2)
print(my_object1.variable2)
my_object.foo()
print(my_object.variable1)
class Car:
color = ""
def description(self):
description_string = "This is a %s car." % self.color
return description_string
car1 = Car()
car2 = Car()
car1.color = "blue"
car2.color = "red"
print(car1.description())
print(car2.description())
23) สร้าง Module และการเรียกใช้ import
class Square:
def __init__(self):
self.sides = 4
square = Square()
print(square.sides)
from calculator import Calculator
calc = Calculator() # here we can use Calculator class directly without prefix calculator.
calc.add(2)
print(calc.get_current())
24) การเขียน อ่าน ไฟล์
f = open("input.txt", "r")
# here we open file "input.txt". Second argument used to identify that we want to read file
# Note: if you want to write to the file use "w" as second argument
for line in f.readlines(): # read lines
print(line)
f.close()
แต่ถ้าไม่ได้ลง python ก็ไปลองเขียนโปรแกรมเล่นได้ที่ https://coderpad.io/
ส่วนที่หัดเล่น เป็นโปรแกรมสอนชื่อ pycharm-edu สามารถหาได้ที่ https://www.jetbrains.com/pycharm-edu/
คำสั่งที่เรียนได้
1) แสดงข้อความ
print('Hello World')
2) ใส่ comment ขึ้นต้นบรรทัดด้วย #
ใช้ ctrl / เพื่อเติม # หรือเอาออก
3) ตัวแปร
variable = 1
print(variable)
greetings = "greetings"
print("greetings = " + str(greetings))
a = 2
print("a = " + str(a))
4) เปลี่ยนชนิดตัวแปร
int(number)
float()
str()
5) ตัวดำเนินการคณิตศาสตร์
addition ( +),
subtraction ( -)
multiplication ( *)
division ( /)
power ( **)
modulo ( %)
+=
-=
6) ตัวดำเนินการเปรียบเทียบ
==
>
<
>=
<=
!=
or
and
not
7) ต่อข้อความ
+ ต่อข้อความ
8) พิมพ์ hello 10 ครั้ง
hello = "hello"
ten_of_hellos = hello * 10
print(ten_of_hellos)
9) index เริ่มที่ 0
python = "Python"
p_letter = python[0]
print(p_letter)
ถ้า index ติดลบจะนับจากด้านหลังมาให้
long_string = "This is a very long string!"
exclamation = long_string[-1]
10) ตัดบางส่วนในสตริง
monty_python = "Monty Python"
monty = monty_python[:5]
print(monty)
python = monty_python[6:]
print(python)
str[start:end] # items start through end-1
str[start:] # items start through the rest of the array
str[:end] # items from the beginning through end-1
str[:] # a copy of the whole array
11) ใช้ in ตรวจสอบ substring
ice_cream = "ice cream"
print("cream" in ice_cream)
contains = "ice" in ice_cream
print(contains)
12) ฟังก์ชันหาความยาว
len()
13) พิมพ์ “ หรือ ’ ให้ใช้ \ นำหน้า
14) เปลี่ยนเป็นตัวเล็กหรือตัวใหญ่
print(monty_python.lower())
print(monty_python.upper())
15) %s, %d พิมพ์ค่าจากตัวแปร
name = "John"
print("Hello, PyCharm! My name is %s!" % name)
years = 12
print("I'm %d years old" % years)
16) Add item to list
animals = ['elephant', 'lion', 'tiger', "giraffe"] # create new list
print(animals)
animals += ["monkey", 'dog'] # add two items to the list
print(animals)
animals.append("dino") # add one more item to the list using append() method
print(animals)
animals[6] = 'dinosaur'
print(animals)
17) clear list
animals = ['elephant', 'lion', 'tiger', "giraffe", "monkey", 'dog']
print(animals)
animals[1:3] = ['cat']
print(animals)
animals[1:3] = []
print(animals)
animals[:] = []
print(animals)
18) create list with our index
phone_book = {"John": 123, "Jane": 234, "Jerard": 345}
print(phone_book)
phone_book["Jill"] = 345
print(phone_book)
del phone_book['John']
print(phone_book["Jane"])
phone_book = {"John": 123, "Jane": 234, "Jerard": 345}
print(phone_book)
phone_book["Jill"] = 456
print(phone_book)
print(phone_book.keys())
print(phone_book.values())
19) ประโยคเงื่อนไข if ,elif ,else
name = "John"
age = 17
if name == "John" or age == 17:
print("name is John")
print("John is 17 years old")
tasks = ['task1', 'task2']
if len(tasks) == 0:
print("empty")
if x < 0:
print('x < 0')
elif x == 0:
print('x is zero')
elif x == 1:
print('x == 1')
else:
print('non of the above is true')
20) การวนรอบ (loop) for , while
for i in range(5):
print(i)
primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
while square <= 10:
print(square)
square += 1
while True:
print(count)
count += 1
if count >= 5:
break
21) การสร้าง Function โดยใช้ def
def fun():
print('I want to be a function')
for i in range(3):
fun()
def square(x):
print(x ** 2)
square(4)
square(8)
def sum_two_numbers(a, b):
return a + b
c = sum_two_numbers(3, 12)
def multiply_by(a, b=2): #default of b is 2
return a * b
print(multiply_by(3, 47))
print(multiply_by(3))
22) สร้าง Class และ Object
class MyClass:
variable1 = 1
variable2 = 2
def foo(self):
print("Hello from function foo")
my_object = MyClass()
my_object1 = MyClass()
my_object.variable2 = 3
print(my_object.variable2)
print(my_object1.variable2)
my_object.foo()
print(my_object.variable1)
class Car:
color = ""
def description(self):
description_string = "This is a %s car." % self.color
return description_string
car1 = Car()
car2 = Car()
car1.color = "blue"
car2.color = "red"
print(car1.description())
print(car2.description())
23) สร้าง Module และการเรียกใช้ import
class Square:
def __init__(self):
self.sides = 4
square = Square()
print(square.sides)
from calculator import Calculator
calc = Calculator() # here we can use Calculator class directly without prefix calculator.
calc.add(2)
print(calc.get_current())
24) การเขียน อ่าน ไฟล์
f = open("input.txt", "r")
# here we open file "input.txt". Second argument used to identify that we want to read file
# Note: if you want to write to the file use "w" as second argument
for line in f.readlines(): # read lines
print(line)
f.close()
Comments
Post a Comment