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 ...
随机推荐
- Several Service Control Manager Issues (Event ID's 7000, 7009, 7011)
https://answers.microsoft.com/en-us/windows/forum/windows_7-performance/several-service-control-mana ...
- requirejs概念
- Facebook广告API系列 1
Facebook广告API系列 1 前言 最近遇到大坑了,居然要去对接facebook的广告API,之前以为是跟鹅厂一样的API体系,看了半天Facebook的文档,冷汗直冒.... 这得一点一点的讲 ...
- NetMagic Simple Overview
参考: NetMagic Startup: How to develop NetMagic rapidly NetMagic Simple Overview NetMagic 是什么? NetMagi ...
- FAST:NetMagic交换机 与 Floodlight控制器 连接实战
设备 NetMagic 08交换机 - 1; 装有Windows 7系统的PC - 1; VMware Workstation, Ubuntu 14.04 64bit - 1; 网线 - 1; 网口转 ...
- BZOJ 1005: [HNOI2008]明明的烦恼(prufer数列)
http://www.lydsy.com/JudgeOnline/problem.php?id=1005 题意: Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标 ...
- vscode中使用EF脚手架生成数据库上下文(scaffold-dbcontext)
目前在vscode上用netcore + ef core,在用dbfirst的方式生成模型和context上下文一直没有找到方法,之前在vs2017中,的nuget管理控制台输入命令: Scaffol ...
- python enumerate用法总结--转载
enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...
- 《剑指offer》第二十四题(反转链表)
// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...
- English trip -- 国际音标表
26个字母音标表 A a [ei] B b [bi:] C c [si:] D d [di:] E e [i:] F f [ef] G g [dʒi:] H h [eit∫] I i [ai] J j ...