python入门(一)

python入门

import this

import this

The Zen of Python, by Tim Peters

> Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

print

print ("Hello world!")

Hello world!

Python shell

name = "Newone"
print("hello %s,Nice to meet you!" %name)

hello Newone,Nice to meet you!

name = "Lisi"
print("hello %s,Nice to meet you!" %name)
name = "Newone"
print("hello %s,Nice to meet you!" %name)

age = 27
print ("you are %d !" %age)

n=100
print ("you print is %r" %n)

n="abc"
print ("you print is %r" %n)

name = "zhang"
age =22
print ("student info:%s %d." %(name,age))

hello Lisi,Nice to meet you!
hello Newone,Nice to meet you!
you are 27 !
you print is 100
you print is 'abc'
student info:zhang 22.

input

raw_input()
这是python2的读字符串的

input()在python2里面只能读数字

if

a = 3
b=2
if a>2 : 
    print ("a is max")
else :
    print ("b is max.")

a is max

in 和not in

hi = "hello world"
if "hello" in hi:
	print("contain")
else:
	print("not contain")

contain

布尔型

a = True
if a:
	print("a is True")
else: 
	print("a is not True")

a is True

多重条件

result = 72

if result>=90:
	print ("excellent")
elif result >=70:
	print ("good")
elif result >=60:
	print("suitable")
else:
	print("Not good")

good

for

for i in "hello world":
    print(i)

h
e
l
l
o

w
o
r
l
d

遍历字典

fruits =['banana','apple','mango']
for fruit in fruits:
	print(fruit)

banana
apple
mango

控制循环字数

for i in range(5):
	print(i)

0
1
2
3
4

range

python3 中的range()与python2中的xrange()相同。

数据与字典

数组

Lists = [1,2,3,'a',5]
Lists[3] =  3

字典

dicts = {"username":"zhang",'password':123456}
dicts.keys()    #用python2没测出来
dicts.values()  #用python2没测出来
dicts.items()   #用python2没测出来

for k,v in dicts.items():
	print("dicts keys is %r" %k)
	print("dicts values is %r" %v)

打印
dicts keys is 'username'
dicts values is 'zhang'
dicts keys is 'password'
dicts values is 123456

key不能相同,value可以相同。

函数和类

打印结果

def add(a,b):
	print(a+b)

add(3,5)

8

设置返回值

def add(a,b):
	return(a+b)

add(3,5) #python3里面不显示结果。

8

设置默认参数

def add(a=1,b=2):
 	return a+b

add()    #python3里面不显示结果。
add(3,5) #python3里面不显示结果。
posted @ 2018-11-07 16:00  生姜大叔  阅读(72)  评论(0编辑  收藏  举报