1.print

 

 

 

print('helloworld')
print("helloworld")

print(5)
print(1+3)

#将数据输出到文件中,注意点:1.所指定的盘符存在,2.使用file= 接收
fp=open('D:/text.txt','a+')
print('helloworld',file=fp)# a+  如果文件不存在就创建,存在就在文件内容的后面继续追加
fp.close()

#不进行换行输出
print('hello','world','python')

 

2.转义字符

 

 

 

#转义字符
print('hello\nworld')
print('hello\tworld')
print('helloooo\tworld') #t 制表符
print('hello\rworkd') #world将hello进行了覆盖
print('hello\bworld') #\b 是退一个格,将o退没了

print('hello:\\\\www.baidu.com')
print('老师说:\'大家好\'')

 

 

 

3.进制

 

 

 

4.关键字

 

 

 

import keyword
print(keyword.kwlist)

['False', 'None', 'True', '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']

 

5.变量的定义和使用

6.数据类型

 

 

 

 

 

 

 

 

 

 

 

 

 

 

print('十进制',118)
print('二进制',0b10101111)
print('八进制',0o176)
print('十六进制',0x1EAF)

 

n1=1.1
n2=2.1
n3=2.2
print(n1+n2)
print(n1+n3)
from decimal import Decimal
print(Decimal('1.1') +Decimal('2.1'))
print(Decimal('1.1') +Decimal('2.2'))

 

 

 

7.数据类型转换

 

 

 

name='张三'
age=20
print(type(name),type(age))

#print('我叫'+name+'今年,'+age+'') 当将str类型与int类型进行连接时,报错,解决方案,类型转换
print('我叫'+name+'今年,'+str(age)+'')

print('-----str()将其他类型转成str类型')
a=10
b=198.8
c=False
print(type(a),type(b),type(c))
print(str(a),str(b),str(c),type(str(a)),type(str(b)),type(str(c)))

print('-----int()将其他类型转成int类型')
s1='128'
f1=98.7
s2='76.77'
ff=True
s3='hello'
print(type(s1),type(f1),type(s2),type(ff),type(s3))
print(int(s1),type(int(s1)))#将str转成int类型,字符串为数字串
print(int(f1),type(int(f1)))#float转成int类型,截取整数部分,舍掉小数部分
#print(int(s2),type(int(s2))) #将str转成int类型,报错,因为字符串为小数串
print(int(ff),type(int(ff)))
#print(int(s3),type(int(s3))) # 将str转成int类型时,字符串必须为数字串(整数),非数字串不允许转换

print('-----float()将其他类型转成float类型')
s1='128.98'
s2='76'
ff=True
s3='hello'
i=99
print(type(s1),type(s2),type(ff),type(s3),type(i))
print(float(s2),type(float(s2)))
print(float(ff),type(float(ff)))
#print(float(s3),type(float(s3))) #报错,字符串的数据如果时非数字串,则不允许转换
print(float(i),type(float(i)))

 

 

 

8.注释

 

 

 

posted on 2021-11-21 10:48  从精通到陌生  阅读(41)  评论(0编辑  收藏  举报