Python基础

1.变量名定义

  • 只能由字母、数字、下划线组成
  • 数字不能开头
  • 不能是关键字

关键字:and, as, assert, break, class, continue, def, del, elif, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield

  • 最好不和Python内置的东西重复

 2.Python基础

  • 基础
    C:\Users\Dell>C:\Python\Python.exe 
    print('hello world')

1.后缀名可以是任意,但在导入模块时,如果不是.py文件,可能会出错。。。所以文件后缀名最好用.py的

2.两种执行方式:Python解释器 py文件路径   或者     Python进入解释器-实时输入并获取到执行结果

3.解释器路径

#!/usr/bin/env python

4.编码(告诉Python解释器用什么编码去编)

# -*- coding : utf8 -*-

      Python3无需关注,但Python2仲每个文件只要出现中文,头部必须加上utf8(上面那一串代码)

5.执行一个操作

     提醒用户输入:用户和密码

n = input('请输入用户名')
print(n)

     获取用户名和密码:用户名=root,密码=root

     正确-成功登陆、错误-登录失败

3.基本语句

  • if条件语句

求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)

 

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

n = 1
while n < 4:
 user = input('>>>')
 pwd = input('>>>')
 if user == 'umbrella' and pwd == '123' 
  print('登录成功')
  break
 else
  print('密码或用户名错误')
 n = n+1
print('今日三次机会已用完,请明日再试')

 

 

 

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

在循环中,如果遇到‘continue’则不在继续往下,直接开始下一次循环

在循环中,如果遇到‘break’则终止循环

 

求1-100所有数的和

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

 

  • 奇数偶数

    输出1-100内所有偶数

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

 

posted @ 2021-07-22 15:42  雨伞啊  阅读(103)  评论(0)    收藏  举报