[分享]Python学习入门笔记-基础知识 _Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
3
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1940 | 回复: 2   主题: [分享]Python学习入门笔记-基础知识         下一篇 
    本主题由 koei123 于 2015-2-6 5:10:13 移动
紫狐狸988
注册用户
等级:上士
经验:261
发帖:29
精华:0
注册:2012-3-5
状态:离线
发送短消息息给紫狐狸988 加好友    发送短消息息给紫狐狸988 发消息
发表于: IP:您无权察看 2015-1-15 16:01:02 | [全部帖] [楼主帖] 楼主

从今天开始,本人就要学习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



赞(0)    操作        顶端 
rui.yuan
注册用户
等级:中校
经验:1637
发帖:29
精华:0
注册:1970-1-1
状态:离线
发送短消息息给rui.yuan 加好友    发送短消息息给rui.yuan 发消息
发表于: IP:您无权察看 2015-1-15 20:21:43 | [全部帖] [楼主帖] 2  楼

#!/usr/bin/envpython

print("Content-type:text/html\r\n\r\n")

print("<html>")

print("<head>")

print("")

print("</head>")

print("<body>")

print("<h2>HelloWord!ThisismyfirstCGIprogram</h2>")

print("</body>")

print("</html>")

我也来玩玩

北京联动北方科技有限公司



赞(0)    操作        顶端 
yaoxia.wu
注册用户
等级:中校
经验:2318
发帖:53
精华:1
注册:1970-1-1
状态:离线
发送短消息息给yaoxia.wu 加好友    发送短消息息给yaoxia.wu 发消息
发表于: IP:您无权察看 2015-1-15 23:22:28 | [全部帖] [楼主帖] 3  楼

整理的比较好



赞(0)    操作        顶端 
总帖数
3
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论