pycharm入门学习引导
本文档源自PyCharm2022.1.1版本
内容为学习PyCharm的IDE功能
目录
pycharm入门学习引导
入门引导
熟悉Pycharm编辑编辑
编辑器基础知识
上下文操作
搜索操作
扩大和缩小代码选取
注释行
复制和删除行
移动代码段
收起
环绕和解开
多选
代码补全
基本补全
选项卡补全
后缀补全
类型匹配补全
F-string补全
重构
重构菜单
重命名
提取变量
提取方法
快速修复重构
就地重构
代码辅助
恢复移除的代码
代码格式
形参信息
快速弹出窗口
编辑器编码辅助
导航
随处搜索
在文件中查找并替换
声名和用法
文件结构
最近的文件和位置
运行并调试试运行配置
调试工作流
GIT
快速入门
项目历史记录
提交
互动式变基
使用Git追溯注解
入门引导
PyCharm 中的主要功能概述(代码中的#为光标初始位置,或提示选中位置)
熟悉Pycharm
def find_average(values):
result = 0
for v in values:
result += v
return result
print("AVERAGE", find_average([5,6, 7, 8]))
编辑器基础知识
使用智能快捷键添加、删除、选择、移动和复制代码。
上下文操作
def method_with_unused_parameter(used, #redundant):
print("It is used parameter: " + str(used))
def intention_example(a, b):
if not (a and b):
return 1
return 2
method_with_unused_parameter("first", "second")
method_with_unused_parameter("used", "unused")
intention_example(True, False)
搜索操作
#print('Hello!' 'n' 'Press Ctrl + Shift + A')
扩大和缩小代码选取
def some_method(first, second, third):
print(first, second, third)
def example_method(condition):
if condition:
print("Begin of the work")
some_method("first string", "This is a long string th#at you can select for refactoring", "third string")
print("End of the work")
print("The end")
注释行
#for i in range(5):
print(i)
primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
复制和删除行
import math
def demo(a, b, c):
return_type_of_sqrt = math.sqrt(b ** 2 - 4 * a * c)
root1 = (-b + return_type_of_sqrt) / (2 * a)
root2 = (-b - return_type_of_sqrt) / (2 * a)#
print(root1, root2)
移动代码段
class Car:
def __init__(self, speed=0):
self.speed = speed
self.odometer = 0
self.time = 0
def say_state(self):
print("I'm going {} kph!".format(self.speed))
def accelerate(self):
#print("I will be vary fast!")
self.speed += 5
收起
import math
import sys
def demo(a, b, c):
n = float(sys.argv[1])
d = b ** 2 - n * a * c
if d > 0:
disc = math.sqrt(d)
root1 = (-b + disc) / (2 * a)
root2 = (-b - disc) / (2 * a)
return root1, root2
elif d == 0:
return -b / (2 * a)
else:
return "This equation has no roots"
class Solver:
class Roots:
pass
pass
class Roots:
pass
if __name__ == '__main__':
solver = Solver()
a = int(input("a: "))
b = int(input("b: "))
c = int(input("c: "))
result = demo(a, b, c)
print(result)
#
环绕和解开
def surround_and_unwrap_demo(debug):
if debug:
print("Surround and Unwrap me!")
多选
Multiple selections
| Lastname | Points | |
|---|---|---|
| Eve | Jackson | 94 |
代码补全
让IDE补全您的代码。尝试基本、智能和其他类型的补全。
基本补全
movies_dict = {
'title': 'Aviator',
'year': '2005',
'demo': False,
'director': 'Martin Scorsese',
'distributor': 'Miramax Films'
}
def director():
return movies_dict[#]
选项卡补全
class Calculator:
def __init__(self):
self.current = 0
self.total = 0
def add(self, amount):
self.current += amount
def get_current(self):
return self.#current
后缀补全
movies_dict = {
'title': 'Aviator',
'year': '2005',
'director': 'Martin Scorsese',
'distributor': 'Miramax Films'
}
movies_dict.get('year')#
类型匹配补全
def f(x, file):
x.append(file)
x.rem#
F-string补全
import sys
class Car:
def __init__(self, speed=0):
self.speed = speed
self.odometer = 0
self.time = 0
def say_state(self):
print("I'm going kph!".format(self.speed))
def accelerate(self):
self.speed += 5
def brake(self):
self.speed -= 5
def step(self):
self.odometer += self.speed
self.time += 1
def average_speed(self):
return self.odometer / self.time
if __name__ == '__main__':
my_car_show_distance = sys.argv[1]
my_car = Car()
print("I'm a car!")
while True:
action = input("What should I do? [A]ccelerate, [B]rake, "
"show [O]dometer, or show average [S]peed?").upper()
if action not in "ABOS" or len(action) != 1:
print("I don't know how to do that")
if my_car_show_distance == "yes":
print("The car has driven # kilometers")
重构
通过重命名、提取和其他类型的重构保持代码整洁。
重构菜单
# Need to think about better sample!
import random
def foo(x):
print(x + random#)
重命名
class Championship:
def __init__(self):
self.teams = 0
def matches_count(self):
return self.teams * (self.teams - 1) / 2
def add_new_team(self):
self.teams += 1
def team_matches(champ):
champ.teams() - 1
class Company:
def __init__(self, t):
self.teams = t
def company_members(company):
map(lambda team : team.name, company.teams)
def teams():
return 16
c = Championship()
c.teams# = teams()
print(c.teams)
提取变量
def bubble_sort(arr):
n = len(arr)
for j in range(n):
for i in range(0, n - j - 1):
if arr[i] > arr[i + 1] : #光标选中i+1
arr[i], arr[i + 1] = arr[i + 1], arr[i]
提取方法
def cocktail_sort(a):
n = len(a)
swapped = True
start = 0
end = n - 1
while swapped:
swapped = False
for i in range(start, end):
if a[i] > a[i + 1]:
a[i], a[i + 1] = a[i + 1], a[i]#选中整行
swapped = True
if not swapped:
break
swapped = False
end = end - 1
for i in range(end - 1, start - 1, -1):
if a[i] > a[i + 1]:
a[i], a[i + 1] = a[i + 1], a[i]
swapped = True
start = start + 1
快速修复重构
def foo(x):
print("Hello ", x)
y = 20
foo(10#)
foo(30)
就地重构
def fibonacci(stop):
first = 0
s# = 1
while s < stop:
print(s)
first, s = s, first + s
n = int(input("n = "))
fibonacci(n)
代码辅助
了解如何设置代码格式、获得形参信息和预览快速弹出窗口。
恢复移除的代码
cat:
name: Pelmen
gender: male
breed: sphinx
fur_type: hairless
fur_pattern: solid
fur_colors: [ white ]
tail_length: long
eyes_colors: [ green ]
favourite_things:
- three plaids
- pile of clothes
- castle of boxes
- toys scattered all over the place
behavior:
#选中以下5行
- play:
condition: boring
actions:
- bring one of the favourite toys to the human
- run all over the house
- eat:
condition: want to eat
actions:
- shout to the whole house
- sharpen claws by the sofa
- wake up a human in the middle of the night
代码格式
import sys
import math
class CodeFormatDemo:
a_const = 10000
b_const = 500
#选中以下两行
def calc_and_print(self):
print(self.a_const * self.b_const)
def calc_many_times(self):
for i in range(100):
self.calc_and_print()
形参信息
class Frame:
width = 0
height = 0
def __init__(self):
pass
def set_size(self, width, height):
self.width = width
self.height = height
frame = Frame()
frame.set_size(#)
快速弹出窗口
def print_three_times(value):
"""
Prints given value three times
"""
for i in range(0, 3):
print(str(value))
print_three_times(123)
print_three_times(1)
print_three_times('Hello!')
编辑器编码辅助
#import random
class Cat:
happiness = math.exp(3)
def say_meow(self):
print("meow")
def eat(self):
self.happiness += random.randint(5, 20)
cat = Cat()
cat.say_meow("meow")
cat.eat()
导航
跳转到源,导航到声明、实现和文件结构。
随处搜索
from quadratic_equations_solver import QuadraticEquationsSolver
print("Enter 3 coefficients of full quadratic equation: ")
a, b, c = list(map(float, input().split()))
if a == 0 or b == 0 or c == 0:
print("Any of coefficients is zero. It is not full quadratic equation.")
else:
solver = QuadraticEquationsSolver()
d = solver.discriminant(a, b, c)
print("Discriminant of this equation is {.3f}".format(d))
print("Solution is:")
solver.solve(a, b, c)
在文件中查找并替换
from warehouse import Warehouse
warehouse = Warehouse()
warehouse.add_fruits('peach', 3)
warehouse.add_fruits('pineapple', 5)
warehouse.add_fruits('mango', 1)
warehouse.add_fruits('apple', 5)
result = warehouse.take_fruit('apple')
if result:
print('This apple was delicious!')
warehouse.print_all_fruits()
声名和用法
from quadratic_equations_solver import QuadraticEquationsSolver
print("Enter 3 coefficients of full quadratic equation: ")
a, b, c = list(map(float, input().split()))
if a == 0 or b == 0 or c == 0:
print("Any of coefficients is zero. It is not full quadratic equation.")
else:
solver = QuadraticEquationsSolver()
d = solver.discr#iminant(a, b, c)
print("Discriminant of this equation is {.3f}".format(d))
print("Solution is:")
solver.solve(a, b, c)
文件结构
quadratic_str = "quadratic"
quad = "quad"
class FileStructureDemo:
def __init__(self):
pass
def hello_world(self):
print("Hello world!")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def welcome(self):
print("JetBrains is aiming to create the best IDEs in the world!")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def print_hippogryph(self):
print("Hippogryph! Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def hospital_information(self):
print("Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def print_home_design(self):
print("Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def print_homo_neanderthalensis(self):
print("Homo Neanderthalensis is a parallel evolution branch of humans.")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def print_homo_sapiens(self):
print("Homo Sapiens is a biological name of modern humans.")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def print_homo_erectus(self):
print("Homo Erectus is most likely the ancestor of modern humans.")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def print_sapphire(self):
print("Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def phone_description(self):
print("Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def foo(self):
print("Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def boo(self):
print("Just another method to illustrate fast file structure search :)")
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
# A lot of code can be inside methods :)
def animal(self):
print("Just another method to illustrate fast file structure search :)")
def parrot(self):
print("Just another method to illustrate fast file structure search :)")
def plain(self):
print("Just another method to illustrate fast file structure search :)")
def air(self):
print("Just another method to illustrate fast file structure search :)")
def aim(self):
print("Just another method to illustrate fast file structure search :)")
def book(self):
print("Just another method to illustrate fast file structure search :)")
def bank(self):
print("Just another method to illustrate fast file structure search :)")
def boring(self):
print("Just another method to illustrate fast file structure search :)")
def car(self):
print("Just another method to illustrate fast file structure search :)")
def cancel(self):
print("Just another method to illustrate fast file structure search :)")
def zoo(self):
print("Just another method to illustrate fast file structure search :)")
def zero(self):
print("Just another method to illustrate fast file structure search :)")
def first(self):
print("Just another method to illustrate fast file structure search :)")
def second(self):
print("Just another method to illustrate fast file structure search :)")
def direction(self):
print("Just another method to illustrate fast file structure search :)")
def director(self):
print("Just another method to illustrate fast file structure search :)")
class AnotherClass:
def __init__(self):
pass
def another_method_1(self):
print("Just another method to illustrate fast file structure search :)")
def another_method_2(self):
print("Just another method to illustrate fast file structure search :)")
def homo_history(self):
print("Just another method to illustrate fast file structure search :)")
最近的文件和位置
numbers = [15, 23, 8, 4, 42, 16] lucky_numbers = #sorted(numbers)
运行并调试
试运行配置
def find_average(value):
check_input(value)
result = 0
for s in value:
result += validate_number(extract_number(remove_quotes(s)))
return result / len(value)
def prepare_values():
return ["'apple 1'", "orange 2", "'tomato 3'"]
def extract_number(s):
return int(s.split()[0])
def check_input(value):
if (value is None) or (len(value) == 0):
raise ValueError(value)
def remove_quotes(s):
if len(s) > 1 and s[0] == "'" and s[-1] == "'":
return s[1:-1]
return s
def validate_number(number):
if number < 0:
raise ValueError(number)
return number
average = find_average(prepare_values())
print("The average is ", average)
调试工作流
def find_average(value):
check_input(value)
result = 0
for s in value:
result += validate_number(extract_number(remove_quotes(s)))#1.在此行设置断点
return result / len(value)
def prepare_values():
return ["'apple 1'", "orange 2", "'tomato 3'"]
def extract_number(s):
return int(s.split()[0])
def check_input(value):
if (value is None) or (len(value) == 0):
raise ValueError(value)
def remove_quotes(s):
if len(s) > 1 and s[0] == "'" and s[-1] == "'":
return s[1:-1]
return s
def validate_number(number):
if number < 0:
raise ValueError(number)
return number
average = find_average(prepare_values())
print("The average is ", average)
GIT
学习如何在IDE中使用Git集成
快速入门
cat:
name: Puss in boots
gender: male
breed: scottish fold
personality_type: outgoingness
fur_type: short haired
fur_pattern: tabby
fur_colors: [ red, white ]
tail_length: long
eyes_colors: [ green ]
eyes_number: 2
ear_number: 2
paws_number: 4
favourite_things:
- boots
- sharp sword
- awesome hat
- black cloak
behavior:
- be_cute:
condition: wants everyone to come to tenderness
actions:
- make pretty eyes
项目历史记录
cat:
name: Pelmen
gender: male
breed: sphinx
personality_type: dominant
# long haired, short haired or hairless
fur_type: hairless
# solid, tabby or multi-color
fur_pattern: solid
fur_colors: [ white ]
tail_length: long
eyes_colors: [ green ]
eyes_number: 2
ear_number: 2
paws_number: 4
favourite_things:
- three plaids
- pile of clothes
- castle of boxes
- toys scattered all over the place
behavior:
- play:
condition: boring
actions:
- bring one of the favourite toys to the human
- run all over the house
- eat:
condition: want to eat
actions:
- shout to the whole house
- sharpen claws by the sofa
- wake up a human in the middle of the night
- sleep:
condition: want to sleep
action:
- bury himself in a human's blanket
- bury himself in a favourite plaid
提交
cat:
name: Puss in boots
gender: male
breed: scottish fold
personality_type: outgoingness
fur_type: short haired
fur_pattern: tabby
fur_colors: [ red, white ]
tail_length: long
eyes_colors: [ green ]
eyes_number: 2
ear_number: 2
paws_number: 4
favourite_things:
- boots
- sharp sword
- awesome hat
- black cloak
behavior:
- be_cute:
condition: wants everyone to come to tenderness
actions:
- make pretty eyes
- play:
condition: boring
actions: [ run after mice or own tail ]
功能分支工作流
cat:
name: Oreshek
gender: male
breed: mongrel
personality_type: skittishness
# long haired, short haired or hairless
fur_type: short haired
# solid, tabby or multi-color
fur_pattern: solid
fur_colors: [ black ]
tail_length: short
eyes_colors: [ yellow ]
eyes_number: 2
ear_number: 2
paws_number: 4
favourite_things:
- bunch of candy wrappers
- tennis ball
- plush mouse
- cardboard box
behavior:
- eat:
condition: want to eat
actions: [quietly ask for food]
- sleep:
condition: want to sleep
actions: [find any place and lie down]
互动式变基
cat:
name: Marsy
gender: female
breed: martian longhair
personality_type: impulsiveness
fur_type: long haired
fur_pattern: solid
fur_colors: [ red ]
tail_length: short
eyes_colors: [ red, blue ]
eyes_number: 2
ears_number: 4
paws_number: 4
favourite_things:
- marsian flowers
- two rovers
- crumpled Tesla roadster
behavior:
- drive:
condition: boring
actions:
- put stones on the pedals of Tesla roadster
- steer standing on hind paws
- communicate:
condition: want to speak with someone
actions:
- turn on one of the rovers
- meow "Hmeowston, are meow hmeowing meo?"
变更列表和搁置
cat:
name: Marsy
gender: female
breed: martian longhair
personality_type: impulsiveness
fur_type: long haired # debug: check another types (short haired, hairless)#标记此行
fur_pattern: solid
fur_colors: [ red ]
tail_length: short
eyes_colors: [ red, blue ]
eyes_number: 2
ears_number: 4
paws_number: 4
favourite_things:
-marsian flowers
- two rovers
- crumpled Tesla roadster
behavior:
- drive:
condition: boring
actions:
- put stones on the pedals
- steer standing on hind paws
- communicate:
condition: want to speak with someone
actions:
- turn on one of the rovers
- meow "Hmeowston, are meow hmeowing meo?"
- eat:
condition: hungry
actions: [ fry self-grown potatoes ]
使用Git追溯注解
cat:
name: Marsy
gender: female
breed: martian longhair
personality_type: impulsiveness
fur_type: long haired
fur_pattern: solid
fur_colors: [ red ]
tail_length: short
eyes_colors: [ red, blue ]
eyes_number: 2
ears_number: 4
paws_number: 4
favourite_things:
-marsian flowers
- two rovers
- crumpled Tesla roadster
behavior:
- drive:
condition: boring
actions:
- put stones on the pedals
- steer standing on hind paws
- communicate:
condition: want to speak with someone
actions:
- turn on one of the rovers
- meow "Hmeowston, are meow hmeowing meo?"



