开班前自学—python基础一 2019.02.23

一.学习期间要求

1.不允许迟到。(每次迟到罚20,扣5分)

2.不允许楼道内吸烟,不乱扔杂物。

3.尊重老师们。(老师包括主教/上课老师。。。)

4.听话

二.初识计算机

三 python的发展

  Python 2x vs Python 3x

  1.3x:源码规范,‘优雅’,‘明确’,‘简单’。2x相反,2020。

  2.3x:默认utf-8编码;2x:默认ascii编码。sacii码无法显示中文。

    2x修改默认编码:

# -*- encoding : utf-8 -*-

四 编译语言的分类

1. a 编译型:一次性将代码编译成机器语言(二进制);执行效率高/开发效率低,不能跨平台。

  代表语言:c c++  ......

  b 解释型:逐行编译,再运行;开发效率高/执行效率低,可以跨平台。

  代表语言:JavaScript  Python PHP  ......

2 动态语言与静态语言

3 强类型定义语言与弱类型定义语言

总结:python是一门动态解释性的强类型定义语言

五 python的优缺点及分类

  优点:  ‘优雅’/‘明确’/简单  开发效率高  可移植性 可扩展性 可嵌入性 

  缺点:速度慢

  分类:CPython  IPython  Jython  IronPython  PyPy 

六 Python解释器的安装及Python代码的运行

  指定解释器(文件头部):#!/usr/bin/evn python

七 注释

#单行注释

‘‘‘
多行注释
多行注释
多行注释
‘‘‘

#多行注释中,单双引号都可。

八 变量与常量

  1 变量规则

    a 只能是字母,数字,下划线组成。

    b 不能是数字开头

    c 不能是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‘]

    d 变量不能太长

    e 变量要具有可描述性

  2 变量的赋值

int1 = 25
str1 = hello

  3 常量

  在Python中,没有专门的语法表示常量,但一般默认大写即表示常量。

# !/usr/bin/env python
# -*- encoding :utf-8 -*-

name =input (‘请输入你的名字‘)
age = input ( ‘请输入你的年龄‘)
city = input (‘请输入所在城市‘)

print (‘你好‘,name)
print (‘今年‘,age,‘岁‘)
print (‘来自美丽的‘,city)

九 数据类型

  • int : 整数 (2x有长整型,3x没有)
    +  -  *  /  %  //   (%:余,//:整除)
  • a = 2**64
    print (a,type(a)) 
  • str  : 字符串
    Python中,引号中的即为字符串
    字符串可以相加(拼接),与int相乘
    msg1 = ‘真的‘
    msg2 = ‘假的啊?‘
    
    print (msg1+msg2)
    
    print (msg1*8)
  • bool : 布尔值 (True False)
    print (3>2)   #没有引号,bool值
    print (3>4)   #没有引号,bool值
    print (3>4,type(3>4)) #输出为False <class ‘bool‘>      
    
    print (True)    #True中T要大写,否则报错
    print (‘True‘)  

    type()可查看数据类型

    #print (True,type(True))      #识别为bool
    #print (‘True‘,type(‘True‘))     #识别为str 

十 格式化输出

不好意思,没搞懂

十一  基本运算符

  1 算数运算  +  -  *  /  %  //  **

  2 比较运算  ==    !=    <>    >    <    >=    <=

  3 赋值运算  =    +=    -=    *=    /=    %=    //=    **=

  4 逻辑运算  and    or    not

    优先级:() > not > and > or

print (3>4 or 4<3 and 1==1)
print (1 < 2 and 3 < 4 or 1>2 )
print (2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  #输出True and优先于or
print (1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
print (1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
print (not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) #False

    x or y:  if x is false,then y;else,x.

    x and y:if x is false,then x;else,y.

    not x:if x is false,then True;else,False.

print (8 or 4)  #8
print (0 and 3) #0
print (0 or 4 and 3 or 7 or 9 and 6) #3     ?????????

  5 成员运算 

print (‘参与‘ in ‘药店里有买人参与艾草‘)
print (‘精通‘ in ‘从入门到放弃‘)

  6 运算优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 ‘AND‘
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not and or 逻辑运算符

十二 if

#单独 if 

if 3>2:
    print (666)

# if else 

question=input(‘你爱***吗?‘)  #字符串不能少引号
if question == ‘爱‘:             #冒号要用小写
    print (‘白头偕老‘)
else:
    print (‘天打雷劈‘)

#if elif 
money = int (input (‘亲爱的,你要给我多大的红包:‘))   #int (input(‘str‘))  可将str转为int
if money == 6 :
    print (‘晚上跪搓衣板‘)
elif money ==199 :
    print (‘拥抱‘)
elif money == 520 :
    print (‘亲亲‘)
elif money == 5200 :
    print (‘爱你‘)

# if elif else 
money = int (input (‘亲爱的,你要给我多大的红包:‘))   #int (input(‘str‘))  可将str转为int
if money == 6 :
    print (‘晚上跪搓衣板‘)
elif money ==199 :
    print (‘拥抱‘)
elif money == 520 :
    print (‘亲亲‘)
elif money == 5200 :
    print (‘爱你‘)
else :                              #除if,elif外的选择
    print (‘......‘)

#嵌套
msg1 = input(‘你吃饭了没有?    ‘)
if msg1 == ‘吃了‘:
    msg2 = input(‘我们今天出去玩好不好?‘)
    if msg2 == ‘好呀‘:
        print (‘那我们走吧‘)
    else     :
        print (‘那好吧‘)
else :
    print(‘那快去吃饭把‘)

十三 while

#while

flag = True
while flag:
      print (‘白日依山尽,黄河入海流‘)
      print (‘窗前明月光,疑似地上霜‘)
      print (‘少装不努力,老大徒伤悲‘)
      flag = False    #Q:此行直接从上上行跳行会报错,删掉前面4空格,再加2个空格
#cmd死循环后,ctrl+c 可以结束

int1 = 1
while int1<=100:
    print (int1)
    int1 += 1 # 同 int1 = int1+1

count = 2
while count <= 100:
    print (count)
    count += 2

count = 0
while count <= 100:
    if count % 2 == 0 :
        print (count)
    count += 1      

count = 1
sum = 0
while count:
    sum = sum + count
    count += 1
    if count ==101:  #此处用==,表示比较;而不是用=,表示赋值。
        break        #break 结束循环;continue 本次循环结束,跳至从循环开头
print (sum)          #print的内容一定要加()
# continue 

count = 0
while count <= 100 :
    if count > 5 and count <95:
        count += 1
        continue
    else :
        print (‘loop‘,count)
        count += 1
print (‘-----out of while loop-----‘)

while else

#while else

count = 0
while count <= 10 :
    print (‘loop‘,count)
    count += 1

else:
    print (‘loop over‘)

练习:

1、使用while循环输入 1 2 3 4 5 6     8 9 10

count = 1
while count <=10:
    if count == 7:
        count +=1
        continue
    else :
        print (count)
        count += 1

count = 1
while count <=10:
    count += 1
    if count == 8:
        continue
    else :
        print (count-1)

2、求1-100的所有数的和

count = 1
sum = 0
while count <= 100 :
    sum = sum + count
    count += 1
print (sum)

3、输出 1-100 内的所有奇数

count = 1
while count <= 100 :
    if count % 2 != 0 :
        print (count)
    count += 1

4、输出 1-100 内的所有偶数

count = 1
while count <= 100 :
    if count % 2 == 0 :    #此处用==
        print (count)
    count += 1

5、求1-2+3-4+5 ... 99的所有数的和

count = 1
sum  = 0
while count <= 99:
    if count % 2 == 0:
        sum = sum - count
    else :
        sum = sum +count
    count += 1
print (sum)

6、用户登陆(三次机会重试)

name = input (‘请输入你的账号‘)
password = input (‘请输入你的密码‘)
if name == ‘刘英鹏‘ and password == ‘liu123‘: #此处用双等号,且后面的str不用括号。老是会忘记!!!
    print (passed)
else:
    print (‘密码输入错误,请重新输入。(还有两次机会)‘)
    password = input (‘请输入密码‘)
    if password == ‘liu123‘:
        print (‘passed‘)
    else:
        print (‘账号或密码输入错误,请重新输入。(还有一次机会)‘)
        password = input (‘请输入密码‘)
        if password == ‘liu123‘:
            print (‘passed‘)
        else:
            print (‘账号或密码输入错误,请重新输入。‘)
            print (‘输入密码错误次数已超过3次,请30分钟后重试‘)

感谢太白老师的的博客和讲课视频!

原文地址:https://www.cnblogs.com/yingpeng/p/10422924.html

时间: 2024-08-27 05:18:25

开班前自学—python基础一 2019.02.23的相关文章

开班前自学—python基础二_基础数据(2019.02.27)

一 基础数据类型 list: [],各种数据类型的数据,大量的数据,便于操作. tuple: 元组.()只读列表. dic: 存储大量的数据,关系型数据. 二 int str bool相互转换 1 int <---> str int --->str int(str) 条件:str必须全部由数字组成. str --->int str(int) age = int (input('how old are you?')) print(age) # 如果输入内容包括非数字,会报错 s1 =

开班前自学--python list

列表:容器型数据类型 1 读取List 1.1按索引 l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] print(l1[0]) # output: front print(l1[2][1]) # output: cloudpark 1.2 按切片 l1 = ['front','ove',['library','cloudpark','CC+'],'2019.03.01','09:06'] print(

python基础之面向对象02

---继承 当我们定义完成某个类时,可以再定义一个新类,新类可以继承第一个类.新类被称为子类,而被继承的类称为父类/基类/超类. 继承可以使子类使用父类中的方法,也可以在子类中新定义某个方法,或者在子类中覆盖父类的方法. 来看一个实例: class animal(object): #定义一个动物类 def running(self): print('running....') class dog(animal): #定义一个狗类,且狗类继承动物类 def wangwang(self): prin

python基础-并发编程02

并发编程 子进程回收的两种方式 join()让主进程等待子进程结束,并回收子进程资源,主进程再结束并回收资源 from multiprocessing import Process import time def task(name): print(f'子进程{name}:starting--') time.sleep(1) print(f'子进程{name}:end--') if __name__ == '__main__': print('进入主进程--') pro_list = [] fo

Python基础-03.数据类型-02.变量和常量

本文主要介绍Python中的变量和常量,包括变量的命名规范,使用注意事项 1.变量 1.1.变量理解 何时需要用到变量?一段程序或者数值需要在后面经常用到就可以用变量变量包括:变量名,变量值对于计算机,变量可以看做是一种存放信息的容器,用于存储在计算机程序中需要引用和操作的信息.对于人,提供一种用描述性的名称对数据进行标记的方法,这样我们写的程序就可以更清楚地被读者和我们自己理解. 概念区分:变量的声明.定义.赋值和初始化有什么区别?变量声明或定义:在环境中指定一个变量的名字.代表在内存中开辟一

Python基础教程—2019/1/29

2019/1/29 // 整除** 乘方0x 十六进制 0b 二进制 0o八进制 常用模块和函数1:pow() 乘方 >> pow(2,3)8>> 2**38>> 2:abs() 绝对值 3:round() 向最接近的那个数取整,如果一样,向偶数取整 >> round(2/3)1>>4:math库的floor()函数,向下取整>> import math>> math.floor(32.9)32>> 5:mat

python基础学习之02 元组

#encoding=utf-8 # 元组与列表一样,也是一种序列 print (1,2,3) print 1,2,3 print(1,) print 1, a=1 print(type(a)) a=1,2,3 print(type(a)) # tuple 函数(元组) 以一个序列作为参数并把它转为元组,如果是元组则原样返回 a=[1,2,3,4,4] b=tuple(a) print(a) print(type(a)) print(b) print(type(b)) c=tuple('abc')

PAT 甲级 A1054 (2019/02/23)

1 #include<cstdio> 2 #include<map> 3 using namespace std; 4 int main(){ 5 int m, n, element; 6 scanf("%d %d", &m, &n); // 行数与列数 7 map<int, int> Count; // 数字与出现次数的map映射 8 for(int i = 0; i < m; i++){ 9 for(int j = 0; j

PAT 甲级 A1016 (2019/02/23)0/25

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1010; int toll[25]; struct Record { char name[25]; int month, dd, hh, mm; bool status; } rec[maxn], temp; bool cmp(Record a, Record b) { int