Learn what Python needs to be aware of

Source: Internet
Author: User

Hello World

Print HelloWorld using the print () method
Name = "Jenkin Li"

Print ("My name is", name)

Coding issues in Python 2.x

Because Python 2.x uses ASCII encoding, the default does not support Chinese, the file header must be used to declare what encoding
#--Coding:utf-8--

Comments for Python

Divided into single-line comments and multiline comments
# single Comment
'''
Multi-line comments
'''

Python Text Formatted output

1. Use placeholders such as%s,%d, etc.

Name = input ("Name:") Age = Input ("Age:") job = input ("job:") salary = input ("Salary:") info = ""----------info of%s-- -------Name:%sage:%sjob:%ssalary:%s '% (name, name, age, job, salary) print (info)

PS: If you use%d, you must convert to a numeric type using int (), and the type of input defaults to a string. In contrast to int (), str () converts a numeric type to a string.
It is not possible to concatenate numbers and strings through the + sign in Python, you must first convert

2. Format the output with parameters

info = "'----------info of {_name}---------Name: {_name}age: {_age}job: {_job}salary: {_salary} '. Format (_name = name,           _age = age,           _job = job,           _salary = salary)

3. Format the output with subscript

info = "'----------info of {0}---------Name: {0}age: {1}job: {2}salary: {3} '". Format (Name, age, job, salary)

Hide user-entered passwords using the Getpass module

Import getpassusername = input ("username:") password = getpass.getpass ("Password:") print (username) print (password)

It is important to note that the above code cannot be run in the IDE, such as Pycharm, and must be run in the terminal

Get the variable type using the type () function

Type (variable)

While ... else statement

Count = 0while Count < 3:    guess_age = int (input ("Guess Age:"))    if guess_age = = Age_of_oldboy:        print ("Yes, You got It ")    elif guess_age > Age_of_oldboy:        print (" Ooops, think smaller ... ")    else:        Print ("Ooops, Think bigger!")    Count + = 1else:    print ("Ooops, you dont got It")

The Else statement block must be executed again while it exits normally, and the Else statement block will not be executed if the while statement is break

For ... else ... Statement

For-I in range:    print ("I value =", i)    # does not run the else block after break:    print ("Success ended")

With while ... else ... Similarly, it will run when the for statement ends normally, and will not run after a break

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.