3 基础语法

1.变量与变量的作用

>>> eat = 10+15+7+4+7+3
>>> cloth = 20
>>> traffic = 6+6+6+6+6
>>> 精神=300+300+200+400
>>> 
>>> total = eat + cloth + traffic + 精神
>>> print('总消息',total)
总消息 1296

eat,cloth,traffic,精神,total这几个名字的作用,就是把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做“变量”

变量的作用 

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, 

so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.

Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

2.变量定义规范(重点)

  2.1 声明变量

name = "Alex Li"

  2.2 变量定义规则

  1. 变量名只能是 字母、数字或下划线的任意组合
  2. 变量名的第一个字符不能是数字
  3. 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>> name!2 =3
  File "<stdin>", line 1
    name!2 =3
        ^
SyntaxError: invalid syntax
# 语法错误  

>>> name2 = 3
>>> name_2 = 3
>>>
>>> name_-2 = 3
File "<stdin>", line 1
SyntaxError: can't assign to operator
>>>
>>> name_ = 3
>>>
>>> _name_ = 3
>>>
>>> 3_name_ = 3
File "<stdin>", line 1
3_name_ = 3
^
SyntaxError: invalid syntax
>>>
>>> dd_ name_ = 3
File "<stdin>", line 1
dd_ name_ = 3
^
SyntaxError: invalid syntax

 
# 总结: 数字 字母 下划线

  3.3 定义方式

# 驼峰体

AgeOfOldboy = 56 
NumberOfStudents = 80


#下划线

age_of_oldboy = 56 
number_of_students = 80

你觉得哪种更清晰,哪种就是官方推荐的,我想你肯定会先第2种,第一种AgeOfOldboy咋一看以为是AngelaBaby

  3.4  定义变量不好的方式举例

  • 变量名为中文、拼音
  • 变量名过长
  • 变量名词不达意

 

>>> age_of_oldboy = 56
>>> oldboy_gf_name = 'Lisa'

   3.5 变量的修改

>>> print(age_of_oldboy)
56
>>> age_of_oldboy
56
>>> age_of_oldboy = 70
>>> age_of_oldboy
70
>>> print(age_of_oldboy)
70
>>> a = 1
>>> b = a
>>> b
1
>>>
>>> a = 3
>>> b
1

 

3. 常量:全部大写

常量即指不变的量,如pai 3.141592653..., 或在程序运行过程中不会改变的量

举例,假如老男孩老师的年龄会变,那这就是个变量,但在一些情况下,他的年龄不会变了,那就是常量。在Python中没有一个专门的语法代表常量,程序员约定俗成用变量名全部大写代表常量

AGE_OF_OLDBOY = 56

在c语言中有专门的常量定义语法,const int count = 60;一旦定义为常量,更改即会报错




4.用户交互

name = input("what your name?")

print("hello", name)
print('hello2 '+name)

执行脚本就会发现,程序会等待你输入姓名后再往下继续走。

what your name?alex
hello alex
hello2 alex

可以让用户输入多个信息,如下

name = input("What is your name?")
age = input("How old are you?")
hometown = input("Where is your hometown?")

print("Hello ",name , "your are ", age , "years old, you came from",hometown)

执行输出

What is your name?Alex Li
How old are you?22
Where is your hometown?ShanDong
Hello  Alex Li your are  22 years old, you came from ShanDong

5.注释

代码注释原则:

  1. 不用全部加注释,只需要在自己觉得重要或不好理解的部分加注释即可
  2. 注释可以用中文或英文,但绝对不要拼音噢

 




6.什么是数据类型?

我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’的区别的,因此,在每个编程语言里都会有一个叫数据类型的东东,其实就是对常用的各种数据类型进行了明确的划分,你想让计算机进行数值运算,你就传数字给它,你想让他处理文字,就传字符串类型给他。Python中常用的数据类型有多种,今天我们暂只讲3种, 数字、字符串、布尔类型

 

7.数字

  • int(整型)
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647

在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
  • long(长整型)

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

>>> 2**62
4611686018427387904
>>> 
>>> 2**63
9223372036854775808L

注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。

注意:在Python3里不再有long类型了,全都是int

# python2.7
>>> a = 2**62
>>> a
4611686018427387904
>>> type(a)
<type 'int'>

>>> b = 2**63
>>> b
9223372036854775808L
>>> type(b)
<type 'long'>

   

  

8.字符串

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

>>> name = "Alex Li" #双引号
>>> age = "22"       #只要加引号就是字符串
>>> age2 = 22          #int
>>> 
>>> msg = '''My name is Alex, I am 22 years old!'''  #我擦,3个引号也可以
>>> 
>>> hometown = 'ShanDong'   #单引号也可以

那单引号、双引号、多引号有什么区别呢? 让我大声告诉你,单双引号木有任何区别,只有下面这种情况 你需要考虑单双的配合

msg = "My name is Alex , I'm 22 years old!"

多引号什么作用呢?作用就是多行字符串必须用多引号

msg = '''
今天我想写首小诗,
歌颂我的同桌,
你看他那乌黑的短发,
好像一只炸毛鸡。
'''

print(msg)
>>> name = 'alex'
>>> name
'alex'
>>>
>>> name = alex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'alex' is not defined
# 变量,先定义在使用

9.字符串拼接

数字可以进行加减乘除等运算,字符串呢?让我大声告诉你,也能?what ?是的,但只能进行"相加"和"相乘"运算。

>>> name
'Alex Li'
>>> age
'22'
>>> 
>>> name + age  #相加其实就是简单拼接
'Alex Li22'
>>> 
>>> name * 10 #相乘其实就是复制自己多少次,再拼接在一起
'Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li'

注意,字符串的拼接只能是双方都是字符串,不能跟数字或其它类型拼接

>>> type(name),type(age2)
(<type 'str'>, <type 'int'>)
>>> 
>>> name
'Alex Li'
>>> age2
22
>>> name + age2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects #错误提示数字 和 字符 不能拼接

10.布尔型(bool) :逻辑判断

布尔类型很简单,就两个值 ,一个True(真),一个False(假), 主要用记逻辑判断

但其实你们并不明白对么? let me explain, 我现在有2个值 , a=3, b=5 , 我说a>b你说成立么? 我们当然知道不成立,但问题是计算机怎么去描述这成不成立呢?或者说a< b是成立,计算机怎么描述这是成立呢?

没错,答案就是,用布尔类型

>>> a=3
>>> b=5
>>> 
>>> a > b #不成立就是False,即假
False
>>> 
>>> a < b #成立就是True, 即真
True

计算机为什么要描述这种条件呢?因为接下来就可以根据条件结果来干不同的事情啦呀!比如

if a > b 
   print(a is bigger than b )

else 
   print(a is smaller than b )

上面是伪代码,但是不是意味着, 计算机就可以根据判断结果不同,来执行不同的动作啦?

原文地址:https://www.cnblogs.com/venicid/p/8337993.html