1.注释
单行注释
# 单行注释
多行注释
"""
多行注释
"""
2.变量类型
# 基本变量类型
a = 1 # integer
b = 1.5 # float
c = 'string' # String
d = "string" # string
e = False # boolean
# list\tuple\dictionary后续单独深入学习
3.input交互
# input交互
message_name = input("what's your name? \n")
message_birth = input("When are you born? \n")
message_age = 2023-int(message_birth)
message_country = input("Where are you from? \n")
print('客户档案:\n客户姓名:'+message_name+'\n客户年龄:'+str(message_age)+'\n国家:'+message_country)
input显示括号内的字符串在调试窗口,字符串之后等待user输入,以enter结束输入;运行上述代码后显示的结果:
what's your name?
张三
When are you born?
1999
Where are you from?
中国湖北武汉
客户档案:
客户姓名:张三
客户年龄:24
国家:中国湖北武汉
Process finished with exit code 0
4.string的相关使用方法
# string相关用法
s = "abcdefg"
print(s[1]+"\t"+s[-1]+"\t"+s[:-1]+"\t"+s[-3:]+'\n') # 读取字符串 s = 'abcdefg' 0123456/-7-6-5-4-3-2-1
s = "123"+s # 字符串拼接 s = '123abcdefg'
print(s+str(len(s))+'\n') # 字符串长度 s = '123abcdefg'
print(str(s.find('ab'))+"\n") # 字符串寻位 s = '123abcdefg'
print(s.replace('ab', 'xy')+'\n') # 字符串替代 s = '123xycdefg'
运行这段代码得到的结果为:
b g abcdef efg
123abcdefg10
3
123xycdefg
Process finished with exit code 0
first = 'John'
last = 'Smith'
msg = f'{first} [{last}] is a coder' # string格式化定义
msg_len = len(msg)
msg1 = msg.upper() # string全部大写
msg2 = msg.lower() # string全部小写
msg3 = msg.title() # string首字母大写
这段代码运行结果:
5.Pycharm使用技巧
ctrl+shift+F10 = RUN
ctrl+c = 复制光标所在那一行,无需选中
ctrl+shift+↑= 上移一行
ctrl+shift+↓= 下移一行