Python Basics-The first knowledge of Python

Source: Internet
Author: User
Tags arithmetic operators

Classification of annotations

<1> Single line Comment

Start with #, #右边的所有东西当做说明, rather than the program that is really going to execute, plays an auxiliary role

    # I'm a note, and I can write some functional descriptions in it.    Print ('helloWorld')
<2> Multi-line annotations
" " I am a multi-line comment, can write a lot of lines of function description   hahaha ...  "    print('╔═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗')
Definitions of variables and types <1> variables

In a program, sometimes we need to sum up 2 data, so what should we do?

Everyone analogy to the real life, such as to go to the supermarket shopping, often we need a basket, used to store items, wait until all the items are purchased, at the cashier to checkout

If you need to sum up 2 or more data in a program, you need to store the data first and then add it up

In Python, storing a data requires 变量 something called, as in the following example:

# NUM1 is a variable, just a small basket .  =  num2 is also a variable  # Add the data from the two "basket" of NUM1 and num2 and put it in the result variable 
    • Description
      • The so-called variable, can be understood as 菜篮子 , if you need to store multiple data, the simplest way is to have multiple variables, of course, you can also use a
      • A program is used to process data, and variables are used to store data.

Types of <2> variables

In the program:

To make more use of memory space and more efficient management of memory, variables are of different types, as follows:

How do you know the type of a variable?

    • In Python, as long as a variable is defined, and it has data, then its type is determined, without the developer's initiative to explain its type, the system will automatically identify
    • You can use the type (the name of the variable) to see the type of the variable

Identifiers and Keywords <1> identifiers

Some symbols and names that developers customize in the program

Identifiers are defined by themselves, such as variable names, function names, etc.

<2> Rules for identifiers

Identifiers consist of letters, underscores, and numbers, and numbers do not start

Identifiers in Python are case-sensitive

<3> keywords
    • What is a keyword

      Python some identifiers with special functions, this is called the keyword

      Keyword is used by Python, so developers are not allowed to define their own identifiers with the same name as the keyword

    • View Keywords:
      and     as      assert     break     class      continue    def     del      elif    else    except     exec      finally    for         from    global      if      in      import     is        lambda     not         or      pass      print   raise   return     try       while      with        yield

可以通过以下命令进行查看当前系统中python的关键字







Output of variables in output <1>.python
# Print Tips    Print ('helloWorld')
<2>. Formatted output

Look at the following code:

    Age = Ten    print(" I am%d years old ")    + = 1    print("  I'm%d years old "    % Age "+ = 1    print(" I'm%d this year  "%age)

 ...

In the program, you see this % operator, which is the formatted output in Python.

    Age    ="xiaohua"    print(" My name is%s, ages is%d "% (name,age))

<3> commonly used format symbols

The following is the complete, it can be used with the% symbol list:

format Symbols Conversion
%c Character
%s formatted with the STR () string conversion
%i Signed decimal integer
%d Signed decimal integer
%u unsigned decimal integers
%o Eight-binary integers
%x hexadecimal integers (lowercase letters)
%x hexadecimal integers (uppercase letters)
%e Index symbol (lowercase ' e ')
%E Index symbol (uppercase "E")
%f Floating point Real numbers
%g Shorthand for%f and%e
%G Shorthand for%f and%e

<4>. Line break output

In the output, if \n so, then \n the content will be displayed on a different line

    Print ("1234567890-------"#  is displayed    on one line Print (the "1234567890\n-------"#  line shows 1234567890, and the other line shows-------

 

Python supports the following types of operators

    • Arithmetic operators

The following is calculated using a=10, b=20 as an example

operator Description Example
+ Add Two objects added a + B output result 30
- Reducing Get negative numbers or a number minus another number A-B output 10
* By Two number multiplied or returns a string that is repeated several times a * b output result 200
/ Except x divided by y b/a output result 2
// Take the Divide Returns the integer portion of the quotient 9//2 output result 4, 9.0//2.0 output 4.0
% Take surplus Returns the remainder of division B% A output result 0
** Power Returns the Y power of x a**b to 10 20, output 100000000000000000000











>>> 9/2.04.5>>> 9//2.04.0

    • Assignment operators
operator Description Example
= Assignment operators Give the result on the right of the = number to the left variable num=1+2*3 result num has a value of 7




>>> A, B = 1, 2>>> a1>>> b2

    • Compound assignment operator
operator Description Example
+= Addition assignment operator c + = A is equivalent to C = C + A
-= Subtraction assignment operator C-= A is equivalent to C = c-a
*= Multiplication assignment operator C *= A is equivalent to C = c * A
/= Division assignment operator C/= A is equivalent to C = c/a
%= Modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator C **= A is equivalent to C = c * * A
//= Take the divisible assignment operator C//= A is equivalent to C = c//A
 










常用的数据类型转换

function Description
int (x [, Base]) Convert x to an integer
Long (x [, Base]) Convert x to a long integer
Float (x) Convert x to a floating-point number
Complex (real [, Imag]) Create a complex number
STR (x) Convert an object x to a string
REPR (x) Convert an object x to an expression string
eval (str) Used to evaluate a valid Python expression in a string and return an object
Tuple (s) Converting a sequence s to a tuple
List (s) Convert the sequence s to a list
Chr (x) Converts an integer to one character
UNICHR (x) Converts an integer to a Unicode character
Ord (x) Converts a character to its integer value
Hex (x) Converts an integer to a hexadecimal string
Oct (x) Converts an integer to an octal string

Example
'  - ' # at this point the type of a is a string, which contains 100 of these 3 characters # at this point the type of B is integral type, which is stored in the number of Print ("a=%d"%b)

 

Python Basics-The first knowledge of Python

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.