python基础:语法,数据类型和运算符

如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#print("你好, 世界!");
#print("world");
'''
if True:
    print("True")
else:
    print("FALSE")

input("\n\nPress the enter key to exit.")

import sys; x = 'runoob'; sys.stdout.write(x + '\n');

x="a"
y="b"

#换行输出
print(x)
print(y)
print('----------')

#不换行输出
print(x),
print(y),

#不换行输出
print(x,y)

import sys
sys.argv[0] = "hello"
print(sys.argv)

counter = 100 #赋值整型变量
miles = 1000.0 #浮点型
name = "John" #字符串
print(counter)
print(miles)
print(name)
#Python数值类型
str = 'hello world!'
print(str)
print(str[0])
print(str[2:5])
print(str[2:])
print(str*2)
print(str + "TEST")

#Python列表
list = ['runoob', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']
print(list)
print(list[0])
print(list[1:3])
print (list[2:])
print(tinylist*2)
print(list+tinylist)

#python 元组:不能二次赋值,相当于只读列表
tuple = ('runboo', 786, 2.23, 'jhn', 70.2)
tinytuple = (123, 'john')
print(tuple)
print(tuple[0])
print(tuple[1:3])
print(tuple[2:])
print(tinytuple*2)
print(tuple+tinytuple)

#python字典:除列表外最为灵活的内置数据结构类型,列表是有序的对象集合,字典是无序的对象集合。
#两者区别:字典当中的元素是通过键来存取的,而不是通过偏移存取。
#字典用“{}”标识,由索引(key)和它对应的值(value)组成
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john', 'code':6734,'dept':'sales'}
print(dict['one'])
print(dict[2])
print(tinydict)
print(tinydict.keys())
print(tinydict.values())

#数据类型分数字型和非数字型
#数字型包括:整型,长整型,浮点型,复数型
#非数字型包括:字符串,列表,元组和字典
#非数字型共同点:可使用切片、链接(+)、重复(*)、取值(a[])等相关运算
#非数字型的不同点:列表可直接赋值,元组不可赋值,字典可按照dict[k] = v的方式赋值
n = "runboo"
print(isinstance(n, int))


###算术运算符,比较(关系)运算符,赋值运算符,逻辑运算符,位运算符
###成员运算符,身份运算符,运算符优先级
#算术运算符 + - * 、 % **(幂,返回x的y次幂 x**y) //(返回商的整数部分)
a = 21
b = 10
c = 0
c = a + b
print("1---c的值为:", c)
c = a - b
print("2----c的值为:", c)
c = a * b
print("3---c的值为:", c)
c = a / b
print("4----c的值为:", c)
c = a % b
print("5----c的值为:", c)

#修改变量a、b、c
a = 2
b = 3
c = a * b
print("6----c的值为:", c)

a = 10
b = 5
c = a//b
print("7----c的值为:", c)

#python 比较运算符 == 等于 != 不等于 >大于 <小于 》= 大于等于
# <=小于等于
a = 21
b = 10
c = 0
if (a == b):
    print("1---a等于b")
else:
    print("1---a不等于b")

if (a != b):
    print("2---a不等于b")
else:
    print("2---a等于b")

if (a < b):
    print("3---a小于b")
else:
    print("3---a大于等于b")

if (a > b):
    print("4---a大于b")
else:
    print("4---a小于等于b")

#修改a和b的值
a = 5
b = 20
if (a <= b):
    print("5---a小于等于b")
else:
    print("5---a大于b")

if (b >= a):
    print("6---b大于等于a")
else:
    print("6---b小于a")


#赋值运算符 = 简单赋值运算符 += 加法赋值运算符 -=减法赋值运算符 *= 乘法赋值运算符
# /=除法赋值运算符 %= 取模赋值运算符 **= 幂赋值运算符 //= 取整赋值运算符
a = 21
b = 10
c = 0
c = a + b
print("1---c的值为:", c)
c += a
print("2----c的值为:", c)
c *= a
print("3---c的值为:", c)
c /= a
print("4----c的值为:", c)
c = 2
c %= a
print("5----c的值为:", c)
c **= a
print("6----c的值为:", c)
c //= a
print("7----c的值为:", c)


#位运算符:把位作为二进制来运算 &与 |或 ^异或 ~取反 << 左移 >>右移
a = 0x00111100
b = 0x00001101
print("a&b=",a&b)
print("a|b=",a|b)
print("a^b=",a^b)
print("~a=",~a)

#结果:
a&b= 4352
a|b= 1118465
a^b= 1114113
~a= -1118465

#逻辑运算符 and 与 or 或 not 非
a = 10
b = 20

if(a and b):
    print("1---变量a和b都为true")
else:
    print("1---变量a和b有一个不为true")

if (a or b):
    print("2---变量a和b有一个为true")
else:
    print("2---变量a和b都不为true")

#修改变量a的值
a = 0
if (a and b):
    print("3---变量a和b都为true")
else:
    print("3---变量a和b有一个不为true")

if (a or b):
    print("4---变量a和b有一个为true")
else:
    print("4---变量a和b都不为true")


if not(a and b):
    print("5---变量a和b有一个为false")
else:
    print("5---变量a和b都为true")

#结果如下
1---变量a和b都为true
2---变量a和b有一个为true
3---变量a和b有一个不为true
4---变量a和b有一个为true
5---变量a和b有一个为false



#成员运算符:测试实例中包含了一些列的成员,包括字符串,列表或元组
#in 若在指定的序列中找到值则返回True,否则返回False
#not in 若在指定的序列中没找到值则返回True,否则返回False
a = 10
b = 20
list = [1, 2, 3, 4, 5];

if (a in list):
    print("1 - 变量 a 在给定的列表中 list 中")
else:
    print("1 - 变量 a 不在给定的列表中 list 中")

if (b not in list):
    print("2 - 变量 b 不在给定的列表中 list 中")
else:
    print("2 - 变量 b 在给定的列表中 list 中")

# 修改变量 a 的值
a = 2
if (a in list):
    print("3 - 变量 a 在给定的列表中 list 中")
else:
    print("3 - 变量 a 不在给定的列表中 list 中")

    #输出结果
    1 - 变量a不在给定的列表中list中
    2 - 变量b不在给定的列表中list中
    3 - 变量a在给定的列表中list中


#身份运算符:比较两个对象的存储单元
#is 判断两个标识符是不是引用自一个对象
#is not判断两个标识符是不是引用自不同对象
#id()用于获取对象内存地址
#is 与 ==区别:is用于判断两个变量引用对象是否为同一个, ==用于判断引用变量的值是否相等
a = 20
b = 20

if (a is b):
    print("1 - a 和 b 有相同的标识")
else:
    print("1 - a 和 b 没有相同的标识")

if (a is not b):
    print("2 - a 和 b 没有相同的标识")
else:
    print("2 - a 和 b 有相同的标识")

# 修改变量 b 的值
b = 30
if (a is b):
    print("3 - a 和 b 有相同的标识")
else:
    print("3 - a 和 b 没有相同的标识")

if (a is not b):
    print("4 - a 和 b 没有相同的标识")
else:
    print("4 - a 和 b 有相同的标识")

#结果输出
1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识


#运算符优先级:从高到低优先级所有运算符
#** 指数 最高优先级
# ~+- 按位翻转 一元加号减号(最后两个方法名为+@和-@)
# */%// 乘,除,取模和取整除
# +- <加法减法
# >><<右移左移
# & 位AND
# ^| 位运算符
# <= <> >= 比较运算符
#<> == != 等于运算符
#= %= /= //= -= *= **= 赋值运算符
#is is not 身份运算符
#in not in 成员运算符
# not or and 逻辑运算符

a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d  # ( 30 * 15 ) / 5
print("(a + b) * c / d 运算结果为:", e)

e = ((a + b) * c) / d  # (30 * 15 ) / 5
print("((a + b) * c) / d 运算结果为:", e)

e = (a + b) * (c / d);  # (30) * (15/5)
print("(a + b) * (c / d) 运算结果为:", e)

e = a + (b * c) / d;  # 20 + (150/5)
print("a + (b * c) / d 运算结果为:", e)

#结果如下
(a + b) * c / d 运算结果为: 90.0
((a + b) * c) / d 运算结果为: 90.0
(a + b) * (c / d) 运算结果为: 90.0
a + (b * c) / d 运算结果为: 50.0


a = 2.0
b = 2.0

if (a is b):
    print("a 和 b 相同")
else:
    print("a 和 b 不同") #交互模式下 会给a 和 b分配不同的内存,因此会得出a和b不同的结论

if (a == b):
    print("a 和 b 相同")
else:
    print("a 和 b 不同")
>>> a = 2.0
>>> b = 2.0
>>> a is b
False
>>> a = 2.0; b = 2.0
>>> a is b
True
>>>

'''


参考网址:

http://www.runoob.com/python/python-chinese-encoding.html

Windows下:编译环境下载地址:

https://www.python.org/downloads/release/python-363/

IDE下载地址:

https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows&code=PCC

PATH路径设置:

我的电脑->属性->高级系统设置->高级->环境变量->系统变量:

Path:编辑新建python.exe所在目录路径。



用Pycharm IDE编辑省事。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值