python笔记01:基础知识
1.4 数字和表达式
# -*- coding:utf-8 -*-
#1.4 #除法
print 1 / 2
print 1.0 / 2
print 10 / 3
print 10.0 / 3.0
print int(1.0/2)
print float(1/2)
#如果使用“//”,那么就算是浮点数,双斜线也会执行整除
print 1 // 2
print 1.0 // 2.0 #取余
print 10 % 3
print 2.75 % 0.5
print int(2.75 % 0.5) #乘方运算
print 2 ** 3
print -3 ** 2
print (-3) ** 2
#幂运算符比取反的优先级要高 #长整数
print 1000000000000000000
print 11234567890123456L * 123456789345673456L + 100 #十六进制与八进制数
print 0xAF
print 010
输出如下:
0
0.5
3
3.33333333333
0
0.0
0
0.0
1
0.25
0
8
-9
9
1000000000000000000
1386983681400638600588387302184036
175
8
除法运算:单斜线与双斜线
1.5 变量
1.6 语句
1.7 获取用户输入
input()函数:
#
x = input("x= ")
y = input("y= ")
print x * y # 输出如下:
x= 12
y= 23
276
raw_input()函数:
x1 = raw_input("x1= ")
y1 = raw_input("y1= ")
print x1 * y1
File "ex1-7.py", line 8
y2 = int(raw_input("y2= ")
^
SyntaxError: invalid syntax
x2 = int(raw_input("x2= ")
y2 = int(raw_input("y2= ")
print x2 * y2
x3 = int(raw_input("x3= ")
y3 = raw_input("y3= ")
print x3 * y3
输出如下:
x2= 100
y2= 23
2300
x3= 10
y3= 123
123123123123123123123123123123
raw_input()函数对于所有的输入都当作字符串来处理
1.8 函数
print pow(2,10)
print abs(-10)
print 1/2
print round(1.0/2.0) # 结果如下:
1024
10
0
1.0 >>> divmod(5,2)
(2, 1)
1.9 模块
import math
print math.floor(32.9)
print int(math.floor(32.9)) x = math.ceil(12.1001)
print x
print int(x)
# 输出如下:
32.0
32
13.0
13
from math import floor
from math import ceil
from math import sqrt x = floor(23.89)
y = ceil(12.001)
z = sqrt(100.0) print "x is %s, y is %s, z is %s." % (x,y,z) # 输出如下:
x is 23.0, y is 13.0, z is 10.0.
1.9 cmath与复数
>>> from math import sqrt
>>> sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> (1+3j)*(9+4j)
(-3+31j)
1.10 保存并执行
#!/usr/bin/python
chomod a+x hello.py
1.11 字符串
单引号与双引号:
#!/usr/bin/python2.6
print "Hello,World!"
print "This's a test."
print "let\'s go!"
print 'let\'s go!'
print "\"Hello,World!\",she said."
Hello,World!
This's a test.
let's go!
let's go!
"Hello,World!",she said.
字符串拼接:
>>> "let's say " 'Hello,world'
"let's say Hello,world"
>>> x = "Hello, "
>>> y = "World!"
>>> x y
File "<stdin>", line 1
x y
^
SyntaxError: invalid syntax
>>> x + y
'Hello, World!'
>>> x,y
('Hello, ', 'World!')
>>> print repr("Hello,world!")
'Hello,world!'
>>> print str("Hello,world!")
Hello,world!
>>> print str(100000L)
100000
>>> temp = 42
>>> print "The temperature is :",temp
The temperature is : 42
>>> print "The temperature is :" + temp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is :" + `temp`
The temperature is :42
str函数可以把值转换为合理性似的字符串,以便用户理解
input与raw_input的对比?
长字符串、原始字符串和Unicode
>>> print "C:\\Users\\鹏飞\\Desktop\\新建文件夹"
C:\Users\鹏飞\Desktop\新建文件夹
>>> print "C:\Users\鹏飞\Desktop\新建文件夹"
C:\Users\鹏飞\Desktop\新建文件夹
>>> print "C:\nUsers\鹏飞\Desktop\新建文件夹"
C:
Users\鹏飞\Desktop\新建文件夹
>>> print "C:\\nUsers\鹏飞\Desktop\新建文件夹"
C:\nUsers\鹏飞\Desktop\新建文件夹
对于一些特殊的符号,我们在输出时需要用到反斜线进行转义,但是当特殊符号比较多的时候就非常麻烦了,这时候原始字符串就派上用场了。在原始字符串中输入的每个字符串都会与书写的方式保持一致:
>>> print r'C:\nowwhere'
C:\nowwhere
>>> print "C:\nowwhere\zbc\xyz\001"
ValueError: invalid \x escape
>>> print r"C:\nowwhere\zbc\xyz\001"
C:\nowwhere\zbc\xyz\001
>>> print r'let's go'
File "<stdin>", line 1
print r'let's go'
^
SyntaxError: invalid syntax
>>> print 'let\'s go'
let's go
>>> print r'let\'s go'
let\'s go
但是,不能再原始字符串结尾输入反斜杠,除非你对反斜杠进行了转义:
>>> print r"This is illegal\"
File "<stdin>", line 1
print r"This is illegal\"
^
SyntaxError: EOL while scanning string literal
>>> print r"This is illegal \\"
This is illegal \\
>>> print "This is illegal \\"
This is illegal \
Unicode字符串:
>>> u"hello,world"
u'hello,world'
>>> u"hello,world 测试"
u'hello,world \u6d4b\u8bd5'
>>> r"hello,world 测试"
'hello,world \xe6\xb5\x8b\xe8\xaf\x95'
1.12 小结
6.函数
#!/usr/bin/python
# -*- coding:utf-8 -*-
#
import math
import cmath
x1 = int(raw_input("Please input the num of x1 :"))
y1 = input("Please input the num of y1 :")
z1 = raw_input("Please input some words: ") print "x1 * y1 =",x1 * y1
print "x1 * z1 =",x1 * z1 print "ceil(12.001) = ",math.ceil(12.001)
print "floor(32.998) = ",math.floor(32.998)
print "sqrt(16) = ",math.sqrt(16) print "cmath.sqrt(-15) =",cmath.sqrt(-15)
print "round(10.0/3.0) =",round(10.0/3.0)
print "abs(-101) =",abs(-101) # 输出如下:
Please input the num of x1 :6
Please input the num of y1 :10
Please input some words: xyz
x1 * y1 = 60
x1 * z1 = xyz xyz xyz xyz xyz xyz
ceil(12.001) = 13.0
floor(32.998) = 32.0
sqrt(16) = 4.0
cmath.sqrt(-15) = 3.87298334621j
round(10.0/3.0) = 3.0
abs(-101) = 101
python笔记01:基础知识的更多相关文章
- MyBatis:学习笔记(1)——基础知识
MyBatis:学习笔记(1)--基础知识 引入MyBatis JDBC编程的问题及解决设想 ☐ 数据库连接使用时创建,不使用时就释放,频繁开启和关闭,造成数据库资源浪费,影响数据库性能. ☐ 使用数 ...
- C#学习笔记(基础知识回顾)之值类型与引用类型转换(装箱和拆箱)
一:值类型和引用类型的含义参考前一篇文章 C#学习笔记(基础知识回顾)之值类型和引用类型 1.1,C#数据类型分为在栈上分配内存的值类型和在托管堆上分配内存的引用类型.如果int只不过是栈上的一个4字 ...
- C#学习笔记(基础知识回顾)之值传递和引用传递
一:要了解值传递和引用传递,先要知道这两种类型含义,可以参考上一篇 C#学习笔记(基础知识回顾)之值类型和引用类型 二:给方法传递参数分为值传递和引用传递. 2.1在变量通过引用传递给方法时,被调用的 ...
- C#学习笔记(基础知识回顾)之值类型和引用类型
一:C#把数据类型分为值类型和引用类型 1.1:从概念上来看,其区别是值类型直接存储值,而引用类型存储对值的引用. 1.2:这两种类型在内存的不同地方,值类型存储在堆栈中,而引用类型存储在托管对上.存 ...
- Python:笔记(1)——基础语法
Python:笔记(1)——基础语法 我很抱歉有半年没有在博客园写过笔记了,客观因素有一些,但主观原因居多,再多的谴责和批判也都于事无补,我们能做的就是重振旗鼓,继续出发! ——写在Python之前 ...
- Python进阶----计算机基础知识(操作系统多道技术),进程概念, 并发概念,并行概念,多进程实现
Python进阶----计算机基础知识(操作系统多道技术),进程概念, 并发概念,并行概念,多进程实现 一丶进程基础知识 什么是程序: 程序就是一堆文件 什么是进程: 进程就是一个正在 ...
- Quartz学习笔记:基础知识
Quartz学习笔记:基础知识 引入Quartz 关于任务调度 关于任务调度,Java.util.Timer是最简单的一种实现任务调度的方法,简单的使用如下: import java.util.Tim ...
- Python开发(一):Python介绍与基础知识
Python开发(一):Python介绍与基础知识 本次内容 一:Python介绍: 二:Python是一门什么语言 三:Python:安装 四:第一个程序 “Hello world” 五:Pytho ...
- 基于Python的Flask基础知识
Flask简介 Flask 是一个使用 Python 编写的轻量级 Web 应用程序框架.Armin Ronacher带领一个名为Pocco的国际Python爱好者团队开发了Flask. 下面我们简单 ...
- Python第一章-基础知识
第一章:基础知识 1.1 安装python. 直接官网下载最新的python然后默认安装就可以了,然后开始菜单里找到pyhton *.*.* Shell.exe运行python的交互shell ...
随机推荐
- icpc 2017北京 J题 Pangu and Stones 区间DP
#1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...
- 不在同一主机:vsftpd+pam+mysql
配置环境:Centos7上的mariadb + Centos6上的vsftpd 一.安装所需要程序 1.安装vsftpd和pam_mysql(在centos6-->192.168.108.160 ...
- Ansible 操作windows
1.主控端安装ansible 1) pip install ansible 2.主控端安装相关的包 pip install http://github.com/diyan/pywi ...
- VS 常见快捷键有哪些
自动对齐点[编辑]-[高级]-[设置选定内容的格式]或者按Ctrl + K 然后再按Ctrl + F 就好了 你可以在常用快捷键自定义 窗口中进行查看1.进入工具-选项 对话框2.选择[环境]-[键盘 ...
- 算法笔记--java的BigInteger类及BigDecimal类
引包:import java.math.*; BigInteger类: 可以使用构造方法:public BigInteger(String val),或者valueOf(int)函数,如: BigIn ...
- charles的破解方法
http://blog.csdn.net/tech4j/article/details/53509329 mac下的charles遇到的问题. http://blog.csdn.net/songzhu ...
- jsp forward跟redirect区别
forward 相当于php的 require/include 属于服务器包含/跳转 request.getRequestDispatcher("result.jsp").forw ...
- 20170624xlVBA正则分割分类汇总
Sub RegExpSubtotal() '声明变量 Dim Regex As Object '正则对象 Dim Dic As Object '字典对象 Dim Key As String '关键字 ...
- spoj Prime Generator
题意:判断ll-rr范围内的质数. 一个个用miller-rabin算法判断 //#pragma comment(linker,"/STACK:1024000000,1024000000&q ...
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 拷贝用户到登录
在登录时拷贝用户(Copy User on Login) 这个选项在用户尝试登录的时候将会被触发.如果这个选择框被选择的话,当用户使用 LDAP 授权的用户名和密码登录系统的时候,用户将会在内部目录自 ...