跳到正文

【Python学习】初学Python1小时的成果总结

1.基础知识 #! /usr/local/bin/python3 # 这是我在过去几家公司招聘到工程师,Python入职培训的过程。时间分为4周,全部自学,仅提供大纲。 # 适用于Web方向: # 1、Week1:读完《简明Python教程》,适应Python开发环境 # 2、Week2:写个爬虫,需要深入了解re、urllib2、sqlite3、threading,Queue等几个模块。需要用上多线程抓取,正则表达式分析,并发资源控制,重新开启程序自动继续抓取和分析 # 3、Week3:学习一种Web开发框架,推荐Flask、webpy之类的,学个数据库接口如sqlite3,写个简单的web应用如博客 # 4、Week4:给产品做个小功能并走完测试和上线流程,各个时期是不同的我在之前的几家公司招聘工程师时,学过Python的其实较少。 # 更常见的情况是人聪明,招来再学Python。就是按照如上流程。这个流程安排的挺轻松的,我找到的所有人都成功完成了这个流程。并且之后工作也很顺利。 print("test") ''' 这是一段多行字符串,这是他的第一行 This is the second line. "What's your name?" I asked!. He said "are" ''' # ---------------------------------格式化方法 format() : 将每个参数值替换至格式所在的位置--------------------------------------------------# age = 20 name = 'Xiuxiu' print('{0} was {1} years old when he wrote this book'.format(name, age)) print('Why is {0} playing with that Python?'.format(name)) # 联立字符串来输出 print(name + ' is ' + str(age) + ' years old ') # 数字是一个可选选项{可省略} print('{} was {} years old when he wrote this book'.format(name, age)) print('Why is {} playing with that Python?'.format(name)) # 浮点数'0.333'保留小数点后三位 print('{0:.3f}'.format(1.0/3)) # 使用下划线填充文本,保持文字hello处于中间位置 print('{0:_^58}'.format('hello')) # 基于关键词输出 print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python')) # 自定义print的结尾输出字符(以空白结尾) print('a', end='') print('b', end=' ') print('c', end='\n hahaha') # ---------------------------------转义序列--------------------------------------------------# print('This is the first line \n This is the second line\t This is the table\'s elements ') # 原始字符串(双引号里面的是什么内容,就会输出什么内容) print(r"New lines are indicated there \n") # 不使用原始字符串 print("New lines are indicated there \\n") 2.流程语句 number = 23 running = True while running: guess = int(input('Enter an integer : ')) if guess == number: print('猜对了') running = False elif guess < number : print('猜错了,再试一次!') else: print('No, 错的太离谱了!') print('Done') print('开始执行for循环') for i in range(1, 5): print(i) else: print('The for loop is over ') for i in range(1, 10, 2): print(i) # break 语句的使用 while True: s = input('Enter something : ') if s == 'quit': # 直接退出循环 print('Length of the string is', len(s)) break else: print('The string you enter is error!') print('Done finished!') # continue的使用测试 while True: d = input('Enter something : ') if s == 'quit': break if len(s) < 3: print('Too small') continue print('Input is of sufficient length')3.函数的使用 # 函数的使用测试 def say_hello(): print('hello say!') # 调用函数 say_hello() say_hello() def print_max(a, b): if a > b: print(a, 'is maximum') elif a == b: print(a, 'is equal to', b) else: print(b, 'is maximum') # 开始测试大小 print_max(10, 12) x = 2 y = 7 print_max(x, y) c = input('Please enter the first number:') d = input('Please enter the second number:') print_max(c, d) # 局部变量的使用测试 num1 = 50 def func(num1): print('num1 is', num1) num1 = 2 print('changed local num1 to', num1) func(num1) 4.global的使用 # global 语句的使用测试 num = 50 def function_global(): # 这里要使用的变量是全局变量 global num print('num is', num) num = 15 print('num is', num) function_global() # 默认参数值的使用 def say(message, times=1): # times为连续打印输出的次数 print(message*times) say('Hell0') say('World', 5) # 这里使用了关键字定义函数 def func(a, b=5, c=10): print('a is ', a, 'and b is', b, 'and c is', c) func(3, 7) func(25, c=24) func(c=45, a=100) # 可变参数的使用 # *params : 星号参数,从此处开始到结束所有位置参数都将成为一个元组 # **params: 从开始到结束到会成为一个字典 def total(a=5, *numbers, **phonebook): print('a', a) # 开始遍历元祖中的所有项目 for single_item in numbers: print('single_item', single_item) # 遍历字典中的所有项目 for first_part, second_part in phonebook.items(): print(first_part, second_part) # 开始测试 print(total(10, 1, 2, 3, Jack=1123,john=2231,inge=1560)) # return 的使用测试 def maximum(x, y): if x > y: return x elif x == y: return "the number is equal" else: return y # None 在 Python 中一个特殊的类型,代表着虚无, 用于指示一个变量没有值 print('max is',maximum(10, 12))5.文本字符串的使用 def print_max(x, y): '''Prints the maximum of two numbers.打印两个数值中的最大数。 The two values must be integers.这两个数都应该是整数''' # 如果可能,将其转换至整数类型 x = int(x) y = int(y) if x > y: print(x, 'is maximum') else: print(y, 'is maximum') print_max(3, 5) # 打印输出文档字符串 print(print_max.__doc__)6.sys模块的使用 # 导入 sys 模块 import sys import os #每一个 Python 模块都定义了它的 __name__ 属性。如果它与 __main__ 属性相同则代表这一模块是由用户独立运行的,因此我们便可以采取适当的行动。 # 导入一个模块中的函数 from math import sqrt if (__name__ == '__main__'): print("This program is being run by itself") else: print('I am being imported from another module') print('The command line arguments are:') for i in sys.argv: print(i) print('\n\nTHE PythonPath is ', sys.path, '\n') print(os.getcwd()) print("Square root of 16 is", sqrt(16))

评论

填写昵称与邮箱即可评论,无需登录。

推荐阅读