Python automated O & M course learning-Day1, python automation-day1

Source: Internet
Author: User

Python automated O & M course learning-Day1, python automation-day1

This article summarizes the first day of the Python automated O & M course for old boys.

The general content is as follows:

  Python Introduction

First Python program: Hello World

Python variable

User interaction (user input and output)

Process control: conditional statements (if/elif/else), cyclic statements (for/while/break/continue)

1. Introduction to Python:

1. Python is a high-level programming language that supports interpreted language, dynamic type, and strong type definition language. Developed by Guido van rosum during the 1989 Christmas period, the first official version of the Python compiler was born in 1991. It has become one of the mainstream programming languages.

2. It is mainly used in cloud computing, WEB development, scientific location and data analysis, artificial intelligence, finance, system O & M, and graphic GUI.

3. Advantages and Disadvantages of Python:

Advantages: simple, clear, and elegant; high development efficiency; high portability; scalability; embedded.

Disadvantages: The execution speed is slower than that of C and JAVA (the execution speed of PyPy interpreter is sometimes faster than that of C), the Code cannot be encrypted (interpreted language), and the thread cannot use multiple CPUs.

4. Python interpreters: There are many Python interpreters, such as CPython, IPython, PyPy, Jython, and IronPython, but CPython is the most widely used.

Ii. About all the environments running Python code in this article:

-- Operating System: Ubuntu 16.10 (Linux 4.8.0)

      

-- Python version: 3.5.2

      

-- Python IDE: PyCharm 2016.3.2

      

3. First Program: Hello World

Run the vim/vi command to create a Python File. Run the command "HelloWorld. py" and vim HelloWorld. py.

Enter the front content in HelloWorld. py:

#! /Usr/bin/python3.5 # Tell the Linux system to execute positive code through the/usr/bin/python3.5 interpreter #-*-coding: this line must be added in UTF-8-*-# Python2 to tell the Python interpreter to execute positive code in the form of UTF-8 encoding; the default UTF-8 in Python3 does not need to add this line. # Author: Spencer Jiang # print ("Hello, World! ") # Print Hello, World!

Two running modes:

1) grant the executable permission to HelloWorld. py, and then execute: chmod 755 HelloWorld. py.

> chmod 755 HelloWorld.py> ./HelloWorld.py

    

2) Run Python directly: python installation path

> /usr/bin/python3.5 HelloWorld.py

 

Iii. Python Variables

1. Variable-defined rules:

      • Variable names can only be any combination of letters, numbers, or underscores
      • The first character of the variable name cannot be a number.
      • The following keywords cannot be declared as variable names
        ['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
      • ValidThe following are examples of identifier names:i,__my_name,name_23Anda1b2_c3.

      • InvalidThe following are examples of identifier names:2things,this is spaced outAndmy-name.

  2. assign a value to a variable: assign a value to the variable through an equal sign

     Example: name = "Spencer Jiang"

You can also assign values to multiple variables in a row. For example:

A, B = 3, "jmw" print (a, B) print (type (B), type ()) ######## output result: 3 jmw <class 'str'> <class 'int'>

  

4. User Interaction,You can use the input () function to input Python3. Pyhton2 is a bit complicated.

The input () function can receive Task characters input from the user and return the characters entered by the user as strings.

Example 1 (UserInput. py): name = input ("Please input your name :")

Age = int (input ("Please input you age:") # convert the character entered by the user to the int type, and then assign the value to the age variable.

#!/usr/bin/python3.5# -*- coding:utf-8 -*-# Author: Spencer Jiangname = input("Please input your name: ")age = int(input("Please input you age: "))print("Your Name: %s, Your Age: %d" % (name, age))

  

Example 2: Enter the user name and password. Use the getpass module to hide the password. (HidePassword. py)

#!/usr/bin/python3.5# -*- coding: utf-8 -*-# Author: Spencer Jiangimport getpassusername = input("Please input your username: ")password = getpass.getpass("Please input your password: ")print(username, password)

  

 

V,Process control: Conditional judgment Statement (if/elif/else ):

Each condition is followed by a colon and a line break (the code to be executed when the condition is true uses indentation as a code block sign. python officially recommends four spaces for indentation)

Example 1: Play age (number) Game (GuessAge. py ).

#! /Usr/bin/python3.5 #-*-coding: UTF-8-*-# Author: spencer Jiangage_of_spencer = 65 # first set the value of age guess_age = int (input ("guess an age:") if guess_age = age_of_spencer: print ("Yes, You got it! ") Elif guess_age> age_of_spencer: print (" No, your number is a litter bigger ") else: print (" No, your number is a litter smaller ")

  

VI,Process control: for Loop (for x in range (10), break, and continue:

When the loop condition is met, the code of the loop statement block is executed. If the loop condition is not met, the loop statement ends.

For/while can also be followed by an else.

Break: When the break is executed, the entire cycle is ended;

Continue: When continue is executed, this loop is ended and the next loop is directly performed.

Example 1: output 4 numbers (PrintNumber. py) from 0 to 15, including 2, 4, 6, and 8 ).

#! /Usr/bin/python3.5 #-*-coding: UTF-8-*-# Author: Spencer Jiangfor I in range (, 2): if I = 0: # Skip the number "0". if I> 9: # exit the loop "break else: print (I)" When the value is greater than 9)

  

VII. Process Control: while LOOP, break, and continue (similar to for loop)

  A while LOOP requires a counter or a statement that terminates the while condition in the loop statement block. Otherwise, it will continue to run.

Example 1 (WhileLoop. py): Print 0 ~ 9 digits

#! /Usr/bin/python3.5 #-*-coding: UTF-8-*-# Author: Spencer Jiangcount = 0 # counter while True: print (count) count = count + 1 if count> 9: break

  

Example 2 (GuessAgeWhile. py): Age (number): Only three chances can be guessed.

#!/usr/bin/python3.5# -*- coding: utf-8 -*-# Author: Spencer Jiangage_of_spencer = 65count = 0while count < 3 :    guess_age = int(input("guess an age: "))    if guess_age == age_of_spencer :        print("Yes, You got it!")        break    elif guess_age > age_of_spencer :        print("No, your number is a litter bigger")    else:        print("No, your number is a litter smaller")    count += 1else:    print("You guess to much times!!!")

  

Example 2: After three incorrect guesses, a prompt is displayed to check whether the user still needs to guess. If the user inputs "n", it indicates that it is stopped.

#!/usr/bin/python3.5# -*- coding: utf-8 -*-# Author: Spencer Jiangage_of_spencer = 65count = 0while count < 3 :    guess_age = int(input("guess an age: "))    if guess_age == age_of_spencer :        print("Yes, You got it!")        break    elif guess_age > age_of_spencer :        print("No, your number is a litter bigger")    else:        print("No, your number is a litter smaller")    count += 1    if count == 3 :        continue_confirm = input("Do you want to continue to guess?")        if continue_confirm != 'n' :            count = 0else:    print("You guess to much times!!!")

  

(This section is complete)

Related Article

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.