Long Way To Go 之 Python 1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Long Way To Go 之 Python 1相关的知识,希望对你有一定的参考价值。

 

Python是啥?

 

      动态解释性的强类型定义语言。(看球不懂,慢慢理解...以下)

      编程语言又有些撒子类型:  编译型、解释型

                                        静态语言、动态语言

                                        强类型定义语言、弱类型定义语言

  

 

编译型:(C/C++)

        Complie:  有一个负责翻译的程序来对源代码进行转换,生成相对应的执行代码

        Complier: 负责编译的程序

        一次性把code转换成机器语言,然后写成可执行文件

 

解释型:(Java,Python)

        如果说编译时一名书本翻译,解释更像一名同声传译

        不断解释,不断执行... ...

 

动态类型语言:(Python Ruby)

        运行期间才检查数据类型

 

静态类型语言:(C/C++,Java)

        数据类型在编译时检查

 

强类型定义语言:

        强制数据类型定义的语言

        如没有强制转换,永远是同个数据类型

 

弱类型定义语言:

        数据类型可以忽略的语言

       一个变量可以赋予不同数据类型的值

 

 

小程序之 Hello World! 

name = "你好,世界"    # Python 3 可以写中文,但是不推荐
print(name)

 

小程序之 用户交互 (Interaction)

技术分享
# user input
name = input("name:")
age = input("age:")
job = input("job:")
salary = input("salary:")

# 4 种格式化输出

# Practice 1  格式化输出    forget it
info = ‘‘‘
-------- info of ‘‘‘ + name +‘‘‘ --------
Name: ‘‘‘ + name + ‘‘‘
Age; ‘‘‘ + age + ‘‘‘
Job: ‘‘‘ + job + ‘‘‘
Salary:‘‘‘ + salary + ‘‘‘
‘‘‘
print(info)


# Practice 2  格式化输出
# in shell,  %s = $

info2 = ‘‘‘
------- info of %s --------
Name: %s
Age; %s
Job: %s
Salary: %s
‘‘‘ % (name,name,age,job,salary)
print(info)

# %s  s represents string
# %d  d only works for numbers     Using int() to do transform for numerical variables
# %f   f is floating numbers



# Practice 3 格式化输出     usually use this method

info3 = ‘‘‘
-------- info of {_name} --------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
‘‘‘.format(_name=name,
           _age=age,
          _job=job,
         _salary=salary)
print(info3)



info4 = ‘‘‘
-------- info of {0} --------
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
‘‘‘.format(name, age, job, salary)

print(info4)
View Code

 

getpass: 输入密码时,如想不可见,可用它

               但python自带IDE(Integrated Development Environment)终端不支持隐藏回显

               用windows下的cmd就OK~

import getpass

_username = momo
_password = abc123
username = input("username:")
password = getpass.getpass("password:")   #encrypt 
# not working well in Pycharm but works in python

 

 

小程序之 猜年龄

流程: 1.定义一个年龄

        2.用户去猜测

        3.判断是否正确

---make only one guess---

if else:

技术分享
age_of_oldboy = 56


# can only make one guess

guess_age =int(input("guess age:"))    # input 默认是string, need to use int()

if guess_age == age_of_oldboy:
    print("yes, you got it!")
elif guess_age > age_of_oldboy:
    print("think smaller!")
else:
    print("think bigger!")
View Code

 

---make only 3 guesses---

while:

技术分享
age_of_oldboy = 56

count = 0
while count < 3:    # make only 3 guesses
    guess_age =int(input("guess age:"))
    if guess_age == age_of_oldboy:
       print("yes, you got it!")
       break
    elif guess_age > age_of_oldboy:
       print("think smaller!")
    else:
       print("think bigger!")
    count += 1
else:
    print("you have tried too many times...fuck off")
View Code

while True:

技术分享
age_of_oldboy = 56

count = 0
while True:
    if count == 3:   # only make 3 guesses
       print("you have tried too many times...fuck off")
       break
    guess_age =int(input("guess age:"))
    if guess_age == age_of_oldboy:
        print("yes, you got it!")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller!")
    else:
        print("think bigger!")
    count += 1
View Code

for:

技术分享
age_of_oldboy = 56

for i in range(3):  # make only 3 guesses
    guess_age =int(input("guess age:"))
    if guess_age == age_of_oldboy:
       print("yes, you got it!")
       break
    elif guess_age > age_of_oldboy:
       print("think smaller!")
    else:
       print("think bigger!")
else:
    print("you have tried too many times...fuck off")
View Code

 

---keep guessing---

技术分享
age_of_oldboy = 56

count = 0
while count < 3:  
    guess_age =int(input("guess age:"))
    if guess_age == age_of_oldboy:
       print("yes, you got it!")
       break
    elif guess_age > age_of_oldboy:
       print("think smaller!")
    else:
       print("think bigger!")
    count += 1
    if count == 3:
        countine_confirm = input("do you wanna keep guessing?")
        if countine_confirm != n:
            count = 0
View Code

 

 

★★★★★添补小知识★★★★★

1.comments

     两种方法:# comments          

                   ‘‘‘comments‘‘‘/"""comments"""

     PS: ‘‘‘ comments‘‘‘可以print多行;

             但是 ‘ 和 " 只能print单行

2.解释器

# !/usr/bin/ python

 告诉操作系统执行这个脚本时,调用/usr/bin下的python解释器

# !/usr/bin/env python

防止python没装在默认的/usr/bin 路径里,会到env设置里查找python的安装路径,再调用对应的路径下的解释器程序

 

3.编码申明

#-*- coding uft-8 -*-

可变长的编码集

 

4.Define variables

# Good try
gf_of_oldboy = "Chen rong hua"
GFOfOldboy = "Chen rong hua"        # called hump hhhhh 23333
 __________name = "Chen rong hua"

# non sense
2name = ""
a
b

# Define constants(in Capital)  eg. PIE

Important!!!

name = "Momo"
name2 = name

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

name = "PaoChe Ge"
print(name,name2)  # name2 does not change

技术分享

 

5. int(), str()  

# convert string into integer
age = int(input("age:"))

# convert integer into string  
print(str(age))

 

6. while, for 循环

例子:

# while True
count = 0
while True:
    print("count:",count)
    count = count + 1      #  same as   count += 1
    if count == 1000:
        break

# for
for i in range(10):   # 0-10,0 gap
    print(-----------,i)
    for j in range(10):
        print(j)
        if j > 5:
            break

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

      

 

以上是关于Long Way To Go 之 Python 1的主要内容,如果未能解决你的问题,请参考以下文章

Long Way To Go 之 Python 5

Long Way To Go 之 Python 5

Find A Way To Go Ahead

Go实战--实现一个自己的网络请求日志httplogger(The way to go)

Go语言学习之os包中文件相关的操作(The way to go)

My way to Python - Day05 - 面向对象-思维导图