基础语法

1.变量

1.1变量作用

变量是用于存储数据

创新互联专注于网站建设,为客户提供成都做网站、网站建设、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,品牌网站建设,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。

1.2变量定义规范

声明变量

name = "vita"
pep8规范:等号两边要有空格

变量定义规则

  • 变量名只能是字母、数字、下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 程序关键字不能是数字,例如if else while等

变量命名习惯

驼峰体
AgeOfVita = 27
下划线
age_of_vita = 27

变量定义的low方式

变量名为中文、拼音 名字 = "vita"
变量名过长 qeqweqwe_qweqweqw_qwewqeq
变量名词不达意 a = 23

1.3常量

常量定义

常量即指不变的量,如pai 3.1415926 ,或在程序运行过程中不会改变的量。
Python中没有专门的语法定义常量,程序员约定用变量名全部大写代表常量。
python中可自定义一个不修改的常量的类。

AGE_OF_VITA = 27

Java中定义常量,修改该常量会报错

public static final String SUNDAY = "SUNDAY";

c语言中定义常量,修改该常量也会报错

const int count = 60;

变量与常量用法示例

在使用中变量和常量没什么区别

Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> a = 1
>>> b = a
>>> print(a)
1
>>> print(b)
1
>>> a=2
>>> print(a)
2
>>> print(b)
1
>>> 
>>> 
>>> A = 10
>>> B=A
>>> print(A)
10
>>> print(B)
10
>>> A=20
>>> print(A)
20
>>> print(B)
10
>>> 

基础语法

2.读取用户输入

name = input("please input your name:")
age = input("please input your age:")
hometown = input("please input your hometown:")
print("your name is:"+name
      + " your age is:"+age
      + " your hometown is:"+hometown)
运行
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
please input your name:vita
please input your age:27
please input your hometown:jl
your name is:vita your age is:27 your hometown is:jl

3.注释

本行注释

age = input("please input your age:")    # age pep8规范:#号前面至少两个空格,后面一个空格

单行注释

hometown = input("please input your hometown:")
# print age pep8规范:#号前面无空格,后面一个空格

多行注释

"""
print age 
name hometown
"""
'''
print age 
name hometown
pep8规范:注释与下面的代码的空行不能多于两行
'''

print("your name is:"+name
      + " your age is:"+age
      + " your hometown is:"+hometown)

4.数据类型

4.1数字类型

4.1.1介绍

int(整型)

在32位机器上,整数的位数为32位,取值范围为-2**31~2**31 -1,即-2147483648~214748364
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807

long(长整型)

跟C语言不同,Python的长整型没有指定宽度,即python没有限制长整数数值的大小,但实际上由于内存的限制,我们使用的长整数数值不可能无限大。

注意:

自从Python2.2起,如果整数发生溢出,Python会自动把int转换为long,所以现在在long数据后面不加字母L也不会导致严重后果了。
在Python3中没有int和long的区分了,全部都是int

除了int和long外,还有float浮点型,复数型

4.1.2Python2&3之int long

 (venvP3) E:\PythonProject\python-test>python2
Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**4)

>>> type(2**65)

>>> quit()

(venvP3) E:\PythonProject\python-test>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**4)

>>> type(2**65)

>>>

4.2字符串

在Python中,加了引号的字符就被认为是字符串

4.2.1字符串种类

Name = "vita"   # 双引号
Age = "27"  # age是字符串
AgeNum = 27  # age是int
Msg = 'My name is vita,i am 27 years old!'  # 一对单引号
# 一对三个单引号
MsgThree = '''
My name is vita,
i am 27 years old!
'''
# 三个双引号
MsgDouThree = """
My name is vita,
i am 27 years old!
""" 

4.2.2单引号,双引号,多引号的区别

单引号和双引号没有任何区别,只是在下面情况下需要考虑单双引号配合

Msg = "my name is vita,I'm 27 years old"

多引号的作用是多行字符串,需要使用多引号

# 一对三个单引号
MsgThree = '''
My name is vita,
i am 27 years old!
'''
# 三个双引号
MsgDouThree = """
My name is vita,
i am 27 years old!
"""

4.2.3字符串拼接

字符串可以进行“相加”和“相乘”运算

>>> name = "vita"
>>> age = "27"
>>> print("your name is:"+name+" your age is:"+age)
your name is:vita your age is:27
>>> print("v"*3)
vvv

字符串不能喝整数拼接

>>> name = "vita"
>>> age = 27
>>> print(name+age)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: must be str, not int

4.3布尔型

布尔型就两个值,true和false,主要用于逻辑判断

>>> a =3
>>> b = 5
>>> a>b
False
>>> a

4.4格式化输出

name = input("input your name:")
age = input("input your age:")
hometown = input("input your hometown:")
msg = '''
------the info of %s is ------
name:       %s
age:        %s
hometown:   %s
------the info of end   ------
''' % (name, name, age, hometown)
print(msg)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
input your name:vita
input your age:27
input your hometown:jl

------the info of vita is ------
name:       vita
age:        27
hometown:   jl
------the info of end   ------

4.5运算符

运算符种类算数运算符、比较运算符、逻辑运算符、赋值运算符
基础语法
基础语法
基础语法
基础语法


>>> a = 3
>>> b = 4
>>> a>b and a==3
False
>>> a>> a>> a>b or a==3
True

>>> not a==3
False
>>>

4.6流程控制

4.6.1单分支

if 条件成立:
    执行代码
# _*_coding:utf-8_*_
"""
输入姓名,性别和年龄,
如果是女生且年龄小于28,输出我喜欢年轻女孩
"""
name = input("input your name:")
sex = input("input your sex(boy|BOY/girl|GIRL):")
#input()读入的age是字符串,要转换为int才能与28做大小比较
age = int(input("input your age:"))
if sex.lower() == "girl" and age < 28:
    print(name+" i like young girl")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):GIRL
input your age:26
vita i like young girl

4.6.2双分支

if 条件:
    条件满足,执行代码
else:
    条件不满足,执行代码
# _*_coding:utf-8_*_
"""
输入姓名,性别和年龄,
如果是女生
年龄小于28,输出我喜欢年轻女孩,
年龄不小于28,输出年龄比我大也可以
如果不是女生
输出我不喜欢男生
"""
name = input("input your name:")
sex = input("input your sex(boy|BOY/girl|GIRL):")
#input()读入的age是字符串,要转换为int才能与28做大小比较
age = int(input("input your age:"))
if sex.lower() == "girl" :
    if age < 28:
        print(name+" i like young girl")
    else:
        print("the age is greater than me is also ok!")
else:
    print("i do not like boy")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):girl
input your age:12
vita i like young girl

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):girl
input your age:28
the age is greater than me is also ok!

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):boy
input your age:23
i do not like boy

4.6.3多分支

if 条件:
    满足条件执行代码
elif 条件:
    上面条件不满足,执行这里
elif 条件:
    上面条件不满足,执行这里
else:
    所有条件都不满足,执行这里
# _*_coding:utf-8_*_
"""
输入姓名,性别和年龄,
如果是女生
年龄小于28,输出我喜欢年轻女孩,
年龄不小于28,输出年龄比我大也可以
如果不是女生
输出我不喜欢男生
"""
name = input("input your name:")
sex = input("input your sex(boy|BOY/girl|GIRL):")
#input()读入的age是字符串,要转换为int才能与28做大小比较
age = int(input("input your age:"))
if sex.lower() == "girl" :
    if age < 28:
        print(name+" i like young girl")
    else:
        print("the age is greater than me is also ok!")
elif sex.lower() == "boy":
    print("i do not like boy")
else:
    print("your input is not manual human")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):w
input your age:23
your input is not manual human

4.6.4while

while 条件:
    执行代码
"""
只打印1-10的偶数
"""
count = 0
while count < 10:
    if count%2 == 0:
        print(count)
    count = count + 1
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
2
4
6
8
"""
打印1-10,第五次不打印,6-8打印值的平方
"""
count = 0
while count <= 10:
    if count == 5:
        pass
    elif count>=6 and count<=8:
        print(count*count)
    else:
        print(count)
    count = count + 1
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
2
3
4
36
49
64
9
10

4.6.5死循环(dead loop)

只要运行起来,就不再停止,直到把内存耗尽

"""
只打印1-10的偶数
"""
count = 0
while count < 10:
    print(count)
    # count = count + 1
        #注释掉最后一行,程序一直成立,会一直运行

4.6.6循环终止

break:用于完全结束一个循环,跳出循环体执行循环后面的语句
continue:终止本次循环,下次的循环继续

"""
打印1-10,第五次不打印,6-8打印值的平方
"""
count = 0
while count <= 10:
    if count == 5:
        # 这里一定要记得把count+1,否则count就一直等于5,就死循环了
        count = count + 1
        continue
    elif count>=6 and count<=8:
        print(count*count)
    else:
        print(count)
    count = count + 1

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
2
3
4
36
49
64
9
10
"""
打印1-10,第五次不打印,6-8打印值的平方
"""
count = 0
while count <= 10:
    if count == 5:
        # 这里一定要记得把count+1,否则count就一直等于5,就死循环了
        count = count + 1
        break
    elif count>=6 and count<=8:
        print(count*count)
    else:
        print(count)
    count = count + 1

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
2
3
4

4.6.7while...else

只有在Python中有while...else
while后面的else指,当while循环正常执行完,中间没有被break中止的话,就会执行else

count = 0
while count <= 3:
    if count == 2:
        count = count + 1
        continue
    else:
        print(count)
    count = count + 1
else:
    print("success")

        E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
3
success

count = 0
while count <= 3:
    if count == 2:
        count = count + 1
        break
    else:
        print(count)
    count = count + 1
else:
    print("success")

        E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1

本章代码案例:
基础语法

"""
猜年龄游戏:
允许用户最多猜三次,猜了三次后,询问是都继续玩,如果输入Y,可以继续猜三次,否则退出
"""
age = 23
count = 0
while count < 3:
    try:
        guess_age = int(input("input the age of you think:"))
    except ValueError:
        print("you should input one number!")
        count = count + 1
        continue

    if guess_age > 23:
        print("the age you input is too big!")
    elif guess_age < 23:
        print("the age you input is too small!")
    else:
        print("excellent!you are right!")
        break
    count = count + 1
    while count == 3:
        your_choice = input("you only have three chances,would you like to continue(Y|y/N|n):")
        if your_choice.lower() == "y":
            count = 0
        elif your_choice.lower() =="n":
            break
        else:
            print("your input is illegal!input again!")

运行

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/guessage.py
input the age of you think:2
the age you input is too small!
input the age of you think:45
the age you input is too big!
input the age of you think:33
the age you input is too big!
you only have three chances,would you like to continue(Y|y/N|n):y
input the age of you think:2w
you should input one number!
input the age of you think:24
the age you input is too big!
input the age of you think:55
the age you input is too big!
you only have three chances,would you like to continue(Y|y/N|n):y
input the age of you think:23
excellent!you are right!

Process finished with exit code 0

网页题目:基础语法
链接地址:http://myzitong.com/article/giipih.html