Section III Hello World--python first Experience

Source: Internet
Author: User
Tags logical operators

Festival Flag--hello World

It is said that simple elegance, powerful function is the charm of Python, here to see the simple, elegant is what kind of, the next learning slowly experience it!

1     Print ("Hello World"

Data types and variables

Data Type

A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly: integer, floating-point, String, Boolean.

Variable

The concept of variables is basically the same as the equation variables in junior algebra, but in computer programs, variables can be not only numbers, but also arbitrary data types.

Variables are represented in the program by a variable name.

Naming rules:

1. The variable name must be a combination of uppercase and lowercase English, numeric and _
2. Cannot start with a number, cannot contain special characters and spaces
3. You cannot name a reserved word---reserved word refers to a python built-in variable name
4. Can not be named in Chinese--in Chinese from a grammatical point of view will not be error
5. The variable name defined should make sense-the variable name defined can literally indicate the meaning
6. Hump-Type life, underline the word--python the official recommendation is to use an underscore Method Example: user_name
7. Variable name case-sensitive--python case-sensitive

Constant

Fixed amount, constant name all in uppercase.

Operator:

There are two kinds of division, a division is / :

>>> 9/4
2.25

/The division evaluates to a floating-point number, even if it is exactly divisible by two integers, and the result is a floating-point number:

>>> 9 / 33.0

There is also a division that is // called a floor divide, where the division of two integers is still an integer:

10 // 33

Residual:

>>> 10 % 31

The result is always an integer, regardless of whether the integer // divides or takes the remainder, so the result of the integer operation is always accurate.

assignment operator : =, + = = *=/=%=//= **=

>>> num = 2    >>> num + = 1   #  equivalent to num = num + 1>>> num-= 1   # equivalent to num = num-1>>> num *= 1   #  equivalent to num = num * 1>>> num/= 1   
    
     # 
      equivalent to num = num/1>>> num//= 1   
     # 
      equivalent to num = num//1>>> N Um%= 1   
     # 
      equivalent to num = num% 1>>> num **= 2   
     # 
      equivalent to num = num * * 2
    

comparison operators:>, <, >=, <=, = =,! = True False

>> a = 5>>> B = 3>>> a > B#checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. True>>> a < b#checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. False>>> a <= b#checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. False>>> a >= b#checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. True>>> A = = b#checks whether the values of the two operands are equal, and if so, the condition becomes true. False>>> A! = B#checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. True

logical operators: not, and, or

Logical operators are used to make logical calculations. Like the comparison operators we used above, each time the comparison is actually a conditional judgment, it will get a value of TRUE or false accordingly. The operand of a logical operator is an expression or variable that is used to make conditional judgments.

To determine the priority with a left-to-right short-circuit principle, the specific method is as follows:

The and and operators, both sides are true, the condition is set. If the left is false, the right side does not need to be judged.

If the OR OR operator is true on the left, the condition is set, otherwise the right side is judged.

Not a non-operator that reverses the logical state.

Use of annotations

Writing neat notes to your code is a good habit for a good programmer. Neat and concise code does not necessarily have a higher readability, in some of the business is more cumbersome, more parameters of the function, reading the code of the people will be in the use of various parameters entangled, but if the parameters or business operation of the code next to add neat comments, you can let the existing code context clear, more flesh and blood.

Annotations are available in two ways:

Single-line Comment #
Multiline comment "" "Content" "" (' content ')

Role:
1. Avoid the code you forgot to write--
2. For people to see
3. Don't comment on what your code does, but comment on why my code is doing it.

Judgment statement

#Guess ageAge = 50User_input_age= Int (Input ("Age is :"))ifUser_input_age = =Age :Print("Yes")elifUser_input_age >=Age :Print("is bigger")Else:    Print("is smaller")

While loop

When a break is encountered in the loop, the entire loop is ended.

#guess the age upgrade versionAge = 50 whileTrue:user_input_age= Int (Input ("Age is :"))    ifUser_input_age = =Age :Print("Yes")         Break    elifUser_input_age >=Age :Print("is bigger")    Else:        Print("is smaller")Print("END")        

Continue

# Continue is used  , the continue is no longer executed continue after the code, the next cycle again. num = 1 while num <=    :if num = = 4:        + = 1         Continue           print(num)    + = 1

#用 "#" to print a rectangle with a Y-high x Length:

#method: 1. First output a #. 2. Loop the first step x times to get the # of a row of x columns. 3. Cycle the 2nd step y times to get the # of the Y row x column. ############Len= Int (Input ("Please enter the length:")) Heigt= Int (Input ("Please enter High:")) H=0 whileH <heigt:l=0 whileL <Len:Print("#", end=" ") L+ = 1h+ = 1Print()

Output Right triangle:

1 #Output Right triangle2NUM1 = 13  whileNUM1 < 5:4num2 = 15      whileNum2 <=NUM1:6         Print("*", end="")7Num2 +=18     Print("")9NUM1 + = 1

99 Multiplication Table:

1 #99 multiplication table using while implementation2NUM1 = 13  whileNUM1 <= 9:4num2 = 15      whileNum2 <=NUM1:6         Print(Num2,"*", NUM1,"=", num1*num2,end="\ t")7Num2 + = 18NUM1 + = 19     Print("")

Section III Hello World--python first Experience

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.