python【1】-基础知识

简介:

1.简介

python是一种解释性的、面向对象的、带有动态语义的高级程序设计语言。

廖雪峰网站:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000   

下载python:https://www.python.org/downloads/

交互式python解释器:IDLE

python是通过缩进来组织代码段的,而不像c#中的大括号。一般推荐使用四个空格作为缩进。

2.表达式

除法:

默认情况下,一个整数除以另一个整数,结果仍然为整数:1/2 =0

如果其中一个数是浮点数,那么结果就是浮点数。

1.0/2 =>0.5

1/2.0=>0.5

1/2. =>0.5

双斜线:使用双斜线即使参数是浮点数,结果也会是整除。

1.0//2=》0.0

幂运算:

2**3=>8

-2**3=>-8

获取用户输入:input(str)方法。

>>> x=input('x=')
x=5
>>> y=input('y=')
y=6
>>> print x*y
30

3.函数

幂函数:pow(2,3)=>8

绝对值:abs(-10)=>10

四舍五入:round(0.4)=>0.0      round(0.6)=>1.0

4.模块

可以使用import命令导入特殊的模块来增强功能。

例如:

import math

math.floor(1.9)

=>1.0

cmath和复数:

>>> import cmath
>>> cmath.sqrt(-1)
1j

python本身是支持复数计算的:

>>> (1+2j)+(3+4j)
(4+6j)
>>> (1+2j)-(3+4j)
(-2-2j)
>>> (1+2j)*(3+4j)
(-5+10j)

5.保存和执行

python保存为.py为后缀的文件

6.注释

用#井号表示注释,井号右侧的内容不会被解释。

7.字符串

字符串可以使用单引号或者双引号,如果中间字符串内容也包括引号,那么可以使用转义字符。

>>> "let's say \"hello world\""
'let\'s say "hello world"'

拼接字符串使用加号:

>>> x='a'
>>> y='b'
>>> x+y
'ab'

字符串表示:str和repr

  • str:把值转换为合理的字符串形式,便于用户理解;
  • repr:把值转换为合理的python表达式的形式。

>>> x="hello world";
>>> print str(x)
hello world
>>> print repr(x)
'hello world'
>>> y=10000L
>>> print str(y)
10000
>>> print repr(y)
10000L

input 与raw_input

  • input 会假设用户输入的是合法的python表达式,在下面的例子中只有输入字符串时才会执行成功。
  • raw_input会把输入作为原始数据,然后将其放入到字符串中。

一般情况下,建议使用raw_input.

#使用Input

>>> name=input('your name?')
your name?chenjing
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    name=input('your name?')
  File "<string>", line 1, in <module>
NameError: name 'chenjing' is not defined
>>> name=input('your name?')
your name?'chenjing'
>>> print 'hello'+name
hellochenjing

#使用raw_input

>>> name=raw_input('your name?')
your name?chenjing
>>> print 'hello '+name
hello chenjing

注意raw_input是以字符串形式返回,如果想要整型需要使用int()进行类型转换。例如:

birth=int(raw_input('your birth'))
if(birth<2000):
  print 'before 00'
else:
  print 'after 00'

长字符串

如果比较长的字符串需要跨行,那么可以使用三个引号,而且其中的引号也不必再使用转义字符了。

>>> str='''hello world
hello 'cathy'
hello chenjing'''
>>> print str
hello world
hello 'cathy'
hello chenjing

原始字符串

原始字符串以r开头,原始字符串不会把反斜线作为特殊字符处理,在原始字符串输入的每个字符都会直接保持一致输出,不过原始字符串不能以反斜线结尾。

c:\program files
>>> print r'c:\newfile'
c:\newfile

字符串编码

ASCII编码是1个字节,而Unicode编码通常是2个字节。

UTF-8编码:可变长编码。把一个Unicode字符根据不同的数字大小编码成1-6个字节,常用的英文字母被编码成1个字节,汉字通常是3个字节,只有很生僻的字符才会被编码成4-6个字节

在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。

Python支持ASCII编码,通过ord()和chr()函数,可以把字母和对应的数字相互转换。

print ord('A') >>>65
print chr(65) >>>A

Python在后来添加了对Unicode的支持,以Unicode表示的字符串用u'...'表示

u'测试'

想转换为utf-8需要encode('utf-8')方法

u'测试'.encode('utf-8')

Image(2)

字符串格式化

Image(3)

还可以指定是否补位0,以及小数位数

Image(4)

如果字符串中包含%,则使用两个百分号代替%%

Image(5)

 

%s和%r:%s等同于调用str()函数,%r等同于调用repr()。例如:
x="There are %d books."%10
print x
print "I said %r"%x
print "I said %s"%x
print str(x)
print repr(x)
 
运行结果:
There are 10 books.
I said 'There are 10 books.'
I said There are 10 books.
There are 10 books.
'There are 10 books.'

8.时间和日期

Python内置的datetime模块提供了datetime,date,time等常用时间类型。

  • date()和time()方法可以分别提取日期和时间;
  • strftime()方法用于将时间格式化为字符串;
  • strptime()方法用于将字符串转换为日期格式;
  • 两个时间相减可以获得delta类型的时间差。

# -*- coding: cp936 -*-

from datetime import datetime,date,time

dt=datetime(2016,5,23,14,40,0)

print(dt.day)#23

print(dt.hour)#14

print(dt.date())#2016-05-23

print(dt.time())#14:40:00

#格式化字符串

print(dt.strftime("%Y-%m-%d %H:%M"))#2016-05-23 14:40

#字符串转换为时间

dt=datetime.strptime('201605231440','%Y%m%d%H%M')

print(dt) #2016-05-23 14:40:00

#时间差

dt1=datetime(2016,5,23,14,40)

dt2=datetime(2016,5,1)

delta=dt1-dt2

print(type(delta))#<type 'datetime.timedelta'>

print(delta)#22 days, 14:40:00

print(dt+delta)#2016-06-15 05:20:00




    本文转自 陈敬(Cathy) 博客园博客,原文链接:

http://www.cnblogs.com/janes/p/5520177.html

,如需转载请自行联系原作者


相关文章
|
3月前
|
存储 Shell 程序员
Python 自动化指南(繁琐工作自动化)第二版:一、PYTHON 基础知识
Python 自动化指南(繁琐工作自动化)第二版:一、PYTHON 基础知识
39 0
|
4月前
|
算法 Java 程序员
Python 基础知识: 解释 Python 的垃圾回收机制是如何工作的?
Python 基础知识: 解释 Python 的垃圾回收机制是如何工作的?
|
1月前
|
存储 机器学习/深度学习 数据挖掘
Python编程语言:基础知识与实用代码示例
本文将带您走进Python编程世界,介绍Python的基础知识,并通过实用代码示例展示Python的魅力和应用。
35 0
|
7天前
|
存储 文件存储 Python
python基础知识(一)
python基础(一){编码,字符串格式化,数据类型,运算符,推导式(简化生成数据),函数编程,模块}
|
9天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
32 0
|
11天前
|
Python
python基础知识
【4月更文挑战第15天】python基础知识
23 7
|
1月前
|
Java Python
【python基础知识】python中怎么判断两个字符串是否相等
【python基础知识】python中怎么判断两个字符串是否相等
30 0
|
1月前
|
程序员 Python
【python基础知识】python怎么查看对象的属性
【python基础知识】python怎么查看对象的属性
19 0
|
3月前
|
存储 BI 网络安全
正在等待继续编辑 - Python - 基础知识专题 - 配置文件与日志管理
正在等待继续编辑 - Python - 基础知识专题 - 配置文件与日志管理
22 0