Python Learning Day1--python Basics

Source: Internet
Author: User

Python the pros and cons

See the pros first

    1. Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
    2. Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
    3. High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
    4. Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
    5. Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
    6. Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.

Look at the disadvantages again:

    1. Slow, Python runs faster than the C language, and slower than Java, so this is the main reason why many so-called Daniel disdain to use Python, but in fact, this refers to the speed of slow in most cases the user is not directly aware of, Must rely on the use of testing tools to reflect, such as you use C a program to spend 0.1s, with Python is 0.01s, so c directly than Python faster 10s, is very exaggerated, but you can not directly perceive through the naked eye, Because a normal person can perceive the smallest unit of time is 0.15-0.4s around, haha. In fact, in most cases python has been fully able to meet your requirements for the speed of the program, unless you want to write to the speed of the most demanding search engine, in this case, of course, it is recommended that you use C to achieve.
    2. Code can not be encrypted, because Python is an explanatory language, its source code is stored in the form of a name, but I do not think this is a disadvantage, if your project requires that the source codes must be encrypted, then you should not use Python in the beginning to implement.
    3. Threads do not take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil, the Global Interpreter lock (interpreter lock), is a tool that the computer programming language interpreter uses to synchronize threads so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil. A compromise solution to this problem is discussed in more detail later in the Threads and Processes section.

Of course, Python also has some other small shortcomings, in this does not enumerate, I want to say that any language is not perfect, have good and not good at doing things, it is recommended that you do not take a language disadvantage to the advantage of another language to compare, language is just a tool, is a tool to realize the idea of the program designer, as we learned in high school geometry, sometimes need compasses, sometimes need to use the same setsquare, take the corresponding tools to do what it is best at the right choice. A lot of people asked me, which is the best shell and python? I replied that the shell is a scripting language, but Python is not just a scripting language, it can do more, and then someone who has a dead-point says there is absolutely no need to learn python, Python can do things that the shell can do, as long as you have enough cow B, And then the shell can write Tetris such a game, I can say to express only, not with SB theory, SB will pull you to the same height as he, and then with the full experience to knock you down.

Variable

Variable \ Character encoding

Variables is used to store information to is referenced and manipulated in a computer program. They also provide a labeling data with a descriptive name, so we programs can be understood more clearly by the RE Ader and ourselves. It's helpful to think of variables as containers, that's hold information. Their sole purpose is to label and store data in memory. This data can and is used throughout your program.

declaring variables

123 #_*_coding:utf-8_*_name ="Alex Li"

The code above declares a variable named: Name, and the value of the variable name is: "Alex Li"

Rules for variable definitions:

      • Variable names can only be any combination of letters, numbers, or underscores
      • The first character of a variable name cannot be a number
      • The following keywords cannot be declared as variable names
        [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

Assigning values to variables

12345678 name ="Alex Li" name2 = nameprint(name,name2) name = "Jack"print("What is the value of name2 now?")

Notes

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

User input
1234567 #!/usr/bin/env python#_*_coding:utf-8_*_#name = raw_input("What is your name?") #only on python 2.xname = input("What is your name?")print("Hello " +name )

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

12345678910 #!/usr/bin/env python# -*- coding: utf-8 -*- import getpass  # 将用户输入的内容赋值给 name 变量pwd = getpass.getpass("请输入密码:") # 打印输入的内容print(pwd)

Expression If ... else

Scenario One, user login verification

1234567891011121314151617181920 # 提示输入用户名和密码 # 验证用户名和密码#     如果错误,则输出用户名或密码错误#     如果成功,则输出 欢迎,XXX!#!/usr/bin/env python# -*- coding: encoding -*- importgetpass  name =raw_input(‘请输入用户名:‘)pwd =getpass.getpass(‘请输入密码:‘) ifname == "alex"andpwd =="cmd":    print("欢迎,alex!")else:    print("用户名和密码错误")

Scenario two, Guess age game

Set your age in the program, and then start the program to let the user guess, after the user input, according to his input prompts the user to enter the correct, if the error, the hint is guessed big or small

+ View Code

  

Outer variables, which can be used within the inner layer code, should not be used by the outer code

An expression for loop

The simplest loop 10 times

123456 #_*_coding:utf-8_*___author__ =‘Alex Li‘  for inrange(10):    print("loop:", i )

Output:

12345678910 loop:  0 loop:  1 loop:  2 loop:  3 loop:  4 loop:  5 loop:  6 loop:  7 loop:  8 loop:  9

Requirements One: or the above program, but encountered less than 5 cycle times will not go, jump directly into the next loop

1234 for   in   Range ( 10 ):      if  i< 5 :          continue   #不往下走了, go directly to the next loop      print ( "loop:"

Requirements Two: or the above program, but encountered more than 5 cycles will not go, direct exit

1234 for   in   Range ( 10 ):      if  I> 5 :          break   #不往下走了, jump right out of the loop      print ( "loop:"

Python Learning Day1--python Basics

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.