Python 第一天

1. 两种执行方式

python解释器 py文件路径

python  进入解释器:实时输入并获取到执行结果

2.解释器路径

#!/usr/bin/env python

3.编码

# -*- coding:utf8 -*-
ascill码         8个二进制位表示
unicode        至少16位
                   1字节 = 8位
utf-8           能用多少表示就是用多少表示

Python3 无需关注

Python2 每个文件中只要出现中文,头部必须加 # -*- coding:utf8 -*-

4.变量名

- 字母
- 数字
- 下划线

PS:

数字不能开头

不能是关键字

'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'

最好不要和python内置的东西重复

5.条件语句

 if基本语句

if 条件:
内部代码块
内部代码块
else:
...

print('....')

 if 支持嵌套

age:
if 1 == 1:
                        if 2 == 2:
                            print("欢迎进入第一会所1")
                            print("欢迎进入第一会所2")
                        else:
                            print('欢迎进入东京特')
                    else:
                        print("欢迎进入一本道")

if elif

age:
inp = input('请输入会员级别:')

                    if inp == "高级会员":
                        print('美女')
                    elif inp == "白金会员":
                        print('大摩')
                    elif inp == "铂金会员":
                        print('一线小明星')
                    else:
                        print('城管')
                        
                    print('开始服务把....')
                        
                补充:pass
                    if 1==1:
                        pass
                    else:
                        print('sb')

6. 基本数据类型

字符串   -    n1 = "alex"   n2 = 'root'   n3 = """eric"""  n4='''tony'''
数字     -    age=21        weight = 64   fight = 5 
加减乘除等:
                a3 = a1 + a2
                a3 = a1 - a2
                a3 = a1 * a2
                a3 = 100 / 10
                a3 = 4**4
                a3 = 39 % 8  # 获取39除以8得到的余数
                补充:         
                 a3 = 39 // 8   # 获取39除以8得到的商

7. 循环

死循环
                
                while 1==1:
                    print('ok')
                

8.练习题

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

n = 1
while n < 11:
    if n == 7:
        pass
    else:
        print(n)
    n = n + 1
print('----end----')

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

n = 1
s = 0
while n < 101:
    s = s + n
    n = n + 1
print(s)

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

n = 1
while n < 101:
    temp = n % 2
    if temp == 0:
        pass
    else:
        print(n)
    n = n + 1

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

n = 1
while n < 101:
    temp = n % 2
    if temp != 0:
        pass
    else:
        print(n)
    n = n + 1

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

n = 1
s = 0
while n < 100:
    temp = n % 2
    if temp == 0:
        s = s - n
    else:
        s = s + n
    n = n + 1
print(s)

 6.用户登录(三次机会重试)

count = 0
while count < 3:
    user = input('>>>')
    pwd = input('>>>')
    if user == 'alex' and pwd == '123':
        print('欢迎登录')
        print('......................')
        break
    else:
        print('用户名或者密码错误')
    count = count + 1
原文地址:https://www.cnblogs.com/liujinlei/p/9073211.html