Day1

1、计算机基础

CPU:相当于人的大脑 运算与控制中心

内存:暂时存储 供给cpu数据  成本高 断电即消失

硬盘:相当于你的电脑数据库 存储着大量数据 文件 成本低 永久保存

操作系统:执行者 支配所有关系

2、python的历史

  python崇尚优美、清晰、简单,是一个优秀并广泛使用的语言

  python2x,3x的区别

    python2x:源码混乱 复杂代码较多 冗余

    python3x:源码规范 崇尚优美清晰 简单

3、开发的分类

  解释性:当程序运行时,将代码逐行解释成二进制,在运行

    优点:排错快 开发效率高 可以跨平台

    缺点:执行效率相对低

    典型:python

  编译型:将代码一次性全部编译成二进制,然后再运行

    优点:执行效率高

    缺点:排错慢 开发效率低 不能跨平台

4、运行第一个python程序

  python 文件路径 回车

  区别:python3x  print(‘内容’)

      python2x print()或者print‘内容’

      python3x 编码:utf-8

      python2x 默认编码:ascii

        解决方法:在首行 # -*- encoding:utf-8 -*-

5、变量

  1、变量必须由数字 字母 下划线 任意组合

  2、变量不能以数字开头

  3、变量不能是python中的关键字

  ['and', 'as', 'assert', 'break', 'class', 

  'continue', 'def', 'del', 'elif', 'else',
  'except', 'exec', 'finally', 'for', 'from',
  'global', 'if', 'import', 'in', 'is', 'lambda',
  'not', 'or', 'pass', 'print', 'raise', 'return',
  'try', 'while', 'with', 'yield']

  4、变量要具有可描述性

  5、变量不能是中文

  6、变量不能太长

  7、官方推荐

  #驼峰体

  AgeOfOldboy = 56

  NumberOfStudents = 100

  #下划线

  age_of_oldboy = 56

  number_of_students = 80

  w__ = 22 T
  e_a_ = '太白' T
  e$ = 'fdsa' F
  -_ = '15' F
  2g = 33 F数字不能开头

  __ = '老男孩' T

6、常量

  不变的量:生日 身份证号

  python规定没有,默认全部大写的变量称为常量

  BIRTH=19970425

7、注释

  为什么有注释?

  帮助你记得之前的代码 或者帮忙别人看懂你的代码

  单行注释:#

  多行注释:'''被注释内容'''     “”“”“”被注释内容"""

8、基础数据类型初始

  数字类型 int

  用于计算

  字符串类型

  在python中被引号引起来的数据就是字符串

  msg=“My name is alex,I'm 22 years old!”

  name='''太白'''

  msg = '''

  今天我想写首小诗,
  歌颂我的同桌,
  你看他那乌黑的短发,
  好像一只炸毛鸡。
  '''

  type()判断此数据是什么数据类型

  字符串+字符串代表拼接

  字符串*数字 代表N个字符串

9、用户交互 input

  input 字数串数据类型

  python2x:raw_input()

  python3x:input()

  input数据类型全部是字符串类型

  

name=input('请输入你的名字')
age=input('请输入你的年龄')
hobby=input('请输入你的爱好')
stu='我的名字是'+name+'我的年龄是'+age+'我的爱好是'+hobby
print(stu)
View Code

10、if语句

第一种结构:

if 条件:

  结果

第二种结构:

if 条件:

  结果

else:

  结果

第三种结构:

choice =input('请输入你的猜的数字')
if choice=='1'print('我请你吃一天饭')
elif choice=='2':
    print('我请你吃一周饭')
elif choice=='3':
    print('我请你吃一个月饭')
View Code

第四种结构:

choice =input('请输入你的猜的数字')
if choice=='1'print('我请你吃一天饭')
elif choice=='2':
    print('我请你吃一周饭')
elif choice=='3':
    print('我请你吃一个月饭')
elseprint('不好意思 你猜错了')
View Code

第五种结构:

if 条件:

  if 条件:

    结果

  else:

    结果

else:

  结果

int --->str       str(12)

ste ---->int  '123'  '123q'  全部由数字组成的字符串才能转换橙数字   int(‘123’)

11、while语句

while 条件:

  结果

跳出循环的条件:

  1、改变条件

  2、break

break:结束循环

continue:结束本次循环 继续下一次循环

  

while True:
    print('happy')
    print('山有木兮')
    print('彩虹天堂')
    print('带你去旅行')
View Code

#标志位 flag

  

flag=True
while flag:
    print('happy')
    print('山有木兮')
    print('彩虹天堂')
    print('带你去旅行')
    flag = False
View Code
count =1
while count<101:
    print(count)
    count=count+1
View Code
count=1
while True:
    print(count)
    count=count+1
    if count==101:
        break
View Code
count=0
while count<10:
    count=count+1
    if count==7:
        continue
    print(count)
View Code
原文地址:https://www.cnblogs.com/a352735549/p/8584607.html