当前位置: 首页 > news >正文

Python数据类型、运算符、语句、循环

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1.数据类型

  • Numbers(int、long、float、complex)
  • String
  • Boolean(True、False)
  • List
  • Tuple
  • Dict
  • Set

2.数据类型转换

  • int(float, string, bytes)
  • float(int, string, bytes)
  • complex(int, float, str)
  • str(int, float, complex, bytes, list, tuple, dict, set, 其他类型)
  • bytes(string)
  • list(str, bytes, tuple, dict, set)
  • tuple(str, bytes, list, dict, set)
  • dict(str, list, tuple, set)
  • set(str, bytes, list, tuple, dict)

3.运算符

  • +、-、*、/、**/<、>、!=、//、%、&、|、^、~、>>、<<、<=、>=、==、not、and、or
#两个数字相加
sumNumber=1+2
print(sumNumber)      #输出结果:3
 
#两个字符串相加
sumString="Nice work"
print(sumString)      #输出结果:Nice work
 
#两个数字相减
subNumber=2-1
print(subNumber)      #输出结果:1
 
#两个数字相乘或者字符串重复
multiplicationNumber=2*3
print(multiplicationNumber)      #输出结果:6
multiplicationString="hello"*2
print(multiplicationString)      #输出结果:hellohello
#/---关于*号重复字符串之前的博客已经介绍过了---/
 
#两个数相除
divisionNumber=9/2
print(divisionNumber)      #输出结果:4
divisionNumber=9.0/2
print(divisionNumber)      #输出结果:4.5
divisionNumber=9/2.0
print(divisionNumber)      #输出结果:4.5
#/---除数或被除数中有任意一个是小数的话,商也会保留小数,反之取整---/
 
#求幂运算
powerNumber=2**3 #相当于2的3次幂,就是2*2*2 关于幂运算大家应该在数学里都很熟悉了
print powerNumber       #输出结果:8
 
#小于符号,返回值是bool值
lessThan=1<2
print(lessThan)        #输出结果:True
lessThan=1<1
print(lessThan)        #输出结果:False
 
#大于符号,返回值是bool值
moreThan=2>1
print(moreThan)        #输出结果:True
moreThan=2>2
print(moreThan)        #输出结果:False
 
#不等于符号 返回值是Bool值
notEqual=1!=2
print(notEqual)        #输出结果:True
notEqual=1!=1
print(notEqual)        #输出结果:False
 
#除法运算// 返回商的整数部分,抛弃余数
divisorNumber=10//3
print(divisorNumber)        #输出结果:3
 
#除法运算% 返回商的余数部分,抛弃商
divisorNumber=10%3
print(divisorNumber)        #输出结果:1
divisorNumber=10%1
print(divisorNumber)        #输出结果:0 /--没有余数则返回0--/
divisorNumberx=10//3         #divisorNumberx是商的整数部分
divisorNumbery=10%3         #divisorNumbery是余数
divisorNumberz=3*divisorNumberx+divisorNumbery    #divisorNumberz是除数乘以商的整数部分加上余数,得到的divisorNumberz的值就是被除数
print(divisorNumberz)        #输出结果:10
 
#按位与运算&, 按位与是指一个数字转化为二进制,然后这些二进制的数按位来进行与运算
operationNumber=7&18
print operationNumber        #输出结果:2
'''
这个有点绕,稍微多说下,如果对二进制不是太熟的朋友,可以打开电脑自带的计算器,按住win+q,输入"calculator"。
然后在打开的计算器设置成程序员模式, 就是View(查看)->>programmer(程序员).
然后我们将7转为二进制:111,自动补全8位:00000111,然后将18转为二进制补全8位后得到:00010010
最后将   00000111
跟       00010010 按位进行与运算,
/-
对与运算不熟的朋友可以看看百度百科的介绍,还是很详细的。
http://baike.baidu.com/link?url=lfGJREBvGCY5j7VdF2OO9n2mtIbSyNUD7lZyyY74QIetguL5lXIQUxY38Yr-p4z4WdUvHUKGjw9CDfagiun2Ea
-/
得到结果:00000010
我们都知道10二进制→十进制=2,所以7跟18的按位与的结果是二进制10(十进制2)
'''
 
#按位或运算|, 按位或是指一个数字转化为二进制,然后这些二进制的数按位来进行或运算
operationNumber=7|18
print operationNumber        #输出结果:23   #结题思路和按位与运算的一样,可以参考按位与运算
 
#按位异或
operationNumber=7^18
print operationNumber        #输出结果:21   #结题思路和按位与运算的一样,可以参考按位与运算
 
#按位翻转 ~   按位翻转公式: ~x= - (x+1)
operationNumber=~12  #~12=- (12+1) = -13
print operationNumber        #输出结果:-13   #结题思路和按位与运算的一样,可以参考按位与运算
 
#左移<<
'''
比如18左移就是将他的二进制形式00100100左移,得到00100100(36)。
左移规律:左移一个单位相当于乘2,左移两个单位相当于乘以4,左移三个单位相当于乘以8,
即:      左移n个单位相当于乘以2的n次幂
'''
operationNumber=12<<1
print operationNumber        #输出结果:24
operationNumber=3<<3
print operationNumber        #输出结果:24
 
#右移>>
'''
理解左移以后,右移就很好理解了。
右移是左移的逆运算,将对应的二进制数向右移动。
右移规律:右移一个单位相当于除以2,右移两个单位相当于除以4,右移三个单位相当于除以8,
即:      右移n个单位相当于除以2的n次幂
'''
operationNumber=12>>1
print operationNumber        #输出结果:6
operationNumber=12>>2
print operationNumber        #输出结果:3
 
#小于等于<= 比较运算,小于或等于返回一个bool值
operationNumber=3<=3
print operationNumber        #输出结果:True
operationNumber=3<=2
print operationNumber        #输出结果:False
 
#大于等于>= 比较运算,大于或等于返回一个bool值
operationNumber=2>=3
print operationNumber        #输出结果:False
operationNumber=3>=2
print operationNumber        #输出结果:True
 
#比较两个对象是否相等==
operationNumber=3==2
print operationNumber        #输出结果:False
operationString="hi"=="hi"
print operationString        #输出结果:True
 
#逻辑非 not
operationx=True
operationy=not operationx
print operationy        #输出结果:False
operationz=False
print not operationz        #输出结果:True
 
#逻辑与 and
'''
True and True = True
True and False = False
False and True = False
False and False = False
'''
print True and True        #输出结果:True
 
#逻辑或 or
'''
True or True = True
True or False = True
False or True = True
False or False = False
'''
print False or False        #输出结果:False

4. 运算符优先级

由高到低排序

  • ** 指数
  • ~ 、+、 - ,取反, 正负号
    • 、/、 %、 // 乘,除,取模和取整除
  • +、- 加号减号
  • 、>>、 << 右移,左移运算符
  • & 位与
  • ^ | 位运算符
  • == 、!= 、< 、<= 、>、 >= 比较运算符
  • = 、-= 、+= 、%= 、/= 、//=、 = 、*= 赋值运算符
  • is、 is not 标识运算符
  • in、not in 成员运算符
  • and、or、not 逻辑运算符

4.语句

  • if、if...else...、if...elif...else...
  • switch...case
  • while、while...do、do...while...
  • for

5.字符串操作

  • find、index、count、replace、split
  • capitalize、title、startsWith、endsWith、lstrip
  • lower、upper、ljust、rjust、center
  • rstrip、strip、rfind、rindex、partition
  • rpartition、splitlines、isalpha、isdigit、isalnum
  • isspace、join

6.列表的常见操作

  • append、extend、insert
  • in、not in、index、count
  • del、pop、remove

7.字典的操作

  • len、keys、values、items、has_key

8.方法

# 计算1~num的累积和
    def calculateNum(num):
        result = 0
        i = 1
        while i<=num:
            result = result + i
            i+=1
        return result
    result = calculateNum(100)
    print('1~100的累积和为:%d'%result)

转载于:https://my.oschina.net/chinahufei/blog/3037809

相关文章:

  • 激活效能,CODING 敏捷研发模块上线
  • cmd中subst的使用
  • [MySQL光速入门]003 留点作业...
  • C# - 为值类型重定义相等性
  • Es6初级入门(一)
  • thinkphp+redis实现秒杀,缓存等功能
  • (JS基础)String 类型
  • django2中表单的使用二
  • css学习_css布局案例
  • jsp简单介绍
  • python inspect模块
  • 通过SQL脚本来查询SQLServer 中主外键关系
  • python的pandas库学习笔记
  • 最全的前端模块化方案
  • 深入浅出了解“装箱与拆箱”
  • #Java异常处理
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【347天】每日项目总结系列085(2018.01.18)
  • Redis 中的布隆过滤器
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 前端技术周刊 2019-02-11 Serverless
  • 前端面试之CSS3新特性
  • 浅析微信支付:申请退款、退款回调接口、查询退款
  • 说说动画卡顿的解决方案
  • 云大使推广中的常见热门问题
  • Android开发者必备:推荐一款助力开发的开源APP
  • #pragam once 和 #ifndef 预编译头
  • #stm32驱动外设模块总结w5500模块
  • #数学建模# 线性规划问题的Matlab求解
  • #微信小程序:微信小程序常见的配置传旨
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (TOJ2804)Even? Odd?
  • (四)库存超卖案例实战——优化redis分布式锁
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • ****Linux下Mysql的安装和配置
  • .NET NPOI导出Excel详解
  • .Net 应用中使用dot trace进行性能诊断
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NetCore部署微服务(二)
  • .NET开发不可不知、不可不用的辅助类(一)
  • .secret勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
  • .skip() 和 .only() 的使用
  • @Documented注解的作用
  • [2018-01-08] Python强化周的第一天
  • [Big Data - Kafka] kafka学习笔记:知识点整理
  • [c++] 什么是平凡类型,标准布局类型,POD类型,聚合体
  • [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
  • [DNS网络] 网页无法打开、显示不全、加载卡顿缓慢 | 解决方案
  • [flink总结]什么是flink背压 ,有什么危害? 如何解决flink背压?flink如何保证端到端一致性?
  • [javaSE] 数据结构(二叉查找树-插入节点)
  • [LeetCode 687]最长同值路径