python学习第二天

3年前 (2017-05-23) gtj python 0评论 已收录 832℃
# -- coding: utf-8 -- python2.7


循环

for i in range(0,100):
print (i)
会打印0-100.

引用循环语句里的内容:
拼接字符串。
# This Python file uses the following encoding: utf-8
print ("你好,python")
for i in range(0,100):
print ("this {0},{1}".format(i,"hello python"))

结果:
this 0,hello python
this 1,hello python
this 2,hello python
this 3,hello python
this 4,hello python
this 5,hello python
this 6,hello python
.................
例子二:
name = raw_input("请输入你的用户名:")
if name == "jack":
print "jack"
elif name == "tom":
print "tom"
elif name == "lili":
print "lili"
elif name == "wangwu":
print "wangwu"
else:
print "用户名不正确"

判断语句

例子一:

# -*- coding:utf-8 -*-
score = 90
if score>=80 :
    print ("很好")
elif score>=60:
    print("及格")
else:
     print("很差")

结果:
很好

例子二:

##模拟用户输入的时候的判断语句
#/usr/bin/env python
# -*- coding:utf-8 -*-
import getpass
name = raw_input('请输入你的用户名:')
pwd = getpass.getpass('请输入你的密码:')
if name == "jack" and pwd == "123abc":
print "欢迎,jack!"
else:
print "你的用户名和密码错误"

例子三:
# -- coding: utf-8 --
import getpass

_username = 'suixiaofeng'
_password = 'abc123'
username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("welcome user {name} ...".format(name=username))
else:
print("Invalid username or password!")
结果:
username:suixiaofeng
password:abc123
welcome user suixiaofeng ...

Process finished with exit code 0
例子4:
# -- coding: utf-8 --
#Author:随小风

age_of_suixiaofeng = 56

guess_age = int(input("guess age:"))
if guess_age == age_of_suixiaofeng :
print("yes, you got it.")
elif guess_age > age_of_suixiaofeng:
print("think smaller...")
else:
print("think bigger!")

定义函数

例子:
def sayhello():
print ("hello world")
sayhello()
结果:
hello world
例子二:
def max(a,b):
if a>b:
return a
else:
return b
print(max(2,3))
结果:
3

面向对象

如何定义类

例子:
class hello:
def sayhello(self):
print("hello python")

h =hello()
h.sayhello()
结果:
hello python

加入构造方法


class hello:
def __init__(self,name):
self._name= name
def sayhello(self):
print("hello {0}".format(self._name))

h =hello("nihao")
h.sayhello()
结果:
hello nihao

类的继承


class hello:
def __init__(self,name):
self._name= name
def sayhello(self):
print("hello {0}".format(self._name))

class Hi(hello):
def __init__(self,name):
hello.__init__(self,name)

def sayhi(self):
print ("Hi {0}".format(self._name))
x1 = Hi("nihenhao")
x1.sayhi()
结果:
Hi nihenhao

引入python文件

mylib.py
class hello:
def sayhello(self):
print ("hello getingjing")

test.py
引用:
方法一:
import mylib
h = mylib.hello()
h.sayhello()
方法二:
from mylib import hello
h =hello()
h.sayhello()
结果:
hello getingjing

linux下python

#!/usr/bin/env python ###env查找python这个环境变量
#!/usr/bin/python #寻找/usr/bin下的python

交互式输入

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

name = input()
print('Hello,', name)
结果:
交互式输入test
Hello,test
例二:
#可以模拟注册用户的时候的场景。
# -- coding: utf-8 --
#Author:随小风

name = input("name:")
#age = input("age:")
age =int((input("age:"))) #强制转化为int
print(type(age)) #打印一个变量的字符类型
job = input("job:")
salary = input("salary:")
info = '''
-----------------info of %s ------------
Name:%s
Age:%d
Job:%s
Salary:%s
''' %(name,name,age,job,salary) #方法一

info2 = '''
-----------------info of %{_name}------------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,
_age=age,
_job=job,
_salary=salary) #方法二,推荐方法二

print(info2)
结果:
name:tege
age:22

job:it
salary:ss

-----------------info of %tege------------
Name:tege
Age:22
Job:it
Salary:ss

循环

# -- coding: utf-8 --
#Author:随小风
count = 0
while True:
print("count:",count)
count = count + 1 # count +=1
if count == 1000:
break #跳出

循环加判断

例一:
##输入一个数,可以输入三次,对了就结束循环,不对第三次跳出循环。
# -- coding: utf-8 --
#Author:随小风
age_of_oldboy = 56
count = 0
while True: #循环输入
if count == 3: #限定輸入次數為三次
break
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("yes, you got it.")
break #猜对了就退出
elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count = count + 1

例一优化:

# -- coding: utf-8 --
#Author:随小风
age_of_oldboy = 56
count = 0
while count < 3: #循环输入 guess_age = int(input("guess age:")) if guess_age == age_of_oldboy : print("yes, you got it.") break #猜对了就退出 elif guess_age > age_of_oldboy:
print("think smaller...")
else:
print("think bigger!")
count = count + 1
else:
print("you have tried too many times...系統禁止輸入")

continue

例子:

# -- coding: utf-8 --
#Author:随小风

for i in range(0,10):
     if i < 5:
        print("loop",i)
     else:
         continue #跳出本次循环,进入下次循环
print("jee")
结果:
loop 0
loop 1
loop 2
loop 3
loop 4
jee
例子二:
for i in range(10):
    print('------------',i)
    for j in range(10):
        print(j)
        if j > 5:
            break
结果:
------------ 0
0
1
2
3
4
5
6
------------ 1
0
1
-------

待续...

博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!