您的位置:首页 > 编程语言 > Python开发

Python - Day9 - 初识&变量&条件语句&基本数据类型&循环

2018-03-19 17:37 666 查看
前言
1.编程语言:
-低级语言
-高级语言
2.Python种类:
-JavaPython
-cPython   *****
-pypy
-字节码 和 机器码

一、基础
1.第一句python
-后缀名可以是任意

-导入模块时,如果不是.py文件时会出错
-Python 文件名   .py  

2.Python 执行方式:
- python 解释器 py文件路径
C:\python35\python.exe D:\1.py
-输入python 进入解释器 实时输入并获取到执行结果
C:\python35\python.exeprint ("Hello World!")

3.文件形:
# !/user/bin/env python 
(Linus系统需要) : 声明所使用的python解释器



Windows & Linux :



4.编码:
-*- coding:utf8 -*- 
python2 :编码为ASCII (出现中文,程序必须在头部)
python3:不需要
utf8:可识别中文,可解释,可执行

ASCII码:8位,最多256个       00000000    &  00000001
万国码(UNI Code ):至少16位  0000000000000000+  & 0000000000000001 
utf-8 :能用多少表示 就用多少表示 00000001  

补充:
字节,位
unicode  utf8 gbk
utf8:3
gbk:2

5.执行一个操作: 
用户登录程序:提醒用户输入用户名和密码
获取用户名和密码,检测:用户名=root 密码=root
正确:登录成功
失败:登录失败

a. input 永远等待,直到用户输入一个值,就会将输入的值赋值给一个东西:input ('请输入用户名:') 将输入的值赋值给n,n指代用户输入的内容:n1=input ('请输入用户名:')
n2=input ('请输入密码:')

print (n1,n2)

单行注释: #
多行注释: """    """  

inp = input ('>>>')
>>> hello
inp="hello"

>>>10
inp="10"

#固若将字符串转换成数字
new_inp=int(inp)
inp*10=???

b.变量,代指某一个变化的值n1="alex"
n2="root"
print(n1)
print(n2)
#C语言,底层将变量名去掉
print("alex")
print("root")

6.变量名
-字母
-数字(不能开头)
-下划线
-不能使用python关键字
-最好不要和python内置方法重复(pycharm写程序可以避免)
n111_bb12
u8)_asdf (X)
_998Ufasd
9ouf_asdf (X) 不能用数字开头
class  (X) python关键字不能使用 
sum()(python 内置的求和方法)

写照:
a12_b1="Love has no borders"

补充:变量名尽量有意义,一看变量名就知道代指什么单词连接用: _   (user_id)
userId 用于java等语言

7.条件语句
代码块(缩进为4个空格,Tab键代指4个空格,一个代码块中缩进一致)
1).if 条件基本语句:if 条件:
内部代码块 #if条件成立,执行此代码块
内部代码块
else:
... #else条件成立,执行此代码块
print('...')Ex:
if 条件:
print('OK')
else:
print('Error')
if 条件{
print('OK')
}else{
print('Error')


2).if 支持嵌套if 1==1:
if 2==2:
print('SHINee’s back')
print('SHINee’s back')
else:
print('SHINee World')
else:
print('SHINee is best')

3).if--elif--elseinp=input('Who is the leader:')
if inp=='Onew':
print('True')
elif inp==‘JongHyun’:
print('JH is not the leader')
elif inp=='Key':
print('Key is not the leader')
else:
print('MH and TM are not the leader')

print('End')

4).补充
pass关键字if 1==1:
pass
else:
print('Go')

8.基本数据类型
1).字符串
- 字符串(引号):引号内为字符(只有以下情况,不可混搭)
name="test"
name='test'
name="""test"""
name='''test'''
#pass关键字
if 1==1:
pass
else:
print('Go')
n1='SHINee'
n2='Is'
n3='Five'
n4=n1+n2+n3
n4=SHINeeIsFive
- 字符串的乘法:
n1='SHINee'
n2=n1*10

2).数字
age=24

Eg:
a1=10
a2=20
- 加法:a3=a1+a2

- 减法:a3=
- 乘法:a3=a1*a2
- 除法:a3=a2/a1
-a3=4**4 
-a3=39%8   #获取39除以8得到的余数(判断奇偶)a=13
tamp=a%2
if tamp=0:
print('偶数')
else:
print('奇数')
补充:
a3=39//8   #取商 
即:a3=4

9.循环
1).死循环import time
while 1==1:
print('True',time.time())2).while循环(先执行判断条件,再执行代码块)import time
count=0
while count<10:
print('True',time.time())
count=100
print(123)

import time
count=0
while count<10:
print(count,time.time())
count=count+1
print(123)


补充:
a).while--elsecount=0
while count<10:
print(count)
count=count+1
else:
print('else') #只执行一次
print('..........')b).continue-break (遇到continue后,当前循环终止)



i).while  --- continue (终止当前循环,开始下一次循环)count=0
while count<10:
count = count + 1
print(count)
continue
print('111111111')
print('..........')


ii).while-- break (终止整体循环)count=0
while count<10:
count = count + 1
print(count)
break
print('111111111')
print('..........')


练习题:1.使用while循环输入 1 2 3 4 5 6    8 9 10count=1
while count<11:
if count==7:
print(' ')
else:
print(count)
count=count+1

2.求 1-100的所有数的和count=1
s=0
while count<101:
s=s+count
count=count+1
print(s)3.输出 1-100内所有的奇数count=1
while count<101:
if count % 2== 0:
pass
else:
print(count)
count=count+14.输出1-100内所有的偶数count=1
while count<101:
if count % 2== 0:
print(coun
92ea
t)
else:
pass
count=count+15.求1-2+3-4+5...99的所有数的和count=1
s=0
while count<100:
if count%2== 0:
#print(s)
s=s-count
else:
#print(s)
s=s+count
count=count+1
print(s)6.用户登录(三次机会重试)count=0
while count<3:
user_name=input('请输入用户名:')
password=input('请输入密码:')
if user_name=='SHINee'and password=='080525':
print('Congradulations!')
break
else:
count=count+1
if count==3:
print('Sorry,you can not try it anymore')
break
else:
print('Sorry,please check your user_name and password')




参考文档:
 Python运算符
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐