Python a few small programs, in fact, I think can be called Beginner's basic algorithm

Source: Internet
Author: User

Yesterday study, today do a finishing, before learning a few days C, feel nothing to get out, a little discouraged, see Python after try, from the most basic things to learn from, hope not to disappoint my this little enthusiasm.

Application of the IF statement

1N=12  whileN<5:3INP = input ('Please enter your membership level:')4     ifINP = ="Senior Member":5         Print('VIP 1')6     elifINP = ="Platinum Member":7         Print("VIP 2")8     elifINP = ='Platinum Members':9         Print("VIP 3")Ten     Else: One         Print("Chengguan") A  -     Print('Why do you have to do that ?') -N+=1 the Pass - Print('poor force, go! ')

List 1-100 of even numbers:

N=1 while n<101:    if n==1:        pass    elif n%2= =1:            #利用奇数的性质, can not be divisible by 2, the remainder is 1, therefore,% (modulo) determine the        remainder is not 1pass    else:         Print (n)    n+=1

List numbers 1 2 3 4 5 6 8 9 10:

Count=1 while count<11:    if count==7: #判断循环后的数值是不是7        Pass                 # C language does not have this keyword (do not know the name of the pair, some seem to be called reserved words)    Else:        Print(count)    Count+=1print('-----END------')

Classical Gaussian algorithm: 1-100 of the cumulative, understand the 5050

N=1sum=0 while n<101:    sum=sum+n    n=n+1print(sum)

An odd number within 100

N=1 while n<101:    if n%2==0:        pass    Else  :        print(n)    n+=1

Calculate 1-2+3-4+5 ...., I finally count is-50, do not know yes No

N=1sum=0 while n<101:    t=n%2    if t==0:        sum= sum-N    else:        sum=sum+n    n=n+1print(sum)

Enter three password, incorrect exit, feel a bit strange, not clear

N=1 whileN<4: Mima=input ('Please enter your password:'#有点疑惑, unlike C, the cursor jumps to the front when the password is required.ifMima = ='123':            Print('Grass bag, come in! ')             Break        Else:            Print('Please re-enter') n+=1Print('you have entered more than 3 times, get out as soon as possible! otherwise alarm')

Python a few small programs, in fact, I think can be called Beginner's basic algorithm

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.