Getting Started with Python 1

A few little things to remember:

  • Use '#' for comments, and use triple quotation marks '''****************''' for multiple lines.
  • The input of input() in Python is of type str by default, and type conversion is required when necessary.
  • There are usually two ways to format output

#! /usr/bin/env python
# Author: JackyHou

name = input("name: ")
age = int(input("age: "))
job = input("job: ")
salary = int(input("salary: "))

info1 = '''
--------info1 of {name}-------
Name: {name}
Age: {age}
Job: {job}
Salary: {salary}
'''.format(name = name, age = age, job = job, salary = salary)

print(info1)

info2 = '''
-----info2 of %s-----
Name: %s
Age: %d
Job: %s
Salary: %d
'''%(name, name, age, job, salary)

print(info2)
name: Jacky
age: 22
job: student
salary: 10000

--------info1 of Jacky-------
Name: Jacky
Age: 22
Job: student
Salary: 10000


-----info2 of Jacky-----
Name: Jacky
Age: 22
Job: student
Salary: 10000
  • Grammars such as while and if no longer use parentheses, and add colons directly behind them.
  • && =》 and,|| =》 or 
  • Int type cannot use ++
  • else if =》elif
#! /usr/bin/env python
# Author: JackyHou

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:
        continue_confirm = input("Do you want to keep guessing?")
        if continue_confirm != 'n':
            count = 0
  • syntax of for loop
for i in range(10):
    if i < 5:
        print("loop ", i)
    else:
        continue
    print("hehhe <<<<")

continue is used to jump out of the current loop (do not execute the subsequent statements of the current loop, directly enter the next loop)
  • Get the parameters when executing the script
#! /usr/bin/env python
# Author: JackyHou

import sys

print(sys.argv)

ringggrAir: day1 apple $ python3 ./model.py xixi haha
['./model.py', 'xixi', 'haha']

  • About Decode and Encode

msg = "I love Yiyi"

print(msg.encode())

print(msg.encode().decode())
b'\xe6\x88\x91\xe7\x88\xb1\xe4\xbe\x9d\xe4\xbe\x9d'
i love yiyi
That is to say, a str is converted into a string of binary by encode, and a string of binary is converted into a str by decode.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735467&siteId=291194637