当前位置: 移动技术网 > IT编程>脚本编程>Python > python学习第一天 -----2019年4月15日

python学习第一天 -----2019年4月15日

2019年04月16日  | 移动技术网IT编程  | 我要评论

霹雳卡盟,平遥天气预报,上海影讯网

第一周-第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.3\python.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.3\python.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.3\python.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.5\python.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.5\python.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.3\python.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.3\python.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.3\python.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.3\python.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.3\python.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.3\python.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流程判断

 



                    

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网