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)
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:
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运算符
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运算符
相关文章推荐
- Python【基础:数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set】注意事项(与java,c比较)
- Python教程学习简记1--Python数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set
- Python 中的数据类型 & print 语句 & 注释&变量&字符串转义&布尔
- Python基本数据类型及循环语句
- Python教程学习简记1--Python数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set
- Python宝典第三章:Python数据类型与基本语句
- 【C++学习】【阶段一】数据初始化&运算符优先级&条件循环语句
- python:变量与赋值 基本数据类型
- python——基本数据类型+常用操作符+循环
- python基础1之python介绍、安装、变量和字符编码、数据类型、输入输出、数据运算、循环
- Python详解(一)Python的基本语法,简单数据类型,和while,for循环控制,ifesle控制
- Java第一课 Java的一些基本概念,Java的起源、为什么我们要学习Java语言,Java跨平台原理剖析;Java环境变量的配置,初学者常犯的问题,Java语言的基本数据类型和Java的语句。
- Python 基本数据类型及变量的使用
- Python学习第2节(基本数据类型操作和执行流程语句)
- Python笔记——基本语法:标识符、数据类型、变量、运算符及表达式/编码风格
- java基本数据类型&java变量类型
- Python的基本语法,涵盖数据类型、循环判断、列表、map和set等
- A Byte of Python 笔记(2)基本概念:数、字符串、转义符、变量、标识符命名、数据类型、对象
- 第01章 CORE C++_C++ 基本结构_注释_输出语句_输入语句_初始化_条件控制_ASCII码值_转义字符_常量_变量_类型长度_运算符
- Python快速学习02:基本数据类型 & 序列