Py-day1 simple use method and syntax use details, py-day1 use Details

Source: Internet
Author: User

Py-day1 simple use method and syntax use details, py-day1 use Details

1. Getting started with python

1. standard python format

Create a python file ending with. py

For example, vi hello. py

#! /Usr/bin/env python

#-*-Coding: UTF-8-*-# Chinese characters can be used by default for Versions later than python3.5.

Print "Hello Word"

Run:

Python hello. py

Or add the execution permission: chmod 755 hello. py.

./Hello. py

 

2. Variables

Name = "liudong"

Name2 = name

Then name = "liuyansheng"

# Name2 remains unchanged because it is re-created once modified. Name2 will open up a new memory space.

 

The set is different:

Name = ['Liu ', 'any', 'sheng',]

Name2 = name

# Name2 changes because different elements in the set and string are not continuous. Adding an element in name will not open up a new space, and name2 will change.

 

3. Input

Name = raw_input ("Enter the User name :")

# Print the output content

Print name

 

To hide a password, use the getpass method in the getpass module, that is:

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Liudong

Import getpass

# Assign the value of user input to the name variable

Pwd = getpass. getpass ("enter the password :")

# Print the input content

Print name

 

4. Process Control

(1) user identity authentication:

Syntax: if... else if... elif... else

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Liudong

Import getpass

Name = raaw_input ("Enter the User name :")

Pwd = raw_input ("enter the password :")

If name = "liudong" and pwd = "123456 ":

Print "Login successful"

Else:

Print "Logon Failed"

(2) Guess age:

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Liudong

Liudong = 24

Guess_age = int (input ("guess age :"))

If guess_age = liudong:

Print ("yes ")

Elif guess_age> liudong:

Print ("smaller ....")

Else:

Print ("bigger ....")

 

Loop Three times, right click to exit:

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Liudong

Liudong = 24

Count = 0

While True:

If count = 3:

Break

Guess_age = int (input ("guess age :"))

If guess_age = liudong:

Print ("yes ")

Break

Elif guess_age> liudong:

Print ("smaller ....")

Else:

Print ("bigger ....")

Count + = 1

 

A maximum of three rounds can be guessed. The system automatically exits. The system prompts "fuck off" by mistake ":

#! /Usr/bin/env python

#-*-Coding: UTF-8 -*-

# Liudong

Liudong = 24

Count = 0

While count <3

Guess_age = int (input ("guess age :"))

If guess_age = liudong:

Print ("yes ")

Break

Elif guess_age> liudong:

Print ("smaller ....")

Else:

Print ("bigger ....")

Count + = 1

Else:

Print ("fuck off ")

Note: continue jumps out of the current loop, and break jumps out of the entire loop.

 

5. Flag Space

 

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.