python3菜鸟教程-Python3⼊门教程简单但⽐较不错本⽂适合有Java编程经验的程序员快速熟悉Python
本⽂程序在windows xp+python3.1a1 测试通过.
本⽂提到的idle指python shell,即安装python后你在菜单看到的IDLE(python gui)
在idle⾥ctrl+n可以打开⼀个新窗⼝,输⼊源码后ctrl+s可以保存,f5运⾏程序.
凡打开新窗⼝即指ctrl+n的操作.
1 你好
#打开新窗⼝,输⼊:
#! /usr/bin/python
# -*- coding: utf8 -*-
s1=input("Input your name:")
print("你好,%s" % s1)
'''
知识点:
* input("某字符串")函数:显⽰"某字符串",并等待⽤户输⼊.
* print()函数:如何打印.
* 如何应⽤中⽂
java中final的用法* 如何⽤多⾏注释
'''
2 字符串和数字
但有趣的是,在javascript⾥我们会理想当然的将字符串和数字连接,因为是动态语⾔嘛.但在Python⾥有点诡异,如下:
#! /usr/bin/python
a=2
b="test"
c=a+b
运⾏这⾏程序会出错,提⽰你字符串和数字不能连接,于是只好⽤内置函数进⾏转换
#! /usr/bin/python
#运⾏这⾏程序会出错,提⽰你字符串和数字不能连接,于是只好⽤内置函数进⾏转换
a=2
b="test"
c=str(a)+b
d="1111"
e=a+int(d)
#How to print multiply values
print ("c is %s,e is %i" % (c,e))
'''
知识点:
* ⽤int和str函数将字符串和数字进⾏转换
* 打印以#开头,⽽不是习惯的//
* 打印多个参数的⽅式
'''
3 列表
#! /usr/bin/python
# -*- coding: utf8 -*-
#列表类似Javascript的数组,⽅便易⽤
#定义元组
word=['a','b','c','d','e','f','g']
#如何通过索引访问元组⾥的元素
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: ")
print (b) # index 1 and 2 elements of word. c=word[:2]
print ("c is: ")
print (c) # index 0 and 1 elements of word. d=word[0:]
print ("d is: ")
fork函数在哪个库print (d) # All elements of word.
#元组可以合并
e=word[:2]+word[2:]
print ("e is: ")
print (e) # All elements of word.
f=word[-1]
print ("f is: ")
print (f) # The last elements of word.
g=word[-4:-2]
print ("g is: ")
print (g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: ")
print (h) # The last two elements.
i=word[:-2]
print ("i is: ")
print (i) # Everything except the last two characters l=len(word)
print ("Length of word is: "+ str(l))
print ("Adds new element")
word.append('h')
print (word)
#删除元素
del word[0]
print (word)
del word[1:3]
print (word)
'''
知识点:
* 列表长度是动态的,可任意添加删除元素.
* ⽤索引可以很⽅便访问元素,甚⾄返回⼀个⼦列表
* 更多⽅法请参考Python的⽂档
'''
4 字典
#! /usr/bin/python
x={'a':'aaa','b':'bbb','c':12}
print (x['a'])
print (x['b'])
print (x['c'])
for key in x:
print ("Key is %s and value is %s" % (key,x[key]))
'''
知识点:
* 将他当Java的Map来⽤即可.
'''
5 字符串
⽐起C/C++,Python处理字符串的⽅式实在太让⼈感动了.把字符串当列表来⽤吧. #! /usr/bin/python
框架漏洞word="abcdefg"
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: "+b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: "+c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: "+d) # All elements of word.
e=word[:2]+word[2:]
print ("e is: "+e) # All elements of word.
f=word[-1]
print ("f is: "+f) # The last elements of word.
g=word[-4:-2]
print ("g is: "+g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: "+h) # The last two elements.
current time翻译i=word[:-2]
print ("i is: "+i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))
中⽂和英⽂的字符串长度是否⼀样?
#! /usr/bin/python
# -*- coding: utf8 -*-
s=input("输⼊你的中⽂名,按回车继续");
print ("你的名字是 : " +s)
undergraduate是什么学位l=len(s)
print ("你中⽂名字的长度是:"+str(l))
知识点:
类似Java,在python3⾥所有字符串都是unicode,所以长度⼀致.
6 条件和循环语句
#! /usr/bin/python
#条件和循环语句
x=int(input("Please enter an integer:")) if x<0:
x=0
print ("Negative changed to zero")
elif x==0:
print ("Zero")
else:
print ("More")
# Loops List
a = ['cat', 'window', 'defenestrate']
for x in a:
print (x, len(x))
#知识点:
# * 条件和循环语句
# * 如何得到控制台输⼊
7 函数
#! /usr/bin/python
# -*- coding: utf8 -*-
def sum(a,b):
return a+b
func = sum
r = func(5,6)
javascript和mysql菜鸟教程print (r)
# 提供默认值
def add(a,b=2):
return a+b
r=add(1)
print (r)
r=add(1,5)
print (r)
⼀个好⽤的函数

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。