Python basics and python Basics

Source: Internet
Author: User

Python basics and python Basics
Python Introduction

Python is an object-oriented scripting language invented by Guido fansumm. Some people may not know what object-oriented and scripting means, but for a beginner, you do not need to understand it now. As we all know, the concept of full-stack engineers is very popular nowadays, while Python is a full-stack development language, so if you can learn Python well, then front-end, back-end, testing, big data analysis, crawlers and other tasks are competent.

Why Python?

There are various discussions about the language selection. I will not mention it here. I will reference an egg in Python to explain why I chose Python, enter import this in the Python interpreter.

1234567891011121314151617181920212223 >>> import this The Zen of Python by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!

In summary, the above words are "elegant", "clear", and "simple". Maybe you still don't understand them. Here is a simple example, if you use C/C ++ to write the same function, you may need to write 100 lines of code. If you use Python to write 20 lines of code, if there are several solutions to a problem, but Python will use the simplest method. Therefore, Python uses the simplest, most elegant, and clearest method to solve the problem.

Python 1. Install Python

Here I Recommend installing Python3, because over time, Python3 will definitely be the trend of the future, and we will follow the trend. You can download the corresponding version from the Python official website.

2. Write Hello, World

After the environment is installed, we can write code. Here we output Hello, World in two ways. The first one is implemented with the interpreter. Open cmd

Enter Python. If the following instance is not displayed, check whether the python environment variable is configured.

C:\Users\Tab>pythonPython 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> print('Hello,World')Hello,World

We can see that the output of hello world only requires a line of code, but there is a drawback, that is, if you accidentally close the window, the code will be gone, therefore, this method is not commonly used in actual work. In actual work, we save the code in. in The py file, it can also be saved as a txt file, but it cannot be seen that this is a Python file, so it is not recommended to do so, then execute python 1.py in the command line to print out Hello, World. Here, the Text editor I use is Sublime Text.

#!/usr/bin/env python# coding=utf-8print("Hello,World")

The first line of code indicates that the file is executed by the python interpreter in Linux, and the second line shows the encoding used by the interpreter when loading the file, if you do not add this sentence, Chinese characters will be garbled in python2, but not in python3, So if you use windows and python3, in fact, you do not need to add these two sentences, but we recommend that you add them in practice. Here, we use two methods to enter Hello and World.

3. variables, inputs, and outputs

Now you can useprint()Output the desired result. However, what if you want the user to input some characters from the computer? Python providesInput () allows you to input strings and put them in a variable. For example, enter the User Name:

name = input('username:')print('name')

Assume that the above Code is saved in file 1. in py, execute python 1 in the command line. py, you are prompted to enter a user name. After the input is complete, the entered characters are printed.nameVariable, and then save the input characters in the name variable. The variable can not only be a string, but also an integer or floating point number, such as a = 2

Defines an integer variable a with a value of 2. In addition, the rules defined by variables are as follows:

  • Variable names can only be any combination of letters, numbers, or underscores
  • The first character of the variable name cannot be a number.
  • The python keyword cannot be declared as a variable name.
Iv. Process Control and indentation

Python syntax is relatively simple and indented

12345678910 # xxxxx:'''A> 0: Output a. If a <0, output-.The variable a = 1 is defined first.'''= 1if a >= 0:    print(a)else:    print(-a)

To#The statement at the beginning is a comment. You can also use '''xxxx''' to comment multiple lines. The comment is for people, and the interpreter ignores the comment. Annotations are used to explain the functions and functions of a program. In the future, you must develop a good habit of writing comments. When the statement uses a colon:At the end, the subsequent statements must be indented.

5. while loop 1. Basic Format
1234567 # While condition:      # Loop body  # If the condition is true, the loop body executes  # If the condition is false, the loop body is not executed.     while True:    print(1)

The above test sequencing is an endless loop, the condition is always true, and 1 is continuously output.

2. The break exits the entire loop.
while True:    print(1)    break    print(2)

The above code will first output 1, and then encounter break, it will exit the current loop, so it will no longer print out 2

3. continue to exit the current loop
while True:    print(1)    continue    print(2)

You can think about what the above program outputs. The answer is that it will not stop outputting 1 1 1... why? Because the loop condition is always true, when the continue statement is encountered, the program jumps out of the current loop and does not execute the print (2) statement.

Vi. Homework

1. Use a while loop to input 1 2 3 4 5 6 8 9 10

2. Calculate the sum of all numbers from 1

3. Output all odd numbers in 1-100

4. Output all the even numbers in 1-100

5. Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99.

6. User Login (three machines

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 # Question 1t=0while t<10:    t+=1    if t==7:        continue    print(t) # Question 2sum=0for in range(101):    sum+=iprint(sum) # Question 3for in range(1,100,2):    print(i) # Question 4for in range(2,101,2):    print(i) # Question 5sum=0for in range(0,100):    if i%2==0:        sum-=i    else:        sum+=iprint(sum) # Question 6import getpassusername='jason'password='1233456'flag=0def login():    user=input('username:')    passwd=getpass.getpass('password:')    return user,passwordwhile flag<3:    user,passwd=login()    if username==user and password==passwd:        print('hello,jason')        break    else:        flag+=1        print ('error,input again')

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.