python簡單基礎介紹
1.開發環境的搭建
我用的開發環境是pycharm,下載地址:www.jetbrains.com
2.Python變量的定義
a = 10
b = 10
c = a + b
print c
3.Python判斷語句的使用
\#coding=utf-8
score = 0
if score >= 60:
print ("及格")
elif score >= 40:
print ("不好")
else:
print ("太差了")
需要注意編碼方式的指定。
4.python循環的簡單使用
for i in range(0,100):
print (i)
print ("item {0}".format(i))
print ("item {0} {1}".format(i,"hello python"))
注意字符串的拼接,佔位符的使用。
5.python函數的定義
def sayhello():
print ("hello world")
def max(a,b):
if a>b:
return a
else:
return b
sayhello()
print (max(2,3))
這裏定義了兩個函數,調用了兩個函數
6.python類的定義,對象的使用
class Person:
def __init__(self,name):
self._name = name
def run(self):
print ("{0}我正在跑步!".format(self._name))
class Student(Person):
def __init__(self,name):
Person.__init__(self,name)
def study(self):
print ("我正在學習!")
person = Person("張三")
person.run()
student = Student("王武")
student.study()
7,python文件的導入(對外部文件的引用)
這裏需要先創建兩個python文件.
mylib.py
class Dog:
def cry(self):
print ("wang wang wang !")
loadlib.py
from mylib import Dog
dog = Dog()
dog.cry()
運行loadlib.py,即可使用外部文件的類