从今天开始,本人就要学习Python了。
今天主要学习了Python的一些基础知识,主要涵盖了如下内容:
1、数字和表达式
在Python交互式解析器中,可以直接进行运算。
一般的数学函数可以引入math块,一些复杂的数学函数需要引入cmath块。Python支持长整形数据:1000000000L。取整运算的两种方法:(1):math.floor(X) (2) int(X)
在Python中,八进制和十六进制的书写如下:
0xAF , 010...
首数字都是零。
2、变量
Python中的变量可以包括字母、数字和下划线,但是不能以数字开头。在使用变量以前要对变量进行赋值,没有赋值的变量时没有意义的。
3、获取用户输入
两种方法:input 和 raw_input,二者的区别在于,input会假象获取的输入是合法的Python表达式,raw_input 获取的是用户的原始输入,并将其存储在字符串中。除非特殊需要,一般使用raw_input来获取用户输入。
4、字符串
通常在输入一些单双引号都有的句子或者说一些在Python中被认作具有特殊含义的字符,需要将这些特殊字符使用转义字符来加以区别。一个更好的解决类似问题的办法是使用原始字符串输入,例如:
print r' string '避免了大量使用转义字符的麻烦,但是需要指出的是,使用原始字符串输入在语句的末尾不能使用" \ ",如果必须要使用,还是要加上转义字符 " \\ "。
长字符串。如果需要输入一个很长的字符串并且需要跨越多行,那么可以使用3个单引号来代替普通引号。
unicode字符,就是在输入的字符串前加 u 。如 print u' hello world !'
数值被转换成字符串的两种机制:函数str和repr。二者的区别在于,str将数值转换成用户可以理解形式的字符串,而repr会将创建一个字符串,这个字符串是python合法形式的表达式。
5、模块
在我们使用一些非python默认函数的时候,可以使用引用模块的形式来调用函数。
可以使用以下两种方式:
(1)from 一个模块 import 函数
然后可以直接使用函数。
(2)import 一个模块
module.函数
建议使用第二种形式调用函数,因为在有些地方会导致命名的冲突。
今天练习使用的代码:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 2 ** 3
8
>>> -3 **2
-9
>>> (-3) ** 2
9
>>> 1111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111L
>>> 11111111111111111111111111111111111111l
11111111111111111111111111111111111111L
>>> 2324324523423432432432432L * 2132132143432423423432L
4955767028159349056163576467822347272465546624L
>>> 0X86
134
>>> 01010101010101010101
18300341342965825L
>>> x = 5
>>> x % 4
1
>>> x *9
45
>>> x+1
6
>>> y= x+1
>>> y
6
>>> print x+y
11
>>> z =x +y
>>> z
11
>>> input("the meaning of life: ")
the meaning of life: 34
34
>>> x = input("x: ")
x: 9
>>> y =input("y: ")
y: -9
>>> print x+y
0
>>> if 1 == 2:print 'One equals two '
>>> if 1 == 2: print 'One equals two '
>>>
>>> if 1 ==1 : print 'One equals one '
One equals one
>>> if time % 60 == 0: print 'On the hour '
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
if time % 60 == 0: print 'On the hour '
NameError: name 'time' is not defined
>>> pow(2.9)
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
pow(2.9)
TypeError: pow expected at least 2 arguments, got 1
>>> pow(2 9)
SyntaxError: invalid syntax
>>> pow(2,9)
512
>>> pow(2, 2+1)
8
>>> x= input(" 你想要的数字:“)
SyntaxError: EOL while scanning string literal
>>> x= input(" 你想要的数字:")
SyntaxError: invalid syntax
>>> x = input(" 你想要的数字: ")
SyntaxError: invalid syntax
>>> x= input(" 123:")
SyntaxError: invalid syntax
>>> x = input(" 你想要的数字: ")
你想要的数字: pow (2,10)
>>> x
1024
>>> abs(-1)
1
>>> 1/2
0
>>> round(1.0/2.0)
1.0
>>> floor (1,2)
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
floor (1,2)
NameError: name 'floor' is not defined
>>> import math
>>> math.floor(30.1)
30.0
>>> floor(30.9)
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
floor(30.9)
NameError: name 'floor' is not defined
>>> math.floor(23.9)
23.0
>>> int(math.floor(20.5))
20
>>> from math import floor
>>> floor(34.1)
34.0
>>> big=math.floor
>>> big(23.1)
23.0
>>> int(34.2
)
34
>>> from math import sprt
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
from math import sprt
ImportError: cannot import name sprt
>>> from math import sprt
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
from math import sprt
ImportError: cannot import name sprt
>>> import math
>>> math.sprt(2)
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
math.sprt(2)
AttributeError: 'module' object has no attribute 'sprt'
>>> from math import sqrt
>>> sqrt(-2)
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
sqrt(-2)
ValueError: math domain error
>>> from math import sqrt
>>> sqrt(4)
2.0
>>> imprt math
SyntaxError: invalid syntax
>>> import math
>>> cmath.sqrt(-2)
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
cmath.sqrt(-2)
NameError: name 'cmath' is not defined
>>> import cmath
>>> cmath.sqrt(-2)
1.4142135623730951j
>>> from math import sqrt
>>> from cmath import sqrt
>>> sqrt(3)
(1.7320508075688772+0j)
>>> from math import sqrt
>>> sqrt(3)
1.7320508075688772
>>> ================================ RESTART ================================
>>>
Hello, world
>>> ================================ RESTART ================================
>>>
What is your name? bing
Hello, bing !
>>> x= "hello"
>>> y= "world"
>>> xy
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
xy
NameError: name 'xy' is not defined
>>> x y
SyntaxError: invalid syntax
>>> x
'hello'
>>> she's mothen
SyntaxError: EOL while scanning string literal
>>>
>>> she\'s mother
SyntaxError: unexpected character after line continuation character
>>> "hello, world "
'hello, world '
>>> she
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
she
NameError: name 'she' is not defined
>>> 'she 's mother '
SyntaxError: invalid syntax
>>> ' she\ 's mother '
SyntaxError: invalid syntax
>>> ' she \'s mother '
" she 's mother "
>>> print ' hello world '
hello world
>>> 10000000l
10000000L
>>> print ' 10000L '
10000L
>>> print " 10000L "
10000L
>>> 10000L
10000L
>>> print 10000l
10000
>>> print repr("hello, world !")
'hello, world !'
>>> print repr(10000L)
10000L
>>> print str("hello, world !"
)
hello, world !
>>> temp =42
>>> print "the temperature is" + temp
Traceback (most recent call last):
File "<pyshell#99>", line 1, in <module>
print "the temperature is" + temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "the temperature is" + `temp`
the temperature is42
>>> print "the temperature is " + `temp`
the temperature is 42
>>> name = input("what is your name ? ")
what is your name ? bing
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>
name = input("what is your name ? ")
File "<string>", line 1, in <module>
NameError: name 'bing' is not defined
>>> name = input("what is your name ? ")
what is your name ? 1223
>>> name = input("what is your name ? ");
what is your name ? 122
>>> print "hello " + name "!"
SyntaxError: invalid syntax
>>> print "hello " + name " ! "
SyntaxError: invalid syntax
>>> print "hello " + name + "!"
Traceback (most recent call last):
File "<pyshell#107>", line 1, in <module>
print "hello " + name + "!"
TypeError: cannot concatenate 'str' and 'int' objects
>>> name = input("what is your name ? ");
what is your name ? 123
>>> print "hello " + name + "!"
Traceback (most recent call last):
File "<pyshell#109>", line 1, in <module>
print "hello " + name + "!"
TypeError: cannot concatenate 'str' and 'int' objects
>>> ================================ RESTART ================================
>>>
what is your name ? 123print "hello " + name + "!"
Traceback (most recent call last):
File "D:/python/test", line 1, in <module>
name = input("what is your name ? ")
File "<string>", line 1
123print "hello " + name + "!"
^
SyntaxError: invalid syntax
>>> ================================ RESTART ================================
>>>
what is your name ? 123
Traceback (most recent call last):
File "D:/python/test", line 2, in <module>
print "hello " + name + "!"
TypeError: cannot concatenate 'str' and 'int' objects
>>> ================================ RESTART ================================
>>>
what is your name ? "bing"
hello bing!
>>> print ''' This is a vert
idd
fdfd
" hello world "
there '''
This is a vert
idd
fdfd
" hello world "
there
>>> print " hello, \
world ! "
hello, world !
>>> print 'C:\\ on"
SyntaxError: EOL while scanning string literal
>>> print "C:\\ on"
C:\ on
>>> print ' hello, \n world ! '
hello,
world !
>>> print 'c:\nowhere'
c:
owhere
>>> print r'c:\nere\ndf'
c:\nere\ndf
>>> print r"this is not corrert\"
SyntaxError: EOL while scanning string literal
>>> print r"this is not corrert" "\\"
this is not corrert\
>>> print r"this is not corrert" "\"
SyntaxError: EOL while scanning string literal
>>> u'hello world !'
u'hello world !'
>>> print u'你好'
ÄãoÃ
>>>
print r'你好'
你好
>>>
--转自
该贴由koei123转至本版2015-2-6 5:06:14
该贴由koei123转至本版2015-2-6 5:10:13