首页 > 技术文章 > python_learn1

kanite 2019-03-12 09:05 原文

  1、python在命令行获取当前的路径

    import os

    os.getcwd()

    os.chdir(r"C:\Users\szlon\Desktop\pythonexer")

    os模块介绍

  2、在Python中\是转义符,\u表示其后是UNICODE编码,因此\User在这里会报错,在字符串前面加个r表示就可以了。

    Note:python中的字符串定义不是单引号吗?

  3、解答上面的Note问题,Python中的单引号、双引号和三引号

  4、python安装过程及怎么在windows命令行运行python文件

    python安装过程不叙述,记得勾选将python路径放入Windows运行环境。

     

    上面用到了两个Windows命令,CD转换运行路径。DIR显示当前路径下的所有文件。

  5、在python IDLE运行python文件

    import 文件名即可。

  6、教程中最开始的$符号是Linux命令行默认开始符号。

  7、获取List的长度。

     内置len函数。

     S1 = [1,2,3]

     S2 = [0,[1,2,3]]

        len(S1) = 3

        len(S2) = 2

     也就是list可以当任意类型当做一个元素来对待,即使它本身也是个list类型。

       元组合list都是序列。

  8、获取命令行输入

    直接input就好,是系统内置函数,不需要import。

      例:

1 >>> mac_addr = input('请输入MAC地址:')
2 请输入MAC地址:10:23:45:67:89:10
3 >>> print(mac_addr)
4 10:23:45:67:89:10
View Code

   9、练习一早上

  1 Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
  2 Type "help", "copyright", "credits" or "license()" for more information.
  3 >>> print('Hello World!')
  4 Hello World!
  5 >>> $python hello.py
  6 SyntaxError: invalid syntax
  7 >>> import os
  8 >>> os.getcwd()
  9 'C:\\Users\\szlon\\AppData\\Local\\Programs\\Python\\Python37'
 10 >>> os.chdir("C:\Users\szlon\Desktop\pythonexer")
 11 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
 12 >>> os.chdir('C:\Users\szlon\Desktop\pythonexer')
 13 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
 14 >>> os.chdir(r"C:\Users\szlon\Desktop\pythonexer")
 15 >>> os.getcwd
 16 <built-in function getcwd>
 17 >>> os.getcwd()
 18 'C:\\Users\\szlon\\Desktop\\pythonexer'
 19 >>> hello.py
 20 Traceback (most recent call last):
 21   File "<pyshell#9>", line 1, in <module>
 22     hello.py
 23 NameError: name 'hello' is not defined
 24 >>> import hello
 25 Hello World!
 26 >>> a = 10
 27 >>> print(a)
 28 10
 29 >>> print(type(a))
 30 <class 'int'>
 31 >>> a = 1.3
 32 >>> print(a,type(a))
 33 1.3 <class 'float'>
 34 >>> a = True
 35 >>> print(a,type(a))
 36 True <class 'bool'>
 37 >>> a = 'Hello!'
 38 >>> print(a,type(a))
 39 Hello! <class 'str'>
 40 >>> s1 = (2,1.3,'love',5.6,9,12,False)
 41 >>> print(s1,type(s1))
 42 (2, 1.3, 'love', 5.6, 9, 12, False) <class 'tuple'>
 43 >>> s2 = [true,5,'simle']
 44 Traceback (most recent call last):
 45   File "<pyshell#22>", line 1, in <module>
 46     s2 = [true,5,'simle']
 47 NameError: name 'true' is not defined
 48 >>> s2 = [True,5,'simle']
 49 >>> print(s2,type(s2))
 50 [True, 5, 'simle'] <class 'list'>
 51 >>> s3 = [1,[3,4,5]]
 52 >>> print(s3,type(s3))
 53 [1, [3, 4, 5]] <class 'list'>
 54 >>> print(s3.count)
 55 <built-in method count of list object at 0x0000021356CD5AC8>
 56 >>> print(s3.count())
 57 Traceback (most recent call last):
 58   File "<pyshell#28>", line 1, in <module>
 59     print(s3.count())
 60 TypeError: count() takes exactly one argument (0 given)
 61 >>> print(len(s3)
 62      )
 63 2
 64 >>> print(len(s3))
 65 2
 66 >>> print(s1[0])
 67 2
 68 >>> print(s2[2])
 69 simle
 70 >>> print(s3[1][2])
 71 5
 72 >>> mac_addr = input('请输入MAC地址:')
 73 请输入MAC地址:10:23:45:67:89:10
 74 >>> print(mac_addr)
 75 10:23:45:67:89:10
 76 >>> print 1+9
 77 SyntaxError: Missing parentheses in call to 'print'. Did you mean print(1+9)?
 78 >>> print (1+9)
 79 10
 80 >>> print (1.3 - 4)
 81 -2.7
 82 >>> print (3*5)
 83 15
 84 >>> print (4.5/1.5)
 85 3.0
 86 >>> print(3**2)
 87 9
 88 >>> print(10%3)
 89 1
 90 >>> print( 5 == 6)
 91 False
 92 >>> print (8.0 != 8.0)
 93 False
 94 >>> print( 3 < 3, 3 <= 3)
 95 False True
 96 >>> print(4 > 5, 4 >= 0)
 97 False True
 98 >>> print(5 in [1,3,5])
 99 True
100 >>> print(True and True, True and False)
101 True False
102 >>> print(True or False)
103 True
104 >>> print(not True)
105 False
106 >>> print( 5==6 or 3 >=3)
107 True
108 >>> 
View Code

   10、input输入的是字符串,怎么输入整数或者基本类型的数

   a = int(input('请输入一个整数'))

  11、一个求绝对值的小模块

1 a = int(input('请输入一个数:'))
2 
3 if a >= 0:
4     a = a
5 else:
6     a = -a
7 
8 print('a的绝对值为:',a)
View Code

  12、1-100求和

1 sum = 0;
2 
3 for i in range(101):
4     sum += i
5 
6 print(sum)
View Code

 

推荐阅读