一种面向对象的解释型计算机程序设计语言
具有丰富且强大的内置库和第三方库
语法简洁灵活
开源,跨平台
应用场景
数据分析
人工智能
网络爬虫
自动化运维
Web开发
交互模式
zhangjinyudeMacBook-Pro:~ zhangjinyu$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("hello world")
hello world
>>>
zhangjinyudeMacBook-Pro:~ zhangjinyu$ python helloworld.py
pycharm创建项目工程
(1)创建项目 : pycharm->Create New Project->Pure Python->/Users/zhangjinyu/PycharmProjects/python_basic_v1
(2)创建包 : 项目->右键->New->Python Package->lesson1
(3)创建文件:Lesson1右键->New->Python File->helloworld
删除项目
(1)file->close Project (2)点击项目右上角的X号,删除项目
zhangjinyudeMacBook-Pro:PycharmProjects zhangjinyu$ pwd
/Users/zhangjinyu/PycharmProjects
zhangjinyudeMacBook-Pro:PycharmProjects zhangjinyu$ rm -rf python_basic_v1
注释:
(1)使用原因: 希望通过自己的语言来描述一段代码的实现逻辑和功能,方便理解代码,易于维护
单行注释: # 只注释一行,多行需要多个#
多行注释: ''' 注释内容'''
注释的代码不被执行
变量
格式:变量名=数值
type(变量)查看变量类型
类型转换
int(x) #将x转换为一个整数
float(x) #将x转换到一个浮点数
str(x) #将对象x转换为字符串
eval(str) #用来计算在字符串中的有效python表达式,并返回一个对象
tuple(s) #将序列s转换为一个元组
list(s) #将序列s转换为一个列表
chr(x) #将一个整数转换为一个字符
标识符, 命名规则,关键字
标识符:在python程序开发过程中,自定义的一些符号,名称
由字母,数字,下划线(_)组成,不能以数字开头
标识符区别大小写
命名规则
见名知意,如:name
驼峰命名法,如:类名(UserInfo),异常名(ValueError)等
小写字符+下划线,如变量名(user_name),函数名(get_name)
不能够使用关键字
关键字:
在python内部具有特殊功能的标识符
通过keyword模块的kwlist函数查看
#!/usr/bin/python # -*- coding: utf-8 -*- #学习python第一程序 #实现的功能是:打印helloworld ''' 学习python第一程序 实现的功能是:打印helloworld ''' print("hello world") print("hello python") # print("hello java") # print("hello c")zhangjinyudeMacBook-Pro:lesson1 zhangjinyu$ cat helloworld.py #学习python第一程序 #实现的功能是:打印helloworld ''' 学习python第一程序 实现的功能是:打印helloworld ''' print("hello world") print("hello python") # print("hello java") # print("hello c")llo world")
#!/usr/bin/python # -*- coding: utf-8 -*- ''' 变量 知识点: 1.变量的定义 2.变量赋值 ''' #打印个人信息 name = "zhangsan" #名字 #name2 = 'lisi' high = 180 #身高 # weight = 80 #体重 # age = 20 #年龄 # print(name) # print(high) # print(weight) # print(age) # weight = weight + 5 # print(weight) print(type(name)) print(type(high))
#!/usr/bin/python # -*- coding: utf-8 -*- ''' input print 知识点: 1.通过input获取用户输入内容 2.print直接输出字符串内容 ''' # card_id = input("请输入卡号:") # pwd = input("请输入密码:") # print(card_id) # print(type(card_id)) # print(pwd) #1.print打印字符串 # print("hello python") #2.print打印变量值 # name = "zhangsan" # print(name) #3.print格式化输出 #使用格式化占位符格式化输出 # print("您输入的卡号是:%s"%card_id) # print("您输入的密码是:%s"%pwd) #多个变量同时输出 # card_id = "234567" # pwd = 123 # print("您输入的卡号是:%s,您输入的密码是:%d"%(card_id,pwd)) #格式化输出浮点数,并指定精度 # height = 180.35 # print("您的身高是:%.2f"%height) #格式化输出时,打印%,要使用%%表示是字符串而不是转换说明符 # p = 99.99 # print("您战胜了全国%.2f%%的用户"%p) #print无换行输出 # print("hello",end="") # print("python") #输出换行符 # print("中国\n北京") #转义字符\ # print("中国\\n北京") #format函数 # card_id = "234567" # pwd = 123 # print("您输入的卡号是:{},您输入的密码是:{}".format(card_id,pwd)) # height = 180.35 # print("您的身高是:{:.2f}".format(height)) #类型转换 # name = input("请输入姓名:") # age = input("请输入年龄") # print("name:%s,age:%d"%(name,int(age))) # print(int("123")) # a = int("123") #字符串转整型 # b = float("3.14")#字符串转浮点型 # c = str(345) # print(type(a)) # print(type(b)) # print(type(c)) #eval(str)把字符串自动转换成合适的数据类型 a1= eval("123") a2 = eval("3.14") print(type(a1)) print(type(a2))
#!/usr/bin/python # -*- coding: utf-8 -*- ''' if条件判断 知识点: 1.if...else.. ''' ''' 如果 条件成立: 做某事 否则: 做其他事 if sth == True: do sth else: do other ''' # age_in = input("请输入年龄:") # age_int = int(age_in) # if age_int < 18: # print("温馨提示:") # print("不想未成年人销售烟酒,请买块糖把") # # else: # # print("请付款") # # print("欢迎下次光临") #多条件判断 # age = 12 # # if age < 18: # print("未成年人") # # elif age == 18: # print("成年人") # else: # print("--------") # print("********") #if嵌套 fee = 50 gender = "b" if fee == 50: print("缴费成功") if gender == "b": print("请排在男生队伍") elif gender == "a": print("请排在女生队伍") else: print("请出去") else: print("谢谢")
''' 项目实践:计算器 ''' num1 = input("请输入第一个数字:") operator = input("请输入运算符:") num2 = input("请输入第二个数字:") num_1 = int(num1) num_2 = int(num2) if operator == "+": result = num_1 + num_2 print("计算结果:{}".format(result)) elif operator == "%": result = num_1 % num_2 print("计算结果:{}".format(result)) elif operator == "**": result = num_1 ** num_2 print("计算结果:{}".format(result)) elif operator == "//": result = num_1 // num_2 print("计算结果:{}".format(result)) else: print("正在开发..")
a = 10 # a = a + 5 # print(a) a *= 5 print(a)
""" 项目名称:奶茶馆结算系统 作者:Jiessie 时间:2018-3-27 版本:第一版 """ print('\n欢迎光临奶茶馆!奶茶馆售卖宇宙无敌奶茶,奶茶虽好,可不要贪杯哦!每次限尝鲜一种口味:\n 1)原味冰奶茶 3元 2)香蕉冰奶茶 5元 ' ' 3) 草莓冰奶茶 5元 4)蒟蒻冰奶茶 7元 5)珍珠冰奶茶 7元') milk_tea_no = input('请选择您要购买的奶茶编号:') if int(milk_tea_no) <= 5 and int(milk_tea_no) >= 1: milk_tea_amount = int(input('请输入您要购买的数量:')) if milk_tea_no == "1": price = 3 elif milk_tea_no == "2" or milk_tea_no == "3": price = 5 elif milk_tea_no == "4" or milk_tea_no == "5": price = 7 money = price * milk_tea_amount print('您购买的是{}号奶茶,共购买{}杯,总计{}元'.format(milk_tea_no, milk_tea_amount, money)) if_vip = input('您是奶茶馆的会员吗(y/n)?') if if_vip == 'y': money *= 0.9 print('您可以享受会员价,折后总价:{}元'.format(money)) else: print('Woops!我们只售卖以上五种奶茶哦!新口味敬请期待!') print("\n********************************************************") print('\t奶茶馆力争做一枚有态度、有思想的奶茶馆(傲娇脸)!\n\t祝您今日购物愉快!诚挚欢迎您再次光临!') print("********************************************************")