python基础教程-基础知识

1.安装

Linux,mac系统自带,可直接使用


2.运算

/  除法

// 整除

% 取余

** 幂运算


3.长整数型

长整型,末尾加L  1000000000000L

可以和普通整型混用,普通整数不能大于2147483647


4.十六进制和八进制


5.变量

以字母数字下划线组成,不能以数字开头

使用首先要初始化

x=2

x*2


6.语句

print

input()

x=input("x:")


if 1 == 2: print  'one equals two'  注意冒号,等号空格,引号    交互式,需要按两次回车才会执行if

if 1 == 1: print  'one equals one'

if time % 60 == 0: print 'On the hour'


7.函数

pow(2,3) 内建函数,函数调用

abs,取绝对值  round浮点数四舍五入为最近的整数值

floor


8.模块

import math

math.floor(32.9)  ->32.0

在确保不会从多个不同模块导入多个同名函数,可以使用函数,而不需要模块做前缀  from模块import函数

from math import sqrt

sqrt(9)  ->3.0


sqrt计算数的平方根

cmath.sqrt计算负数平方根(复数)


应坚持使用普通的import


9.保存并执行程序

File->New Window创建编辑窗口 选择目录 保存程序hello.py


10.通过命令提示符运行

python hello.py


希望像其他程序一样运行python程序,在unix标准实现方法:在脚本首行前面加上#!,在其后面加上解释脚本的程序的绝对路径

#!/usr/bin/env python

当新旧版本共存时,需要找到新版本的可执行文件,如#!/usr/bin/env python2

在实际运行前,要让脚本具有可执行属性,chmod a+x hello.py

运行 $ hello.py 或./hello.py


11.单双引号转义

>>>"\"hello,world!\" she said"

'""hello.world!" she said'

>>>"hello,world"

'hello,world"

>>>"let's go"

let's go

>>>'let\'s go'

"let's go"


+拼接字符串 “hello, "+"world"

'hello, world'


12.str, repr

str表示为更于理解的形式,repr和反引号'则为合法的python表达式

>>>print repr("hello,world")

'hello,world'

>>>print repr(1000L)

1000L


>>>print str("hello,world")

hello,world

>>>print str(1000L)

1000


>>>temp = 42

>>>print "The temperature is" + 'temp'

The temperature is 42


13.input和raw_input

input要求用户输入带双引号

raw_input会把输入当做原始数据再放入字符串中,所以尽量使用raw_input


14.长字符串,原始字符串和unicode

字符串跨行,在开头结尾处使用'''三个引号代替普通引号

或者在一行的最后使用\转义换行

\n转义换行,\\再转义


原始字符串,r‘C:\nowhere’

不能在原始字符串结尾加入反斜线,

可以将反斜线单独作为一个字符处理

>>>print r'C:\program files\foo\bar' '\\'

C:\program files\foo\bar\


python中普通字符以8位ASCII码存储,unicode字符串则存储为16位unicode字符


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值