What you need to pay attention to when learning Python

零下一度
Release: 2017-07-21 15:46:18
Original
1306 people have browsed it

Hello World

Use the print() method to print HelloWorld
name = "Jenkin Li"

print("My name is ", name)
Copy after login

Encoding issues in Python 2.x

Because Python 2.x uses ASCII encoding and does not support Chinese by default. You must declare in the file header what encoding the file uses.
# -- coding:utf-8 --

Python comments

are divided into single-line comments and multi-line comments
# Single-line comments
'''
Multi-line comments
'''

Python text formatted output

1. Use placeholders such as %s, %d

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)
Copy after login

PS: If you use %d, you must use int() to convert to numeric type, input Type defaults to string. In contrast to int(), str() converts a numeric type to a string.
In Python, values ​​and strings cannot be connected through the + sign. They must be converted first

2. Use parameters to format the output

info = '''
---------- info of {_name} ---------
Name: {_name}
Age: {_age}
job: {_job}
salary: {_salary}
'''.format(_name = name,
           _age = age,
           _job = job,
           _salary = salary)
Copy after login

3. Use Subscript formatted output

info = '''
---------- info of {0} ---------
Name: {0}
Age: {1}
job: {2}
salary: {3}
'''.format(name, age, job, salary)
Copy after login

Use the getpass module to hide the password entered by the user

import getpass
username = input("username: ")
password = getpass.getpass("password: ")
print(username)
print(password)
Copy after login

It should be noted that the above code cannot be run in IDEs such as PyCharm and must be run in the terminal Run in

Use the type() function to obtain the variable type

type(variable)
Copy after login

while … else statement

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("Ooops, think smaller...")
    else:
        print("Ooops, think bigger! ")
    count += 1
else :
    print("Ooops, you dont got it")
Copy after login

else statement block must be executed before the while exits normally. When the while statement is broken, the else statement block will not be executed.

for … else … statement

for i in range(10):
    print("i value = ", i)
    # break 后不会运行 else 块 
else:
    print("success ended")
Copy after login

is similar to while … else …. It will only run when the for statement ends normally. It will not run after break

The above is the detailed content of What you need to pay attention to when learning Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!