Python历程【入门】

一、开发工具

  • IDE
  • PyCharm
  • Eclipse+pydev

二、输出"Hello World"

1. IDE输出

 Python2

>>> print “Hello World”
Hello World!
View Code

Python3

>>> print("Hello World!")
Hello World!
View Code

PS:print在python2中属于方法,在python3中属于函数

2. python脚本实现输出

Python2

vim hello.py
#!//usr/bin/env python
# -*- conding:utf-8 -*-             #指定字符编码

print “Hello World”                    #打印输出

python ./hello.py                      #运行行脚本
View Code

Python3

vim hello.py
#!//usr/bin/env python

print(Hello World)                          #打印输出

chmod +x /sh/hello.py                  #赋予脚本可执行权限
./hello.py                                      #运行脚本
View Code

PS:执行脚本方式

  1. 进入脚本存放目录执行脚本
  2. 全路径执行脚本
  3. 给脚本赋予执行权限,执行脚本

3. PyCharm实现

此处采用python3实现print输出,python2以后才上图。

PS:python2print作为方法实现输出,而在python3中作为函数实现输出。

三、单引号、双引号及三引号区别

Python中通过单引号(' ')、双引号(" ")、三引号(''' '''或""" """) 来引用字符串

其中三引号可以由多行组成,编写多行文本的快捷语法,常用语文档字符串,在文件的特定地点,被用于注释。

四、注释

  1. # 单行注释
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

# 输出Hello World!                                  # 这里是单行注释
print('Hello World!')
View Code

      2. """ """ 或 ''' ''' 多行注释

'''
多行注释
输出
Hello world!
'''
print('Hello World!')

"""
带颜色的多行注释
输出
Hello world!
"""
print('print 你好嘛?')
View Code

五、编程习惯

1)第一行顶头写。

2) 同级代码须对齐。

3) 非同级代码,行前需空4个空格。其它级代码依次类推。

PS:TAB键实现快速缩进

六、基础语法

1. 变量

  • 变量赋值
name1 = 10
name2 = name1
print("name1的值为:",name1)
print("name2的值为:",name2)

输出结果
name1的值为: 10
name2的值为: 10
View Code
  • id查看内存值
name1 = 10
name2 = name1
print("name1的值为:",id(name1))
print("name2的值为:",id(name2))

输出结果
name1的值为: 1676475168   
name2的值为: 1676475168

PS:通过ID可以查看name1和name2在内存中的值相同
View Code
  • 重新赋值变量
name1 = 10
name2 = name1
name1 = 100                                # name1重新赋值后
print("name1的值为:",name1)
print("name2的值为:",name2)
print("重新赋值后name1的值为:",name1)

输出结果
name1的值为: 100                        # 值已更新
name2的值为: 10                          # 值未变
重新赋值后name1的值为: 100
View Code
  • id查看内存值
name1 = 10
name2 = name1
name1 = 100
print("name1的值为:",id(name1))
print("name2的值为:",id(name2))
print("重新赋值后name1的值为:",id(name1))

输出结果
name1的值为: 1676478048                   # 重新赋值后,内存值发生改变
name2的值为: 1676475168                   # 内存值未发生改变
重新赋值后name1的值为: 1676478048    
View Code

2.命名规则

  1. 变量名可以包括字母、数字(0-9)及下划线( _ )任意组合,首字符须由字母(大小写均可)或下划线(_)开头。
  2. 不能以数字开头。
  3. 变量名中不能有特殊符号。
  4. 关键字不能声明变量名。
  5. 查看python中的关键字
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if','import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
View Code

      6.查看字符是否是关键字

>>> keyword.iskeyword('False')
True
>>> keyword.iskeyword('for')
True
>>> keyword.iskeyword('else')
True
>>> keyword.iskeyword('in')
True
>>> keyword.iskeyword('aaa')             # 不是关键字
False
>>> keyword.iskeyword('ccc')              # 不是关键字
False
View Code

PS:python中,为系统关键字为True,不是系统关键字为False

3.变量写法

file_is_path = "/root/"          # 变量名全小写
FileIsPath = "/root/"             # 变量名首字母大写,驼峰写法
View Code

PS:任选一种写法命名变量。

4.回收变量

>>> a = 1.3
>>> print(a)
1.3
>>> type(a)                         #查看变量类型
<class 'float' at 0x000000006BB926E0>
View Code

七、input用法(用户交互)

  • python2
name = raw_input("请输入你的名字:")
print "name"

输出结果
请输入你的名字:Mr.Chow                      # 等待输入值
Mr.Chow                                              # 输出值
View Code
  •  python3
name = input("请输入你的名字:")
print(name)

输出结果
请输入你的名字:Mr.Chow                      # 等待输入值
Mr.Chow                                              # 输出值
View Code

 PS: python2中采用raw_input接收值;python3中采用input接收值

八、循环语句

  1. if ... else ...

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

age = int(input("请输入你的年龄:"))                            #定义变量,把输入的数值转换为int。默认为字符串

if age < 16:
    print("你目前还未成年。")
else:
    print("恭喜,你已经是成年人啦!")
View Code

    2.  if ... elif ... else ...

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

title = input("请输入你下节课的名称:")

if title == "linux":
    print("你下节课程为:",title)
elif title == "python":
    print("你下节课程为:",title)
else:
    print("对不起,你今天课程已修完,请安心休息。")

输出结果
请输入你下节课的名称:linux
你下节课程为: linux

请输入你下节课的名称:python
你下节课程为: python

请输入你下节课的名称:go                                         # 不存在的课程
对不起,你今天课程已修完。请安心休息。
View Code

    3. while

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

num = 0
while (num < 2):
    print("This is num:",num)
    num += 1
print("Good bye!")

输出结果
This is num: 0
This is num: 1
Good bye!
View Code

PS: 判断条件可以是数值,也可以是布尔值(TrueFalse)

    4. break与continue在while内的用法   

    continue                  # 跳过本次循环
    break                      # 退出循环

  • continue用法演示
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

num = 1
while num < 10:   
    num += 1
    if num%2 > 0:                       # 非双数时跳过输出
        continue
    print("值为:",num)

输出结果
值为: 2
值为: 4
值为: 6
值为: 8
值为: 10
View Code
  • break用法演示
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

num = 1
while 1:                                     # 循环条件为1必定成立
    print("值为: ",num)                # 输出1~10
    num += 1
    if num > 10:                         # 当num大于10时跳出循环
        break

输出结果
值为:  1
值为:  2
值为:  3
值为:  4
值为:  5
值为:  6
值为:  7
值为:  8
值为:  9
值为:  10
View Code

    5. for ... in ...

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auth: Mr.Chow

list = ["Mr.Chow",1,23,'hello',"45"]
for i in list:                                 # 根据元素下标进行判断
    print("值为: ",i)

输出结果
值为:  Mr.Chow
值为:  1
值为:  23
值为:  hello
值为:  45
View Code
原文地址:https://www.cnblogs.com/RobinChow/p/5793085.html