python 01篇

一、Pycharm 使用小tips

1.1 pycharm创建项目时,选择Python环境,不使用默认的虚拟环境

1.2 如何在pycharm中查看python版本

  路径File-Settings-Project Interpreter

 1.3 修改pycharm中的字体

  路径:File-Settings-Editor--Front

1.4 配置pycharm可使用Ctrl+鼠标滚轮切换字体大小

  路径:File-Settings-General 勾选Change front size (Zoom) with Ctrl+Mouse Wheel

1.5 pycharm配置git

路径:VCS-Get from Version Control ,输入git仓库地址

 

 1.6 pycharm快捷键

 编辑类:

  Ctrl + D             复制选定的区域或行
  Ctrl + Y           删除选定的行
  Ctrl + Alt + L     代码格式化
  Ctrl + Alt + O     优化导入(去掉用不到的包导入)
  Ctrl + 鼠标        简介/进入代码定义    
  Ctrl + /           行注释 、取消注释

  Ctrl + 左方括号   快速跳到代码开头
  Ctrl + 右方括号   快速跳到代码末尾
  Shift + F10        运行
Shift + F9         调试

查找/替换类:

Ctrl + F          当前文件查找
Ctrl + R          当前文件替换
Ctrl + Shift + F  全局查找
Ctrl + Shift + R  全局替换 

运行类:

Shift + F10        运行
Shift + F9         调试
Alt + Shift + F10  运行模式配置
Alt + Shift + F9   调试模式配置

调试类:

F8                单步调试(一行一行走)
F7                进入内部
Shift + F8        退出
Ctrl + F8         在当前行加上断点/断点开关
Ctrl + Shift + F8 查看所有断点

导航类:

Ctrl + N          快速查找类(也可查找当前工程中的文件,以文件名查找)
Double Shift      任意位置查找

二、Python知识点

2.1 定义变量

name = "小黑"  # 字符串 str
age = 18  # 整数类型 int
score = 97.5  # 浮点型 float

words = " let'go "
words2 = ' tom very "shy" ' #
words3 = ''' let'go,tom very "shy" '''
print(words)
print(words2)
print(words3)

# bool   布尔类型 True 真   False  假
l = [1, 2, 3, 4]
print(1 > 2)
print(4 in l)
print(1 not in l)
print(len(l) > 3)

2.2 条件判断

'''
判断条件为真时,执行该部分代码逻辑
使用 if或if else或if elif else或 嵌套if else等
判断中用的比较运算,> < >= <= != ==
'''
score = input('请输入你的分数:')  # input接受到的全都是str
score = int(score)
# print('score的类型',type(score)) #查看类型函数type(val)
if score >= 90:
    print('优秀')
elif score < 90 and  score >= 75:
    print('良好')
elif score >= 60 and score < 75:
    print('及格了')
else:
    print('不及格')

2.3 循环

循环包括for和while,一般迭代和遍历会用到。

break:立即结束循环,不管你剩余多少次没有循环
continue:结束本次循环,继续进行下一次循环

循环对应的else:
  ①循环正常结束循环后,会执行else里面的代码
  ②else不是必须写的

while循环必须指定结束条件,否则会死循环

count = 0 # 计数器
rate = 140 # 心率
while count < 10:
    if rate > 160:
        break  #break   循环里遇到break循环立即结束
    
    print('跑圈',count)
    count = count + 1
    rate += 5  # rate = rate + 5
count = 0 # 计数器
while count < 10:
    print('循环', count)
    count = count + 1
else:
    print('当循环%d次时,执行else语句' % count)

 for 循环:不需要计数器,也不会死循环

rate = 130
for count in range(8):
    print('跑圈', count)
    if rate > 160:
        print('你的心跳太快了,强制休息')
        break
    rate += 5
else:  # 只要for循环不是break提前结束的,都会执行else
    print('for对应的else')

循环练习

#写一个小游戏
#猜数字有戏
# 1.最多输入7次,如果猜的不对,提示它大了还是小了,猜对了游戏结束,如果次数用尽了还没有猜对,提示已经达到次数

import random
number = random.randint(1, 100) #生成一个1~100内的随机整数
for i in range(7):
    guess = int(input('输入您猜的数据:'))
    if guess == number:
        print('恭喜你,猜对了')
        break
    elif guess > number:
        print('猜的大了')
        continue
    else:
        print('猜的小了')
        continue
else:
    print('您的次数用尽了,请下次再参与游戏')

 2.4 字符串格式化

方式1:直接 +     不推荐使用

方式2:占位符  %s   %d   %f    

  %s  占位str  %d  占位int  %f  占位float  其中%.2f  代表保留2位小数

方式3:{}占位      适用于参数比较多的时候,可以对应

import datetime
name = 'marry'
welcome = 'marry,welcome'
today = datetime.datetime.today()
#字符串格式化:
#1.简单粗暴直接 +
print(name + ',欢迎登陆。今天的日期是'+ str(today))  #不推荐
#2.占位符,%s  %d  %f     %s  占位str  %d  占位int  %f  占位float  其中%.2f  代表保留2位小数
welcome = '%s,欢迎登陆' % name
welcome1 = '%s,欢迎登陆,今天的日期是 %s' % (name,today) #有多个占位符时,要加()
print(welcome)
age = 18
age_str = '你的年龄是%d' % age  #这里也可以用%f,不过会有小数
score = 85.789
info = '你的年龄是%d,你的成绩是%.2f' % (age, score)
print(age_str)
print(info)

#3.大括号的方式    适用于参数比较多的时候,可以对应
welcome2 = '{name},欢迎登陆.今天的日期是{today}'.format(today=today, name=name)  #如果{}中加了名字,可以使用名字对应,不应按照顺序传参
welcome3 = '{},欢迎登陆.今天的日期是{}'.format(name, today)
welcome4 = '{},欢迎登陆.今天的日期是{}'.format(today, name)  #如果{}中没有加名字,就是%s一样的效果,必须按照顺序传参
print(welcome2)
print(welcome3)
print(welcome4)

 2.5 列表   list

#数组/列表  list
student_new = ['张三', '李四', 'wangwu']  #定义一个列表
stu_list = []  # 定义一个空列表

# 取值   索引/下标/角标,从list中取出来   list_name[index]
print(student_new[-1]) #取最后一个元素
print(student_new[0])   #取第一个元素
print(student_new[1])

#往list中增元素
student_new.append('张明')  #在列表的末尾添加元素
student_new.insert(0, 'yangyang')   #在列表的指定位置添加元素

#修改   找到指定元素后修改
student_new[2] = '黄丽'
#删除
student_new.pop(3)  #传入指定的下标
student_new.remove('yangyang')  #使用remove方法可以传元素的值
del student_new[0]
student_new.clear()   #清空列表   此方法无返回值

#其他方法
hy_count = student_new.count('张三')   #统计这个list里面某个元素出现的次数(有返回值),常用于判断元素是否存在
print(hy_count)
hy_index = student_new.index('张三') #找到list中某个元素的下标,如果同一元素有多个,返回第一次出现时的索引
print('hy_index=',hy_index)
users = ['张三', '李珊']
student_new.extend(users)  #把一个列表里面的元素加入另一个列表里面
print(student_new)

numbers = [1, 25, 3, 95, 102, 4]
numbers.sort()    #默认升序排序
print(numbers)
numbers.sort(reverse=True)   # 降序排序
print(numbers)
#多维数组   数组套数组
student_info = [
    [1, 'lhy', '北京'],
    [2, 'hzy', 'shanghai'],
    [3, 'lhh', '天津']
]
#二维数组
student_info2 = [
    [1, 'lhy', '北京', ['bmw', 'benz', 'aodi']],
    [2, 'hzy', 'shanghai'],
    [3, 'lhh', '天津']
]
#一层层去找要操作的数据即可
student_info[0][2] = '山东'
student_info2[0][-1] .append('tesla')

print(student_info)
print(student_info2)
原文地址:https://www.cnblogs.com/lhy-qingqiu/p/13472993.html