Python的输入和raw_input()内建函数等以及相关运算符
2015-09-19 00:19
866 查看
1. print 输出
>>> :主提示符,表示解释器在等你输入下一个语句
... :次提示符,表示解释器在提示你它在等你输入下一个字符。
%s,%d, %f等,分别是用字符串,整数,浮点数替换。
其中,print "%s is a beautiful girl , she's %d ." 和C语言中的printf() 函数很相似,从Python与C语言的关系不难看到两者的相似之处。
>>> yes = "YES"
>>> no = "NO"
>>>
>>> print "I say ", yes,"u say ",no
I say YES u say NO
>>>
>>> print "I say %s , u say %s" % (yes*3,no*3)
I say YESYESYES , u say NONONO
>>>
带有两个参数的print :
2.
(1)raw_input()内建函数,
该函数接收用户标准输入,然后赋值给变量。 下面的输入信息赋值给了变量pyStr 从hello ---->world
(2) 使用in()函数 : 将数值字符串转换成整数值
3. 其他
1)注释: Python中的注释使用 #号,也支持和java里面的文档注释,但是用的少
2)运算符:
+ - * / // **
注意: / // 两个都是除法运算符,//表示浮点除法 ** 表示乘方运算
< <=
> >= !=<>
注意: 3<4<5 可以理解为: 3<4 and 4<5
逻辑运算符: and or not 且,或,非
3)变量名: 以大写或者小写字母开头,或者下划线开头,其余可以是数字,字母,下划线等
4)五种基本数据类型:
int long
bool(TRUE---非0,FALSE---0) float complex (复合类型)
注意: 还有一种数据类型,decimal : 该种数据类型用于十进制浮点数,不是内建类型,需要导入decimal模块。
5)字符串:包含在单引号,双引号,三引号中,角标从0开始,最后一个的索引是 -1
[] :索引运算符
[:] :切片运算符
多元赋值,并且互相交换两个值:,这里的交换值的方式和c或者其他语言中的交换方式简化多了。
6)列表和元组
列表使用 [] --->中括号 元组使用 ()----->小括号 可以存储任意数量,任意类型,这个和java中的List有区别
注意: 我在上面的列子中加入了true,以及TRUE都提示了错误信息; 我去角标为4的元素,也提示了错误信息,越界了。
7)字典:这个和java中的hashMap hashSet 方式有点相似,都是键值对的形式。
上面列子中: “key1”:"value1" 既可以用单引号,也可以用双引号;
添加元素: aDict["key3"] = 888 添加进去后,在字典的头部
注意: 此处遇到一个异常错误,与代码的缩进相关、
>>> for k in aDict:
... print k , aDict[k]
File "<stdin>", line 2
print k , aDict[k]
^
IndentationError: expected an indented block
>>>
IndentationError: expected an indented block
表示代码的缩进不对,于是我在print 的前面加了个tab
代码就正常了,看来这个缩进还是有点那啥......万一成百上千行代码,少了个缩进....不过放心,上面的错误会提示到是哪一处,不过好像也不好找啊。
8)if-elif-else注意代码的缩进
if
if代码块
elif
elif代码块
else
else代码块
9)while循环 ,
注意:
>>> while count < 9:
后面有个: 表示还有继续输入,还有代码块
print count 和 count +=1 的首行,都添加了一个空格
>>> count =0
>>> while count < 9:
... print count
... count +=1
...
0
1
2
3
4
5
6
7
8
>>>
for 和 range函数
>>> strArr = ["u","r","my","sunshine"]
>>> for s in strArr:
... print s
...
u
r
my
sunshine
>>>
>>> print range(5)
[0, 1, 2, 3, 4]
>>>
range函数的相关介绍:
Help on built-in function range in module __builtin__:
range(...)
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
(END)
others:
range函数可以携带三个参数,其中第三个参数是增量,后面一个数在前一个数的基础上按照第三个增量参数增加。比如:
[0, 5, 10, 15, 20]
10)异常处理
try except java中用的是catch 用catch多好,非要搞独立派......
11)列表解析
注意:使用一个for循环将所有的值放到列表中,
>>> s = [x**2 for x in range(4)]
>>> print s
[0, 1, 4, 9]
>>>
>>> str = [x*2 for x in range(8) if not x % 2]
>>> print str
[0, 4, 8, 12]
>>>
12)函数
定义一个函数:def func (variable)
使用def 关键字,func是方法名,小括号里面是参数
>>> def func(num):
... if num >0:
... print "the number is positive"
... elif num < 0:
... print "the number is nagative"
...
>>>
>>> 12
12
>>> func(43)
the number is positive
>>>
>>> func(-12)
the number is nagative
>>>
注意缩进: 靠。。。。。
导入模块:
import sys
sys.platform
sys.version
13)相关的一些练习
这个后面在添加,发现必须要搞个ide,我下载了Pycharm
这个错误,解决办法就是直接在loopChars的方法生命地方,加一个参数self
练习: 输入一段字符串,并使用while 和 for循环打印出来。
14) Python IDE PyCharm
http://www.jetbrains.com/pycharm/ 网站中也有相关教程。
>>> :主提示符,表示解释器在等你输入下一个语句
... :次提示符,表示解释器在提示你它在等你输入下一个字符。
%s,%d, %f等,分别是用字符串,整数,浮点数替换。
>>> myString = "Hello World" >>> >>> print myString Hello World >>> >>> myString 'Hello World' >>> >>> >>> print "%s is a beautiful girl , she's %d ." % ("Lucy",21) Lucy is a beautiful girl , she's 21 . >>>
其中,print "%s is a beautiful girl , she's %d ." 和C语言中的printf() 函数很相似,从Python与C语言的关系不难看到两者的相似之处。
>>> yes = "YES"
>>> no = "NO"
>>>
>>> print "I say ", yes,"u say ",no
I say YES u say NO
>>>
>>> print "I say %s , u say %s" % (yes*3,no*3)
I say YESYESYES , u say NONONO
>>>
带有两个参数的print :
strArray = ["hello","my","name","is","fulairy"] for s in strArray: print(s,len(s))结果输出:
/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/apple/project/PycharmProjects/untitled/python_demo/fibonacci.py ('hello', 5) ('my', 2) ('name', 4) ('is', 2) ('fulairy', 7) Process finished with exit code 0
2.
(1)raw_input()内建函数,
该函数接收用户标准输入,然后赋值给变量。 下面的输入信息赋值给了变量pyStr 从hello ---->world
>>> pyStr = raw_input("hello") helloworld >>> pyStr 'world' >>>
(2) 使用in()函数 : 将数值字符串转换成整数值
>>> testInt = 0 >>> print testInt 0 >>> testInt = int("2222") >>> testInt 2222 >>>
3. 其他
1)注释: Python中的注释使用 #号,也支持和java里面的文档注释,但是用的少
2)运算符:
+ - * / // **
注意: / // 两个都是除法运算符,//表示浮点除法 ** 表示乘方运算
< <=
> >= !=<>
注意: 3<4<5 可以理解为: 3<4 and 4<5
逻辑运算符: and or not 且,或,非
3)变量名: 以大写或者小写字母开头,或者下划线开头,其余可以是数字,字母,下划线等
4)五种基本数据类型:
int long
bool(TRUE---非0,FALSE---0) float complex (复合类型)
注意: 还有一种数据类型,decimal : 该种数据类型用于十进制浮点数,不是内建类型,需要导入decimal模块。
5)字符串:包含在单引号,双引号,三引号中,角标从0开始,最后一个的索引是 -1
[] :索引运算符
[:] :切片运算符
>>> pythonStr = "Python" >>> pythonStr[2] 't'
>>> pythonStr[2:4] #半开半闭区间 'th' >>>
>>> pythonStr[2:4] #半开半闭区间 'th' >>>
>>> pythonStr[2:]
'thon'
>>> pythonStr[0:]
'Python'
>>>
多元赋值,并且互相交换两个值:,这里的交换值的方式和c或者其他语言中的交换方式简化多了。
x, y ,z = 1,2,3 print x,y,z #change value x between y x , y = y,x print x,y,z输出分别是:123 和 321
6)列表和元组
列表使用 [] --->中括号 元组使用 ()----->小括号 可以存储任意数量,任意类型,这个和java中的List有区别
>>> pythonArr = ["hello","FuLaiRy",222,444,TRUE] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'TRUE' is not defined >>> pythonArr = ["hello","FuLaiRy",222,444] >>> pythonArr ['hello', 'FuLaiRy', 222, 444] >>> pythonArr[4] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> pythonArr[3] 444 >>> pythonArr[0:] ['hello', 'FuLaiRy', 222, 444] >>>
注意: 我在上面的列子中加入了true,以及TRUE都提示了错误信息; 我去角标为4的元素,也提示了错误信息,越界了。
7)字典:这个和java中的hashMap hashSet 方式有点相似,都是键值对的形式。
>>> aDict = {"key1":"value1","key2":"value2"} >>> aDict {'key2': 'value2', 'key1': 'value1'} >>> >>> aDict["key3"] = 888 >>> aDict {'key3': 888, 'key2': 'value2', 'key1': 'value1'} >>>
上面列子中: “key1”:"value1" 既可以用单引号,也可以用双引号;
添加元素: aDict["key3"] = 888 添加进去后,在字典的头部
>>> for key in aDict : ... print key ... key3 key2 key1 >>> >>> for key in aDict : ... print key,aDict[key] ... key3 888 key2 value2 key1 value1 >>>
注意: 此处遇到一个异常错误,与代码的缩进相关、
>>> for k in aDict:
... print k , aDict[k]
File "<stdin>", line 2
print k , aDict[k]
^
IndentationError: expected an indented block
>>>
IndentationError: expected an indented block
表示代码的缩进不对,于是我在print 的前面加了个tab
代码就正常了,看来这个缩进还是有点那啥......万一成百上千行代码,少了个缩进....不过放心,上面的错误会提示到是哪一处,不过好像也不好找啊。
8)if-elif-else注意代码的缩进
if
if代码块
elif
elif代码块
else
else代码块
9)while循环 ,
注意:
>>> while count < 9:
后面有个: 表示还有继续输入,还有代码块
print count 和 count +=1 的首行,都添加了一个空格
>>> count =0
>>> while count < 9:
... print count
... count +=1
...
0
1
2
3
4
5
6
7
8
>>>
for 和 range函数
>>> strArr = ["u","r","my","sunshine"]
>>> for s in strArr:
... print s
...
u
r
my
sunshine
>>>
>>> print range(5)
[0, 1, 2, 3, 4]
>>>
range函数的相关介绍:
Help on built-in function range in module __builtin__:
range(...)
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
(END)
others:
range函数可以携带三个参数,其中第三个参数是增量,后面一个数在前一个数的基础上按照第三个增量参数增加。比如:
print range(0,21,5) #range from 0 to 21上面这句话打印的是
[0, 5, 10, 15, 20]
10)异常处理
try except java中用的是catch 用catch多好,非要搞独立派......
11)列表解析
注意:使用一个for循环将所有的值放到列表中,
>>> s = [x**2 for x in range(4)]
>>> print s
[0, 1, 4, 9]
>>>
>>> str = [x*2 for x in range(8) if not x % 2]
>>> print str
[0, 4, 8, 12]
>>>
12)函数
定义一个函数:def func (variable)
使用def 关键字,func是方法名,小括号里面是参数
>>> def func(num):
... if num >0:
... print "the number is positive"
... elif num < 0:
... print "the number is nagative"
...
>>>
>>> 12
12
>>> func(43)
the number is positive
>>>
>>> func(-12)
the number is nagative
>>>
注意缩进: 靠。。。。。
导入模块:
import sys
sys.platform
sys.version
13)相关的一些练习
这个后面在添加,发现必须要搞个ide,我下载了Pycharm
/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/apple/project/PycharmProjects/untitled/python_demo/__init__.py pls input some characters:sdvasd' Traceback (most recent call last): File "/Users/apple/project/PycharmProjects/untitled/python_demo/__init__.py", line 66, in <module> instance.loopChars(testStr) TypeError: loopChars() takes exactly 1 argument (2 given) Process finished with exit code 1
这个错误,解决办法就是直接在loopChars的方法生命地方,加一个参数self
练习: 输入一段字符串,并使用while 和 for循环打印出来。
class CharactersInput: def loopChars(self,strArr): if len(strArr) <= 0: print "Error : empty , not suggested." else: print "use while loop" count = len(strArr) i = 0 while i < count: s = strArr[i] print "#" + strArr[i], i +=1 print "use for loop :" for s in strArr: print s testStr = raw_input("pls input some characters:") instance = CharactersInput() instance.loopChars(testStr)
14) Python IDE PyCharm
http://www.jetbrains.com/pycharm/ 网站中也有相关教程。
相关文章推荐
- Windows Sublime Text3配置Python
- python 用request模块发送cookie,发送多文件。django后台做转发。代码
- 零基础学python-18.4 函数对象:间接调用函数
- 零基础学python-18.4 函数对象:间接调用函数
- python 什么叫迭代
- Ubuntu安装Python的包管理工具Pip
- xjtu_001:寻找完全数,并对完全数分类
- python语言简单了解
- Python C :ctypes库
- [LeetCode]题解(python):015-3Sum
- python 3.3.2报错:No module named 'urllib2'
- python 3.3.2报错:No module named 'urllib2'
- python下libsvm的使用
- 送给半路出家的Pythoner
- Sublime Text 3 打造舒服的工具(直接运行python)
- Windows下如何构建和发布Python模块
- Python网页爬虫学习
- ubuntu14.04环境下python3 Image.show()不显示图片问题
- Python入门
- Python 二维字典查询