python简单基础介绍

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,即可使用外部文件的类

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章