วันศุกร์ที่ 6 พฤษภาคม พ.ศ. 2559

การรับค่าจากแป้นพิมพ์ Input from Keyboard

ตอนที่ 4 การรับข้อมูลจากคีย์บอร์ด

ในการรับข้อมูลจากคีย์บอร์ดก็ง่ายมากคือใช้ input("ข้อความที่จะพิมพ์ออกคีย์บอร์ด")
ถ้าจะนำตัวแปรมารับข้อมูลเพื่อนำไปใช้ก็ทำได้เช่น

variable_name = input("คำถาม")

ส่วนการแสดงข้อมูลข้อความออกทางหน้าจอ เราจะเชื่อมข้อมูลกับตัวแปรด้วย
เครื่องหมาย + เช่น

print("ข้อความ"+variable_name+"ข้อความ2")

มาดูโค้ดเต็มๆได้ที่


name=input("What is your name?")
print("Hello "+name+" Nice to meet you.")
age=input("How old are you?")
print("Oh you're "+age+" years old")

ภาษา python ง่ายนิดเดี๋ยว

แหล่งที่มาแอบอ้าง http://www.python-course.eu/input.php

การดำเนินการทางคณิตศาสตร์ใน python

ตอนที่ 3 arithmetic operators in python

การดำเนินการทางคณิตศาสตร์ใน python สรุปจากโค้ดเลยได้ดังนี้


a=10
b=15
c=5.5
d=-125

print(a+b)    #10+15
print(b-a)    #15-10
print(a*c)    #10x5.5
print(b/a)    #15/10
print(b//a)   #15//10 result only floor
print(b%a)    #15%a   modulo the remainder after division
print((-a)+b) #-10+15
print(abs(d)) #absolute a
print(a**b)   #a^b or a exponent b

ลำดับการดำเนินการของตัวดำเนินการ order of operations

Parentheses หรือ ( )
Exponents หรือ **
Multiplication and Division หรือ * , / , // , %
Addition and Subtraction หรือ + , -

มีเทคนิคการจำง่ายๆคือ Please excuse my dear aunt Sally. แหล่งที่มาแอบอ้าง https://en.wikibooks.org/wiki/Python_Programming/Basic_Math

วันพฤหัสบดีที่ 5 พฤษภาคม พ.ศ. 2559

การประกาศตัวแปรในภาษา python


ตอนที่ 2 การประกาศตัวแปรในภาษา python

data types ในภาษา python มีคร่าวๆดังนี้

Types are a category for things within Python with which Python will work. Types are
integers 
Whole numbers from negative infinity to infinity, such as 1, 0, -5, etc.
float 
Short for "floating point number," any rational number, usually used with decimals such as 2.8 or 3.14159.
strings 
A set of letters, numbers, or other characters.
tuples 
A list with a fixed number of elements. ie x=(1,2,3) parentheses makes it a tuple.
lists 
A list without a fixed number of elements. ie x=[1,2,3] note the square brackets, a list
dictionaries 
A type with multiple elements i.e. x = {1: 'a','b': 2,3: 3} where you address the elements with, e.g., a text.

เผิ่นว่ามี data types ชนิดต่างๆใน python ดังนี้
intergers ก็คือตัวเลขต่างๆตั้งแต่ค่าลบ ไปจนถึงค่าบวกเช่น 1,0,-5, อื่นๆ
float เป็นชื่อย่อของ floating point numer คือจำนวนจริงที่ใช้กับเลขฐาน 10 เช่น 2.8 หรือ 3.14159
strings ก็คือเซ็ทของข้อความ, ตัวเลข, หรืออักขระอื่นๆ
tuples คือ รายการของจำนวนตัวเลขที่มีการกำหนดค่าคงที่ เช่น x=(1,2,3) อยู่ในวงเล็บ
lists คือ รายการของตัวเลขที่ไม่ได้ถูกกำหนดค่าคงที่ เช่น x=[1,2,3]
dictionaries คือชนิดของข้อมูลหลายองค์ประกอบ เช่น x={1:'a','b':2,3:3} 
where you address the elements with, e.g., a text.
------
การประกาศตัวแปร
ก็พิมพ์ค่าของตัวแปรเข้าไปได้เลยโดยไม่จำเป็นต้องระบุชนิด เหมือนภาษา C/C++
ซึ่งถือว่าค่อนข้างง่ายมากๆ 
เช่น 

a=10
b=20.3
c='a'
d=(1,2,3)
e=[4,5,6]
x={1: 'a','b': 2,3: 3}

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(x))

ลองรันดูด้วยคำสั่ง python3 ตามด้วยชื่อไฟล์ที่ท่านเขียน ก็จะได้



<class 'int'>

<class 'float'>

<class 'str'>

<class 'tuple'>

<class 'list'>

<class 'dict'>


ออกทางหน้าจอ

ลืมบอกไปเรื่องคำสงวน คือคำที่ไม่สามารถเอามาใช้เป็นชื่อตัวแปรได้ มีดังนี้

คำสงวน
            คำสงวน คือ ชื่อหรือคำที่ภาษาไพธอนสงวนไว้เฉพาะเพื่อใช้เป็นคำสั่ง   หรือมีไว้เพื่อเขียนเป็นโครงสร้างของตัวภาษาเอง ฉะนั้นผู้เขียนโปรแกรมจึงควรหลีกเลี่ยงคำสงวนเหล่านี้ในการตั้งชื่อโปรแกรม  ตัวแปร  หรือชื่อใด ๆ ก็ตามที่ตั้งขึ้นมาใหม่แล้วตรงกับคำสงวน  คำสงวนมีด้วยกัน 31 คำดังต่อไปนี้
and
as
assert
break
class
continue
def
del
elif
else
except
exec
finally
for
from
global
if
import
in
is
lambda
not
or
pass
print
raise
return
try

แหล่งที่มาแอบอ้าง
https://en.wikiversity.org/wiki/Python/Basic_data_types
https://sites.google.com/site/dotpython/installation/variable

การเตรียมพร้อม Environment สำหรับการเขียนเกมส์ด้วย Python บนลินุกซ์

ตอนที่ 1 เตรียมสภาพแวดล้อมให้พร้อมก่อนเริ่ม python

กลับมาอีกครั้งตามคำเรียกร้องกับตอนที่ 1

Environment requirement สำหรับการเขียนเกมส์ด้วย Python บนลินุกซ์ แน่นอนเราต้องมี
python interpreter อยู่แล้วล่ะครับ
\แหละที่เหลือก็ตามนั้น อ่านดูเอาครับ

การติดตั้ง python interpreter

เข้าไปที่ https://www.python.org/downloads/ จัดการดาวน์โหลด python 3.5.1 ขึ้นมาเลย
พอดาวน์โหลดเสร็จก็แตกไฟล์

ตามด้วย command เหล่านี้ (โปรดอย่าถามเรื่อง Basic ของ Linux นะครับ เราเป็นโปรแกรมเมอร์แล้ว จะข้ามก้าวจุดนี้ (ไม่ใช่ก้าวข้ามนะ ข้ามก้าว) จุดนี้ ต้องเข้าใจได้ถึงพื้นฐาน)

    ./configure
    make
    make test
    sudo make install

หลังจากติดตั้ง python เป็นที่เรียบร้อยแล้ว ให้ลองเช็คจาก command ว่า

python ดูครับ แล้วให้ลองพิมพ์ คำสั่ง print("Hello world") ถ้าอีกบรรทัดออกมาว่า Hello World
ก็เป็นอันเสร็จสิ้นกลิ่นน้ำนมของการติดตั้ง python แต่ยัง ยังเหลือ pygame ที่เราจะต้องติดตั้งอีกนะ

การติดตั้งpygame


#install dependencies
sudo apt-get install mercurial python3-dev python3-numpy libav-tools \
    libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \
    libsdl1.2-dev  libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev
 
# Grab source
hg clone https://bitbucket.org/pygame/pygame
 
# Finally build and install
cd pygame
python3 setup.py build
sudo python3 setup.py install


ตามโค้ดด้านบนนี้เลย

พอติดตั้งเสร็จแล้วการจะรันเกมส์ (ให้ลองดาวน์โหลดเกมส์อะไรก็ได้มาจาก ww.pygame.org)
เมื่อดาวน์โหลดมาแล้ว ให้ใช้คำสั่ง python3 ในการ compile นะแจ้

แหล่งที่มาแอบอ้าง
https://www.python.org/downloads/
http://www.pygame.org/wiki/CompileUbuntu
https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=linux






วันจันทร์ที่ 2 พฤษภาคม พ.ศ. 2559

การกลับมาของการเขียนบล็อคของนีโอ

ผมชื่อนีโอ และผมกลับมาพร้อมกับการเขียนบล็อคครั้งใหม่ ซึ่งห่างจากครั้งที่เขียนบล็อคนี้ครั้งแรกไป 6 ปี ตั้งแต่ปี 2553 นานมาก การกลับมาครั้งนี้ได้แรกบันดาลใจจาก concernedApe ที่สร้างเกมส์ Stardew Valley ซึ่งผมก็ซื้อเกมส์แท้มาเล่นเป็นที่เรียบร้อยแล้ว แต่การกลับมาครั้งนี้ของผม ก็คือการเขียน python ได้รับการแนะนำจาก ดร.ชูเกียรติ ศักดิ์จิรพาพงษ์ อาจารย์ที่เคยสอนผมสมัยเรียนมหาวิทยาลัย เพื่อสร้างเกมส์ และแน่นอน ยังคงพัฒนาบน Platform Linux จอมโปรดของผมเช่นเคยครับ

วันเสาร์ที่ 19 กุมภาพันธ์ พ.ศ. 2554

To enable mod_rewrite on linux if you don't have any module"mod_rewrite" before

To enable the mod_rewrite module at apache, I just simply do this 3 step:

First, add the rewrite.load to /etc/apache2/mods-enabled/
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/

Second, edit the apache configuration for my virtualhosting. For example, in my computer I only have one virtual hosting (/var/www) that is default from installation, so I make some adjustment for that (In my case I have to edit this file /etc/apache2/sites-enabled/000-default)
sudo vi /etc/apache2/sites-enabled/000-default
Change the Allowoverride value to all for the document root directory
For example, I made change to this part of the configuration:

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/
</Directory>


Finnaly, just restart the apache
sudo /etc/init.d/apache2 restart

I dont't have any problem when try to install wordpress and get it's mod_rewrite rules works for my local site. Just put the .htaccess ,which has the RewriteEngine On, at the wordpress installation directory.

Wish that could help ;)



Thank you for jalanbuntu 


http://ubuntuforums.org/archive/index.php/t-7304.html