Introduction to computer science and programming (2) Python syntax basics: branches and conditions

Source: Internet
Author: User

I. Value

Every date type in python is a type.

Any data type in Python belongs to a certain type.

For example, 3 belongs to the integer type, 3.14 belongs to the floating point type, and 'abc' belongs to the string type.

# Use the type () function to view the variable type (3) #=> <class 'int'> type (3.14) #=> <class 'float'> type ('abc') #=> <class 'str'>
Ii. type check

Python is a language with strong type check.

For example, when you perform another four arithmetic operations, the operator checks the type of the calculation object.

#2.2 type check 3 + 4 #=> 73 * 'AB' # => 'abababab' 'AB' + 'ac' # => 'abac' 3 + 'AB '# = TypeError: unsupported operand type (s) for +: 'int' and 'str' str (3) + 'AB' # => '3ab'

When the string and number are added, Python checks that the type does not match and automatically reports an error. Of course, python does not check all type errors. For example, Python does not check the type when comparing 3 <'AB' in earlier versions (the latest version is available ). Therefore, a good type specification should be developed.

Develop good type specifications.

Iii. variable types

The binding method between variable types and values is dynamic.

When a value is assigned to a variable, the variable inherits the value type. However, if the value of the variable changes, the type also changes.

#2.3 variable type x = 3 type (x) # => <class 'int'> y = 'abc' type (y) #=> <class 'str'> x = ytype (x) #=> <class 'str'>

When the value of variable x is 3, x is an integer. When the value 'abc' of y is assigned to x, the x type is changed to the string type. Therefore, to avoid unnecessary errors, you should develop good programming habits:

Do not arbitrarily change the variable type.

Iv. Notes

Python supports single-line and multi-line comments. Single-line comments are marked with #, and multi-line comments are marked with '''comment content. To develop good programming habits

A year later, you still understand your code.

5. Condition statements

If condition:

Instruction Set

Elseif conditions:

Instruction Set

Else:

Instruction Set

The colon is very important because it declares that the next series of commands will be a code block.

#2.5 determine the parity of x = 15if (x/2) * 2 = x: print ('even ')
Print ('even') else: print ('odd ')

 

 

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.