python基础

hello world!

1 print("hello world!")
View Code

后缀名

python文件后缀名为.py,导入模块时,如果文件后缀名不是.py,解释器会报错

执行方式

注意 : 在linux下解释器路径#!/usr/bin/env python

  (1)  cmd直接调用系统程序 > .> > python解释器  py文件路径

  (2)  cmd调用系统程序,输入python进入解释器:  实时输入并获取到执行结果

编码

 (1)  python2版本默认使用ascill编译,如果输入文字,用ascill编码输出会乱码,需要在前面声明编码格式:  # - * - coding:utf-8 - * -

 (2)  python3默认使用UTF-8 ,  无需注意

input用法

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

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

  正确:  登陆成功

  错误:  登陆失败

  a.input的用法, 永远等待, 直到用户输入了值,  就会将输入的值赋值给一个东西

1 user_name1 = input("请输入用户名")
2 user_password1 = input("请输入密码")
3 if user_name1 == "root"  and  user_password == "root":
4                 print("登陆成功")
5 else:
6                 print("登陆失败")
input

变量名

 (1) 组成结构:  由字母,数字,下划线的任意组合组成

PS: (1)  不能以数字开头

  (2)  不能是关键字

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

 (2)推荐使用驼峰体和下划线(AaBbCc  a_b_c)

条件语句

(1)  if...else...语句, 缩进用四个空格,表示if条件下的字代码,  缩进用于区分代码块

  if与else条件为并列关系,非if即else

  pass用法为占位符,没有意义

 1 n1 = input('>>>')
 2 
 3 if  n1 == "tom":
 4         n2 == input(">>>")
 5         if n2 == "确认":
 6                 print("welcome")
 7         else:
 8                 print("sorry")
 9 
10 else:
11         print("error")
if...else...

  注意: n1 = "tom","=" 为赋值符号

  n2 = ="确认" , "==" 为比较符号,多用于判断

(2) if...elif...elif...else,意思为非if则elif或elif,如果不符合则else

1 if  条件1:
2     pass
3 elif  条件2:
4     pass
5 elif  条件3:
6     pass
7 else:
8     pass
if...elif...elif...else

(3)"and" 和 "or"  用法

  and表示和的关系,即前面条件跟后面条件同时成立

  or表示或的意思,即前面条件跟后面条件只成立一个即可

  a and b    a为非零则取b,a为零则取a

  a or b      a为非零则为a,a为零则b

  not    非正即负

数据基本类型

(1)  字符串  n1 = 'asdc'  n2 = 'fghj'

(2)  数字  age=21  fight = 5

加减乘除法

1.字符串可以做加法乘法, 但不能做减法

1 n1 = "home"
2 n2 = "ad"
3 n3 = "rt"
4 n3 = n1 + n2 + n3
View Code
1 n1 = "tom"
2 n2 = n1 * 10
View Code

2.数字可以做加减乘除等运算,%表示取余数

1 11  12  13 ...
2 num = 12
3 n = num % 2
4 if n == 0:
5     print("偶数")
6 else:
7     print("奇数")
View Code

循环(while语句)

while 表示死循环,即如果while结果为真,则一直循环执行下去

1 while 1==1:
2         print("ok")
while循环

 

 

练习题

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

1 count = 0
2 while count <= 9:
3         if count == 7:
4                 pass
5         else:
6                 print(count)
7         count += 1
View Code

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

1 count = 1
2 n1 = 0   #每一次加在加值
3 while count <= 100:
4 
5         n1 = n1 + count
6         count += 1
7 
8 
9 print(n1)
View Code

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

1 count = 1
2 while count <= 100:
3         if count%2 == 1:
4                 print(count)
5         else:
6                 pass
7         count += 1
View Code

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

1 count = 0
2 while count <= 100:
3         if count%2 == 0:
4 
5                 print(count)
6         else:
7 
8                 pass
9         count += 1
View Code

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

1 count = 1
2 s = 0
3 while count < 100:
4         if count%2 == 1:
5                 s = s+count
6         else:
7                 s = s-count
8         count += 1
9 print(s)
View Code
 1 count = 1
 2 n1 = 0
 3 n2 = 0
 4 n3 = 0
 5 while  count <= 99:
 6         if count % 2 == 1:
 7                 n1 = n1 + count
 8         else:
 9                 n2 = n2 + count
10         
11         count += 1
12 n3 = n1-n2
13 print(n3)
View Code

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

 1 user_name = "tom"
 2 user_password = "123"
 3 count = 1
 4 while count <= 3:
 5         user_name1 = input("请输入用户名:")
 6         user_password1 = input("请输入密码:")
 7         if user_name1 == user_name and user_password1 ==         
 8                    user_password:    
 9                 print("welcome")
10                 break
11         else:
12                 pass
13         count += 1
View Code

 

  

posted @ 2018-07-02 17:24  微凉fjc  阅读(141)  评论(0编辑  收藏  举报