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 ...
随机推荐
- hdu 3874(树状数组)题解
Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ba ...
- ubuntu 安转redis
一 ,redis 安装配置 在 Ubuntu 系统安装 Redis 可以使用以下命令: sudo apt-get update sudo apt-get install redis-server 这样 ...
- OpenVirteX 创建简易虚拟网络
OpenVirteX 创建简易虚拟网络 1.打开OVX sh OpenVirteX/script/ovx.sh 2.创建mininet物理拓扑 1sw, 2hosts mn --controller= ...
- FAST Hello World - Preparation for software's running environment
Ubuntu 14.04 安装 libpcap-1.1.1 & libpnet-1.1.4 & NMAC function lib 参考: NetMagic.org yacc: com ...
- python json 文件读写
import json test_dict = {,,,} print(test_dict) print(type(test_dict))#字典 #dumps 将数据转换成字符串 json_str = ...
- python里的apply,applymap和map的区别
apply,applymap和map的应用总结: apply 用在dataframe上,用于对row或者column进行计算: applymap 用于dataframe上,是元素级别的操作: map ...
- dataframe使用笔记
dates=pd.date_range(',periods=6) #创建固定频度的时间序列 df=pd.DataFrame(np.random.randn(6,4),index=dates,colum ...
- list_01
双向链表 不支持随机存取([?] / at(?)) A.头尾 添加/移除 A.1.list::push_back(elemValue); A.2.list::pop_back(); A.3.list: ...
- charles的破解方法
http://blog.csdn.net/tech4j/article/details/53509329 mac下的charles遇到的问题. http://blog.csdn.net/songzhu ...
- 正则表达式中 (?=pattern) (?!pattern) (?<=pattern) (?<!pattern) 的使用
2018-08-06 12:33:58 StackOverFlow上有个如下的问题: 这里就会用到正向肯定预查(?=pattern):