脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python语言快速上手学习方法

Python语言快速上手学习方法

2021-04-29 00:37Engineer-Bruce_Yang Python

今天小编就为大家分享一篇关于Python语言快速上手学习方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

最近在学习Python,后面搞机器人项目需要用到,所以要快速上手,我使用的是PyCharm这个IDE,看起来就舒服,学习起来就有劲啦,作为一名有工作经验的老司机,我学习编程语言的方法不会像大学生那样从头到尾学一遍,我会选择,够用,能用,实用即可,拒绝晦涩的语法,在不影响效率的情况下,我会采取容易看懂,后期项目可维护性等的方式来学习和编程,至于如何灵活运用Python语言,我认为是需要在项目中,才能不断精进的,毕竟,作为一门编程语言,它仅仅只是工具而已。

如果要在python中写中文,则要在xx.py的最前面声明

?
1
#coding:utf-8

一、基础语法:变量,字符串,函数,逻辑判断,循环

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
varline = 2 ;
print(varline);
#打印字符串
print("hello Python");
print("你好,Python");
#整型和字符串的转化
num1 = 100 ;
num2 = "100";
num3 = num1 + int(num2);
print(num3);
#字符串操作
str1 = "hello world" ;
str2 = str1 * 3 ;
string_count = len(str1);
print(string_count);
print(str2);
#字符串索引等价
print(str1[0]); print(str1[-11])  #===>h
print(str1[1]); print(str1[-10])  #===>e
print(str1[2]); print(str1[-9])   #===>l
#可以将字符串进行分割
print(str1[0:5]);print(str1[6:11]); #===> hello   world
print(str1[-4:]);
#函数的定义和使用
def Print():
  print("hello world");
  return "sss" ;
sss = Print();
print(sss);
def add(arg1 , arg2):
  return arg1 + arg2 ;
print(add(1,2));
def getTempatuare(temp):
  return temp *9/5 + 32 ;
print(str(getTempatuare(35)) + "'F");
#克转千克算法
def print_kg(g):
  return float(g / 1000) ;
print(str(print_kg(1)) + "kg");
#求直角三角形斜边的长度
def Line_print(arg1,arg2):
  return ((arg1*arg1 + arg2 * arg2))**0.5
print("The right triangle third side's length is " + str(Line_print(3,4)));
#str_rp = str1.replace(str1[:3],'*'*9);
#print(str_rp)
str11 = "{} a word she can get what she {} for."
str12 = "{preposition} a word she can get what she {verb} for"
str13 = "{0} a word she can get what she {1} for."
str111 = str11.format('With','came');
str121 = str12.format(preposition = 'With',verb = 'came')
str131 = str13.format('With','came')
print(str111)
print(str121)
print(str131)
#单独创建
file1 = open('F:\\'+'hello.txt','w')
file1.write("Hello world");
file1.close()
#使用函数创建
def text_create(name, msg):
  desktop_path = 'F:\\'
  full_path = desktop_path + name + '.txt'
  file = open(full_path,'w')
  file.write(msg)
  file.close()
  print('Done')
text_create('Yang','hello world') # ????
#变量的比较
teststr1 = "Hello"
teststr2 = "World"
teststr3 = "Hello"
print(teststr1 in teststr2)
print(teststr1 is teststr3)
print(bool(teststr1))
print(bool(''))
print(not teststr1)
print(teststr1 < teststr3 and teststr2 > teststr1)
print(teststr1 > teststr2 or teststr3 < teststr1)
#python逻辑判断学习
a = 1
b = 3
if a < b :
  a = 3
  b = 2
else:
  a = 2
  b = 3
print(a,b);
if a < b:
  a = 3
  b = 2
elif a > b:
  a = 2
  b = 3
else:
  a = 100
  b = 200
print(a,b)
for i in 1,2,3,4,5,6:
  print(i)
for string_str in "hello","world","world":
  print(string_str)
for str1111 in "Hello":
  print(str1111)

二、Python数据结构:列表,元组,字典,集合

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#python列表===>
#特点:可以装python的所有类型,包括元组,列表,字典等
city = ['广东','云南','广西','江西','HongKong','Shenzhen',123456]
for i in 0,1,2,3,4,5,6:
  print(city[i])
city.insert(1,'北京') #列表的插入
for i in 0,1,2,3,4,5,6:
  print(city[i])
city.remove('HongKong') #列表的删除
for i in 0,1,2,3,4,5,6:
  print(city[i])
del city[0#使用del方法删除列表中的元素
for i in 0,1,2,3,4,5:
  print(city[i])
#python元组 ===>
#特点:不可修改,可被查看以及索引
num = ('1','2','3','4','5')
for i in 0,1,2,3,4:
  print(num[i])
#python字典 ===>
#特点:键值成对存在,键不可重复,值可重复,键不可改,值可以变,可以为任何对象
Dog = {'name':'sundy','age':18}
Dog.update({'tel':119}) #往字典中添加键值对
print(Dog)
del Dog['name'] #往字典中删除键值对
print(Dog)
#集合
num_set = {1,2,3,4,1,5}
num_set.add(6) #往集合里添加元素
print(num_set)
num_set.discard(3) #从集合里删除元素
print(num_set)

三、Python语言面对对象:类的定义、使用以及类的继承

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#coding:utf-8
#定义一个类
class Anmial:
  var = 100
  Dog = ['runing','eat','sleep'] #Dog是这个类的属性
  def function(self):   #类里的方法
    if Anmial.var == 10:
      print(Anmial.var)
    else:
      print(self+str(Anmial.Dog))
    return Anmial.var
#实例化类
Dog1 = Anmial()
print(Anmial.Dog)
#遍历类中的成员
for i in Anmial.Dog:
  print(i)
#创建实例属性===>类似创建一个与Dog一样的属性
Anmial.log = '会飞','Hello','Monkey'
print(Anmial.log)
Anmial.function("属性:")
class CocaCola():
  formula = ['caffeine','suger','water','soda']
  def __init__(self,local_name): #===>self相当于可以用来访问类中的成员或者创建属性
      self.logo_local = '橙汁'
      if local_name == '可乐':
        print(local_name)
      elif local_name == '橙汁':
        print(local_name)
      else:
        print('西瓜汁')
  def drink(self): #===>调用该方法的时候等效于 coke = CocaCola.drink(coke)
    print('Energy!')
coke = CocaCola('可乐')
coke1 = CocaCola('橙汁')
coke2 = CocaCola('梨汁')
#类的继承===>xuebi相当于CocaCoal的子类,CocaCoal相当于父类
class xuebi(CocaCola):
  formula = ['白色','黄色','绿色']
xuebi = xuebi(CocaCola) #将CocaCola放在括号中,表面xuebi集成于CocalCola
print(xuebi.formula)
xuebi.drink()      #这样子类就可以调用父类的方法,继续延用了

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/morixinguan/article/details/80658142

延伸 · 阅读

精彩推荐