第一周-第06章节-Python3.5-第一个python程序

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: HelloWorld.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
print("HelloWorld!!!")
===========================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/HelloWorld.py
HelloWorld!!!

Process finished with exit code 0

第一周-第07章节-Python3.5-变量

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: HelloWorld.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""

name = "chenjisong"
name2 = name
print("My name is",name,name2)
=================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/HelloWorld.py
My name is chenjisong chenjisong

Process finished with exit code 0

解释:name值为chenjisong,name将值赋给name2,所以name2也等于chenjisong ,故结果为:My name is chenjisong chenjisong

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: HelloWorld.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""

name = "chenjisong"
name2 = name
print("My name is",name,name2)
print(id(name))
print(id(name2))
print("=================================")
name = "Paochege"
print("My name is",name,name2)
print(id(name))
print(id(name2))

======================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/HelloWorld.py
My name is chenjisong chenjisong
54678256
54678256
=================================
My name is Paochege chenjisong
54678768
54678256

Process finished with exit code 0

解释:name值为chenjisong,name将值赋予给name2,那么name2值也为chenjisong,后面name的值发生改变,变成了Paochege,内存地址发生了改变,但是name2的内存地址没有变化,所以结果是:My name is Paochege chenjisong

第一周-第08章节-Python3.5-字符编码与二进制(略二进制)

第一周-第09章节-Python3.5-字符编码的区别与介绍

#!/usr/bin/env python 

"""
@author:chenjisong
@file: HelloWorld.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
name = "你好,世界"
print(name)
======================================================================

G:Python2.7.5python.exe G:/practise/oldboy/day1/HelloWorld.py
File "G:/practise/oldboy/day1/HelloWorld.py", line 24
SyntaxError: Non-ASCII character 'xe4' in file G:/practise/oldboy/day1/HelloWorld.py on line 24, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Process finished with exit code 1

原因:python2中因为没有指定字符编码集,所以报错

#!/usr/bin/env python 
#-*- coding:utf-8 _*-

name = ("你好,世界").decode(encoding="utf-8")
print(name)
========================================================================

G:Python2.7.5python.exe G:/practise/oldboy/day1/HelloWorld.py
你好,世界

Process finished with exit code 0

解决方法:导入utf-8字符集(#-*- coding:utf-8 _*-)并解码  decode(encoding="utf-8")

在puthon 3中

#!/usr/bin/env python 

"""
@author:chenjisong
@file: HelloWorld.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""

name = "你好,世界"
print(name)
========================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/HelloWorld.py
你好,世界

Process finished with exit code 0

在python 3中无需指定编码格式也无需解码

第一周-第10章节-Python3.5-用户交互程序

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
username=input("username:")
password=input("Password:")
print("Username is "+username,"and Password is "+password)
===========================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/interaction.py
username:chenjisong
Password:chenjisong
Username is chenjisong and Password is chenjisong

Process finished with exit code 0

解释:红色部分为用户输入的部分。返回的结果调用了输入的变量,形成了交互程序

字符串拼接第一种方法(占位符):

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
Name=input("name:")
Age=input("Age:")
Job=input("Job:")
Salary=input("Salary:")
info='''
----------------info of %s------------------
Name:%s
Age:%s
Job:%s
Salary:%s
''' % (Name,Name,Age,Job,Salary)
print(info)
=========================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/interaction.py
name:chenjisong
Age:23
Job:IT
Salary:3000

----------------info of chenjisong------------------
Name:chenjisong
Age:23
Job:IT
Salary:3000


Process finished with exit code 0

注意:%s代表字符串

            %d代表整数类型

            %f代表浮点数

字符串拼接第二种方法(字符串转换):

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
Name=input("name:")
Age=int(input("Age:"))
Job=input("Job:")
Salary=float(input("Salary:"))
info='''
----------------info of %s------------------
Name:%s
Age:%d
Job:%s
Salary:%f
''' % (Name,Name,Age,Job,Salary)
print(info)
====================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/interaction.py
name:chennjisong
Age:23
Job:IT
Salary:1888

----------------info of chennjisong------------------
Name:chennjisong
Age:23
Job:IT
Salary:1888.000000


Process finished with exit code 0

解释:红色部分为数据类型的强制转换,绿色部分为输入的变量

字符串拼接第三种方法(format):

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
Name=input("name:")
Age=int(input("Age:"))
Job=input("Job:")
Salary=float(input("Salary:"))
info='''
----------------info of {_Name}------------------
Name:{_Name}
Age:{_Age}
Job:{_Job}
Salary:{_Salary}
''' .format(_Name=Name,_Age=Age,_Job=Job,_Salary=Salary)
print(info)
=============================================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/interaction.py
name:chenjisong
Age:23
Job:IT
Salary:289

----------------info of chenjisong------------------
Name:chenjisong
Age:23
Job:IT
Salary:289.0


Process finished with exit code 0

解释:将变量与值形成一一对应的关系

字符串拼接第四种方法(花括号):

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: interaction.py
@time: 2019/04/15
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
Name=input("name:")
Age=int(input("Age:"))
Job=input("Job:")
Salary=float(input("Salary:"))
info='''
----------------info of {0}------------------
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
''' .format(Name,Age,Job,Salary)
print(info)
=============================================================================================

G:Python3.7.3python.exe G:/practise/oldboy/day1/interaction.py
name:chenjisong
Age:28
Job:IT
Salary:2900

----------------info of chenjisong------------------
Name:chenjisong
Age:28
Job:IT
Salary:2900.0


Process finished with exit code 0

将变量换成花括号中的位置参数,并在format后面指明变量

第一周-第11章节-Python3.5-if else流程判断

 

    

  
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!