Learn python core programming and python Core

Source: Internet
Author: User

Learn python core programming and python Core

I have read python from scratch for a while, and I have provided HD resources [av4050443] For my website B. However, it is difficult to learn it as a basic program. I downloaded the python core programming pdf, make some notes here.

Although I use the second edition of teaching materials, I am using python3.5.2. I don't feel very similar. I can read it.

Here we start with exercise questions in Chapter 2!

 

 

 

 

--------------------------------------------------------

2-5: Use while and for to print 0-10:

I = 0

While I <11:

Print (I)

I + = 1

------------

For I in range (11 ):

Print (I)

 

 

---------------------------------------------

2-7: Enter the string and print it. Use the while and:

Str_input = input ('Enter the string :')

I = 0

While I <len (str_input ):

Print (str_input [I])

I + = 1

-----------

Str_input = input ('Enter the string :')

For I in str_input:

Print (I)

 

 

--------------------------------------------------------------

2-8: add five numbers:

I = 0

Temp = 0

While I <5:

Temp + = int (input ('enter a number :'))

I + = 1

Print (temp)

--------------

Temp = 0

For I in range (5 ):

Temp + = int (input ('enter a number :'))

Print (temp)

 

 

------------------------------------------------------

2-9: average value of the output five values:

Temp = 0

For I in range (5 ):

Temp + = int (input ('enter a number :'))

Arverage = temp/5

Print (arverage)

 

 

------------------------------------------------------------

2-10: judge whether to input 1 to 100. If yes, stop. Otherwise, re-input:

Num = int (input ('enter an integer between 1 and 100 :'))

While num not in range (2,101 ):

Print ('incorrect input ')

Num = int (input ('enter an integer between 1 and 100 :'))

Print ('you are right! ')

--------------

It is troublesome to use recursion. I am not very familiar with it (as shown below ):

Def function1 (num ):

If 1 <num <100:

Print ('your number in (1,100 )')

Else:

Print (the number you input is not in (1,100 )')

Num = int (input ('Please input a number :'))

Function1 (num)

 

Num = int (input ('Please input a number :'))

Function1 (num)

 

 

---------------------------------------------------------------

2-11: Input 1 sum, input 2 get average, input X Exit:

(It's almost a copy of someone else. I wrote one and always reported an error)

Def add ():

Temp = 0

For I in range (5 ):

Temp + = float (input ('enter the number of % d:' % (I + 1 )))

Print (temp)

Def aver ():

Temp = 0

For I in range (5 ):

Temp + = float (input ('enter the number of % d:' % (I + 1 )))

Print (temp/5)

Foo = str (input ('input 1 sum, input 2 get average, input X Exit :'))
If foo = 'X ':
Print ('exit successful ')
Elif foo = '1 ':
Add ()
Elif foo = '2 ':
Aver ()
Else:
Print ('input error ')

------------------

The following is my error Program (which will be changed later ):

Foo = str (input ('input 1 sum, input 2 get average, input X Exit :'))

Temp = 0

For I in range (5 ):

Temp + = float (input ('enter the number of % d:' % (I + 1 )))

If foo = 'X ':
Print ('exit successful ')
Elif foo = '1 ':
Print (temp)
Elif foo = '2 ':
Print (temp/5)
Else:
Print ('input error ')

 

 

--------------------------------------------------

2-15: input three numbers for sorting:

A = float (input ('first number of inputs :'))
B = float (input ('Enter the second number :'))
C = float (input ('Enter the third number :'))

If a> B:
A, B = B, a # ensure that a is small
If a> c:
A, c = c, a # ensure that a is smaller, and a is the smallest
If B> c:
B, c = c, B # ensure that B is smaller

Print (a, B, c)

 

 

 

 

  

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.