Beginner Python Essays

Source: Internet
Author: User

First of all, the programming language is divided into compiled, interpreted and mixed type, where Python is an interpreted language, the following table is the common language classification

Compiled vs explanatory type

Compiled type
Pros: Compilers typically have pre-compiled procedures to optimize code. Because the compilation is done only once, the runtime does not need to compile, so the program execution of the compiled language is highly efficient. Can run independently from the language environment.
Cons: After compilation, the entire module needs to be recompiled if modifications are required. When compiling the machine code according to the corresponding running environment, porting between different operating systems will be problematic, and you need to compile different executables according to the operating system environment you are running.

Explanatory type
Pros: Good platform compatibility, can be run in any environment, provided the interpreter (virtual machine) is installed. Flexible, modify the code when the direct modification can be quickly deployed, without downtime maintenance.

Cons: Every time you run, you have to explain it again, performance is not as good as the compiled language

The above is a few concepts, but more tired of the described. I learned about Java before I learned Python, but after taking over the python for a while, I felt that Python was actually written in a much smaller way than Java and significantly less code.

The following dry goods (installation configuration Absolutely not speaking)

declaring variables

Name = ' demo '                   # string age = $                        # integer miles = 1000.0                  # floating-point number Name,age = ' demo ',        multiple variable assignments

Python's variable declaration is so simple and rude, direct use, which leads to a sentence behind the learning time, everything is the concept of objects.

Rules for variable definitions

Variable names can be any combination of numbers, letters, and underscores

Variable name the first character cannot be a number

The following keywords cannot be used as variable names

[' False ', ' None ', ' True ', ' and ', ' As ', ' assert ' , ' break ' ,< Span class= "PLN" > ' class ' , ' Continue ' Span class= "pun" >, , ' del ' , ' elif ' , ' Else ' , ' except ' , ' finally ' , ' For ' , ' from '

' Global ', ' If ', ' Import ', ' In ', ' is ' , ' lambda ' , ' nonlocal ' , ' not ' , ' or ' , ' Pass ' , ' raise ' , ' return ' , ' try ' "while" , ' with ' , ' yield ' ]

Python is case-sensitive

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore, which is not directly accessible by the _foo, must be accessed through the interface provided by the class and cannot be imported with the FROM XXX import *;

A __foo that begins with a double underscore represents a private member of a class; A __foo__ that begins and ends with a double underscore represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class

Comments

Comments in Python are two, a single-line comment, a multiline comment

# This is a single-line comment "' This is a multiline comment that can write any character in this space '

 

Number types in Python

int (integer), normal number 1234, Python3 equivalent to Long integer, no python2 long type

BOOL (Boolean), True, and False

Float (floating point), 1.23, 3E-2

Complex (plural), 1+2j, 1.1+2j

A string in Python

Python string, available + splicing, can be performed * operations, can be sliced, the following is a string of some methods, write a separate essay to record

# string concatenation name = ' Demo ' age = ' + ' i = name + ' is ' + ageprint (my) ' Output results Demo is 18 ' ' # Note that my age here defines the string type, so it can be added, if written as Age = 18 Will error # string multiply print (name*3) "Output result Demodemodemo '"

  

Input of Python

Input is used to let the user enter content, such as

Name = input (' Enter your name ') password = input (' Enter your password ') ' The problem is: input returns a string, so when making a judgment, if you want to compare with the number must be a type conversion ' # If you do not want to see the password input content, we can use the Getpass module to modify the import Getpasspassword = Getpass.getpass (' Please enter the password ')    # at this time in the user input interface is not see the password print (' Password =%s '%password)          # Verify  that the%s in the code is a placeholder placeholder  with these kinds of string%s; integer%d; floating-point number%f

The arithmetic operations of Python

Operator Describe Instance B = 10,22
+ Add A + B output result 32
- Subtraction -B Output Result-12
* Multiply A*b Output Results 220
** Power-Returns the Y power of X B**a Output Results 26559922791424
/ Divide B/2 Output Results 2.2
// Divisible B//A Output Results 2
% Take the remainder A%b Output Results 10

Comparison operations for Python

Operator Describe Instance (return Boolean) A, B = 10,12
== Compare objects for Equality A==b return False
! = Comparison is not equal A!=b returns True
<> Ditto A<>b returns True
> Greater than A>b return False
< Less than A<b returns True
>= Greater than or equal A>=b return False
<= Less than or equal A<=b returns True

Assignment operations for Python

Operator Describe Instance B = 10,20
= Simple assignment operators c = A+b assigns the a+b result to C
+= Addition assignment operator c + = a equivalent c = c+a
-+ Subtraction assignment operator c-= a equivalent c = c-a
*= Multiplication assignment operator C *= a equivalent c = c*a
/= Division assignment operator C/= a equivalent c = c/a
%= Modulo assignment operator C%= a equivalent c = c%a
**= Power assignment operator C **= a equivalent c = c**a
//= Take the divisible assignment operator C//= a equivalent c = c//a

Python bitwise operators

Python bit operators have &, |, ^, ~, <<, >> These, is the number as a binary operation, usually not much, also not interested in writing

Python logical operators

Operator Logical Expressions Describe Instance B = 10,20
and X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. (A and B) returns 20.
Or X or Y Boolean "or"-if X is non-0, it returns the value of x, otherwise it returns the computed value of Y. (A or B) returns 10.
Not X not y Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. Not (A and B) returns False

Python member operators

Operator Describe Instance
Inch Returns False if the value found in the specified sequence returns True. x in the y sequence, if X returns True in the y sequence.
Not in Returns True if no value is found in the specified sequence, otherwise False. X is not in the Y sequence if x does not return True in the y sequence.

Python identity operator

Operator Describe Instance
Is is to determine whether two identifiers are referenced from an object x is y, similar to ID (x) = = ID (y) , returns True if the same object is referenced, otherwise False
is not Is does not determine whether two identifiers are referenced from different objects x is not y, similar to ID (a)! = ID (b). Returns True if the reference is not the same object, otherwise False is returned.

Python operation priority

The following table lists all the operators from highest to lowest priority:

Operator

description

* Index (highest priority)
~ +- bitwise rollover, unary Plus and minus sign (most The latter two methods are named [email protected] and [email protected])
*/%// multiply, divide, modulo and divide all
+- Add subtraction
>> << right-shift, left-shift operator
& bits ' and '
^ | Bitwise operators
<= < > >= comparison operators
<> = = = equals operator
=%=/=//=-= + = *= **= assignment operator
is a not identity operator
in Not in member operator
not and or logical operators

Beginner python essay

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.