python杂记

1.新建文件有模板的操作

:文件—设置—编译器—文件和代码模块—python script
eg:#开发时间: ${DATE} ${TIME}

2.print

A.输出数字,字符串,含有运算符的表达式,
B.输出到显示器,文件
C.换行、不换行


# 输出数字
print(520)
print(98.5)

# 输出字符串:单引号,双引号
print("acmdyh")
print('acmdyh')

# 表达式
print(3+1)

# 输出到文件 注意点1.所指定的盘符存在2.使用file=fp,此处fp可改变
fp = open('C:/Users/28251/Desktop/输出到文件.txt','a+')   
# a+:文件不存在就创建,存在就在里面追加
print('acmdyh1',file=fp)
fp.close()


# 不换行输出
print('ac','md','yh2')

3.转义字符

  1. \n \t \r \b
  2. \和’的表示
  3. 原意字符
print('acm\ndyh')

# \t是四个位置
# acm dyh       1空格
# aaaacc  dyh   2空格
print('acm\tdyh')
print('aaaacc\tdyh')


# \r回光标,覆盖前面字符
print('acm\rdyh');

# \b退一个格,将m退没了
print('acm\bdyh')


# \\是一个\            \'是’
print('hppt:\\\\www.baidu.com')
print('老师说\'你好\'')


# 原字符,不希望原意字符起作用,加r或R
print(R'acm\ndyh')
print(r'acm\ndyh')

4.二进制与编码

# 汉字编码表 16===4E58   10===20056      2===100111001011000
print(chr(0b100111001011000))
print(ord('乘'))

5.标识符与保留字

# 以下代码可输出保留字到控制台
import keyword
print(keyword.kwlist)


# 'False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', \
# 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', \
# 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 
# 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'

6.变量

name='玛丽亚'
print(name)
print('标识',id(name))
print('类型',type(name))
print('值',name)



# 结果
# 玛丽亚
# 标识 2950203921392
# 类型 <class 'str'>
# 值 玛丽亚

7.数据类型

整型 ,浮点型,bool


#===========================整数=================================
n1=90
n2=-76
n3=0
print(n1,type(n1))
print(n2,type(n2))
print(n3,type(n3))
print('十进制',118)
print('二进制',0b10101111)
print('八进制',0o176)
print('十六进制',0x1EAF)
"""
结果:
90 <class 'int'>
-76 <class 'int'>
0 <class 'int'>
十进制 118
二进制 175
八进制 126
十六进制 7855
"""

#================================浮点数=========================
a=4.14159
print(a,type(a))
n1=1.1
n2=2.2
n3=2.1
print(n1+n2)# 输出3.3000000000000003
print(n1+n3)# 输出3.2
from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))# 输出3.3
"""
4.14159 <class 'float'>
3.3000000000000003
3.2
3.3
"""

#==========================bool=============================
# python 必须大写
print(True+1)
print(False-1)

#=========================字符串================================
# 多引号可换行写
str1='成功的彼此'
str2="成功的彼此"
str3="""成功
的彼此"""
str4='''成功
的彼此'''
print(str1,type(str1))
print(str2,type(str2))
print(str3,type(str3))
print(str4,type(str4))
"""
成功的彼此 <class 'str'>
成功的彼此 <class 'str'>
成功
的彼此 <class 'str'>
成功
的彼此 <class 'str'>
"""

#=============================其他===============================
# 强制转换 str(),int(),flot()
# 中文编码声明 #coding:utf-8

8.输入函数

 present=input('大声?')
 print(present,type(present))

#================================整数运算===========================
 a=int(input('输入a:'))
 b=int(input('输入b:'))

 print(type(a),type(b))
 print(a+b)

9.±*/%


print(1+1)
print(1-1)
print(2*4)
print(1/2)
print(11/2)
print(11//2)#整除运算
print(11%2)
print(2**2)
print(3**3)

"""
2
0
8
0.5
5.5
5
1
4
27
"""

# 向下取整
print(9//4)           # 2.25   2
print(-9//-4)         # 2.25   2
print(9//-4)          # -2.25  -3
print(-9//4)          # -2.25  -3


# 正负公式      余数   =  被除数-除数*商
print(9%-4)    # -3  =  9-(-4)*3
print(-9%4)    #  3  =  -9-4*(-3)

10.赋值运算符


i=2+3
print(i)


# 链式赋值
a=b=c=20
print(a,id(a))
print(b,id(b))
print(c,id(c))


# 解包赋值
a,b,c =10,20,30
print(a,b,c)


# 交换a,b值
a,b=10,20
print(a,b)
a,b=b,a
print(a,b)

11.比较运算符


a,b=10,20
print('a>b',a>b) # 输出False
print('a<b',a<b) # 输出True


# =是赋值
# ==是比较 ,比较数值       is比较标识
a=10
b=10
print(a==b)
print(a is b)

lst1=[11,12,13,14]
lst2=[11,12,13,14]
print(lst1==lst2)
print(lst1 is lst2)
print(id(lst1),id(lst2))
"""
True
True

True
False
2573310693056 2573311025856
"""

12.and /or /not /in

a=1
b=2
print('==============================and========================')
print(a==1 and b==2)  #true and true ->true
print(a!=1 and b==2)  #false and true ->false
print('==============================or========================')
print(a==1 or b==2) # true
print(a!=1 or b==2) # true
print(a!=1 or b!= 2) # false
print('================================not======================')
f1=True
f2=False
print(not f1) # false
print(not f2) # true
print('===========================in  / not in======================')
s='acmdyh'
print('w' not in s) # true
print('a' in s)    # true

13.位运算

print(4 & 8)
print(4 | 8)
print(4 << 1)
print(4 << 2)
print(4 << 3)
print(4 >> 1)
print(4 >> 2)
print(4 >> 3)
"""
0
12
8
16
32
2
1
0
"""

14.结构,循环

# 1
s = 6
if s>60:
    print('及格')
else:
    print('不及格')
print('退出')# 注意缩进

# 2
s = 6
if s>60:
    print('及格')
else:
    print('不及格')
	print('退出')# 注意缩进
#==============================================================
# if else
s = 86
a = 68
if s > 80 and a > 80:
    print('优秀')
else:
    print('不优秀')

s = 86
a = 68
if (80 <= a < 90) or (80 <= s < 90):
    print('良好')
    
#===========================================================
# while
count = 100
while count > 0:
    print('好好学习')
    count -= 1
#=====================================================
# break
count = 100
while count > 0:
    print('好好学习')
    if count == 99:
        break
    count -= 1

# continue
count = 100
while count > 0:
    print('好好学习')
    if count % 2 == 0:
        continue
    count -= 1
#========================================================
# while else
count = 100
while count > 0:
    print('好好学习')
    count -= 1
else:
    print('程序结束')

#===========================================================
# for
name = 'zhangsan'
for i in name:
    print(i)


for i in range(1,20,2):
    print(i,end=' ')
print()

# for(int i=10;i<20;i++)
# for i in range(10,20):

#========================================================

# 随机数
import random
num = random.randint(5,10)
print(num)


15.字符串

st='hello WORLD'
print(st.lower())
print(st.upper())
print(st.swapcase())
print(st.capitalize())
print(st.title())


"""
hello world
HELLO WORLD
HELLO world
Hello world
Hello World
"""

16.函数

def tp(num):
    if num==1:
        return 1
    else :
        return num*tp(num-1)


res=tp(5)
print(res)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯卡码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值