博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python之基础篇
阅读量:7255 次
发布时间:2019-06-29

本文共 1885 字,大约阅读时间需要 6 分钟。

变量类型

Number(数字)       包括int,long,float,double,complex

String(字符串)     例如:hello,"hello",hello
List(列表)         例如:[1,2,3],[1,2,3,[1,2,3],4]
Dictionary(字典)   例如:{1:"nihao",2:"hello"}
Tuple(元组)        例如:(1,2,3,abc)
File(文件)         例如:f = open(a.txt,rw)

类型转换

ord()    将字符转换成ASCII

chr()    将ASCII转换成字符
hex()    将整数转换成十六进制
oct()    将整数转换成八进制
bin()    将整数转换成二进制

 

此外还有int(),str()等。

 

1.运算符

print 1/2 #0print 1/2. #0.5print 1.0/2 #0.5print 1%3 #取模 1

除了用浮点(不知是否得当)来得到精确数据外,还可以引入__future__的division

from __future__ import division #导入精确除法 print 3/4 #0.75print 3//4 #0 强制整除

2.控制语句

SyntaxError: invalid syntax

原因可能是if when等控制语句首行末尾忘记写:

或则用tab来替代四个空格

当然也有可能是版本问题,因为3.0以后print 已经是一个函数了 因此必须print("msg")

 

age=25if age<18:    print "你还年轻"elif 18
<25: print "你不小了" else: print "要开始发力了" print "不管什么时候,理想都不能失去" #*****for*****for i in range(1,5): print i;else: print "结束for循环" #*****while***** i=15;while i<10: print i; i+=1;else:#当while一开始不满足的时候 就执行 "结束while循环"

结果

要开始发力了不管什么时候,理想都不能失去1234结束for循环

关于break与continue

#breakt="True";while t:    t2=raw_input("enter your flag:");    if t2=="stop":        break;    print "continue"print "结束"    while t:    t2=raw_input("enter your flag:");    print type(t2)    if t2=="stop":        break;    if len(t2)<5:        continue;    print "continue"print "结束"

 

 

3.变量

x=input("first input number:")y=input("second input number:")print x*yhello="hello"msg=hello+" world"print msg

结果:

first input number:10second input number:20200hello world

 

4.函数

在python的世界里,函数确切的定义应该是内建函数,比如一个人有听的功能

因此他不需要import

比如

r=pow(2,3)print r #结果8

5.模块

当内建函数不满足我们需求的时候,我们通过import来引入更多的函数

import mathmath.floor(32.9)#32.0 print int(math.floor(32.9))#32

当然,如果觉得每次输入math都嫌麻烦,可以

from math import floorfloor(r)

同样达到我们想要的效果

关于str与repr

'''str是给人看的repr是给解释器看的'''hello='hello world'print str(hello)#hello worldprint repr(hello)#'hello world'c=100Lprint str(c)#100print repr(c)#100L

关于python的整体把握,可以参考

转载地址:http://uskdm.baihongyu.com/

你可能感兴趣的文章
ASP.NET Core 中文文档 第四章 MVC(4.2)控制器操作的路由
查看>>
git-stash用法小结
查看>>
Mac OS X 在Finder新建文本文件
查看>>
软件测试 -- 元素定位
查看>>
iOS 改变UILabel部分颜色
查看>>
python3下载图片
查看>>
牛B的调试工具:OzCode
查看>>
spider RPC入门指南
查看>>
Nginx 多站点配置
查看>>
批处理删除文件夹下所有文件和文件夹
查看>>
C# WinForm下,隐藏主窗体的方法
查看>>
机器学习-损失函数 (转)
查看>>
WEB项目 后台接收前端数组
查看>>
信号量与条件变量的区别
查看>>
关于plsql连接oracle数据库session失效时间设置
查看>>
三阶魔方花样玩法,公式汇总
查看>>
Python os
查看>>
Ubuntu使用ssh公钥实现免密码登录
查看>>
记一次720度托马斯回旋过狗!
查看>>
Atitit 图像处理的心得与疑惑 attilax总结
查看>>