Python的输入和raw_input()内建函数等以及相关运算符
来源:互联网 发布:python冷门知识 编辑:程序博客网 时间:2024/06/16 06:19
1. print 输出
>>> :主提示符,表示解释器在等你输入下一个语句
... :次提示符,表示解释器在提示你它在等你输入下一个字符。
%s,%d, %f等,分别是用字符串,整数,浮点数替换。
>>> myString = "Hello World">>> >>> print myStringHello 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 testInt0>>> testInt = int("2222")>>> testInt2222>>>
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
列表使用 [] --->中括号 元组使用 ()----->小括号 可以存储任意数量,任意类型,这个和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]>>>
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... key3key2key1>>> >>> for key in aDict :... print key,aDict[key]... key3 888key2 value2key1 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)
print range(0,21,5) #range from 0 to 21上面这句话打印的是
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__.pypls 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 stestStr = raw_input("pls input some characters:")instance = CharactersInput()instance.loopChars(testStr)
14) Python IDE PyCharm
http://www.jetbrains.com/pycharm/ 网站中也有相关教程。
- Python的输入和raw_input()内建函数等以及相关运算符
- Python(2.2) 程序输入和raw_input内建函数
- python程序输入和内建函数raw_input()
- python 输入函数raw_input和input以及eval函数
- 输入函数input()和raw_input()的区别
- Python raw_input()和input() 函数 读取交互输入
- Python 读取键盘输入 :raw_input()和input() 函数 读取交互输入
- Python输入函数:raw_input() 与 input() 的区别
- python中raw_input()以及input()函数的区别
- python的【字符串string】:运算符、内建函数
- python input函数和raw_input函数的区别
- python中的input函数和raw_input函数的区别
- python 输入print 和 输出raw_input
- Python 读取输入 input 和raw_input
- python用户输入问题,input()和raw_input()
- Python的raw_input()和input()
- python的input和raw_input
- python的input()和raw_input()
- 关于activity
- SweetAlert-js应用
- Android 绘图进阶(四):自定义View属性(灰常重要)
- leetcode刷题,总结,记录,备忘66
- clone graph
- Python的输入和raw_input()内建函数等以及相关运算符
- JSP九大内置对象
- 无法掌握也不要尝试掌握
- 【炫技帖】模拟用户中心下拉时背景放大效果
- POJ 3461.Oulipo(KMP)
- 0918知识点总结
- C++之多态性与虚函数
- Android电话拦截实现以及TelephonyManager监听的取消
- 从一个登陆页跳转到一个具有多个导航页面的工具栏控制器UITabbarController中