[Python_Day1], python_day1

Source: Internet
Author: User

[Python_Day1], python_day1

Abstract: data types, variables, operators, selection structures, cyclic structures, and string formatting

First knowledge of python

The first python program:

1 >>> print('Hello,Python')2 Hello,Python

Main differences between python2 and python3:

Integer Data Type (int)

Such as 0,-123, 0xff, 035, 0b101, etc.

Float)

Such as 15.0, 0.37, 21,. 21, 2e2, 15e-2, etc.

Complex (complex) Boolean (bool)

True and False. The following values are considered False:

1 >>> a = False2 >>> type(a)3 <class 'bool'>

 

Forced conversion data type

Sample Code:

1 >>> float (5) 2 5.03 >>> int (3.3) 4 35 >>> list (1, 2, 3) 6 [1, 2, 3] 7 >>> tuple ([1, 2, 3]) 8 (1, 2, 3)View Code

In addition to int (obj), float (obj), list (obj), tuple (obj), str (), dict (), and so on.

              

Variable

Python is a dynamic type language, that is, variables do not need to display declared data types. Python regards any data as an "object". variables are used to point to objects. Variable assignment is to associate objects with variables.

Example:

1 >>> a = 2 2 >>> B = 3 3 >>> c = a * B 4 >>> print (c) 5 6 6> B = 2 7> print (c) 8 6 9> c = a * b10> print (c) 11 4View Code

Note:

And del from not while as elif global or with assert else if pass yield try
Break resume t import print classexec in raise continue finally is return def for lambda
          
          

Arithmetic Operators:

Relational operators:

 

Logical operators:

Bitwise OPERATOR:Sequence and selection Structure

Sample Code:

1 age = 2562 guess = int (input ("Plz input a number:") 3 4 if guess> age: 5 print ("Your guess number is bigger ") 6 elif guess <age: 7 print ("Your guess number is lower") 8 else: 9 print ("Bingo, Your number is right! ")View Code

If single branch:

If condition expression:

Statement Block

Note: The same statement block has consistent indentation, generally four spaces (four spaces for the tab key in windows and eight spaces for the tab key in linux)

If multi-branch:

If condition expression:

Statement Block 1

Elif conditional expression:

Statement Block 2

Elif conditional expression:

Statement block 3

......

Else:

Statement Block n

Loop Structure

The loop structure in python includes the while and for loop structures.

While statement

While loop control conditions:

Loop body

Or

While loop control conditions:

Loop body

Else:

Statement

View Codefor statement

For target identifier in sequence:

Loop body

1 >>> for I in range (3): 2... print (I) 3... 4 05 16 2Formatting of View Code auxiliary statement strings

For details, see the following code example:

1 name = input ('name: ') 2 age = input ('Age:') 3 job = input ('job: ') 4 5 6 # string formatting Method 1: 7 print ('information of ', name,': \ n', 'name: ', Name,' \ nAge: ', age,' \ nJob: ', job) 8 # only strings can be concatenated. 9 # '+' indicates re-opening a memory 10 print ('information of '+ name +' in String concatenation ': \ n' + 'name: '+ Name +' \ nAge: '+ age +' \ nJob: '+ job) 11 # multiple strings appear, large memory usage 12 13 # string formatting method print ('information of % s: \ nName: % s \ nAge: % s \ nJob: % s' % (name, name, age, job) 15 # Only one string, memory occupied less 16 17 # string formatting method message = '''19 Information of % s: 20 Name: % s21 Age: % s22 Job: % s23 ''' % (name, name, age, job) 24 print (message) 25 26 # string formatting method print ('information of {}:\ nName :{}\ nAge :{}\ nJob :{}'. format (name, name, age, job) 28 ''' 29 format function: 30 {} The format is: {Domain name: The alignment occupies the width. precision type} 31 examples: {: 9.4f} 32 '''View Code

 

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.