Chapter 1 of the Python Course

Source: Internet
Author: User

Chapter 1 of the Python Course

Chapter 1 of the Python Course

1. Enter the ascii code 1-and output the corresponding characters
>>> For I in range (0,127)
... Print I, chr (I)
...
0
2. Input integers a, B, c, and d4 to calculate the result of a + B-c * d.
>>> A = 2
>>> B = 3
>>> C = 4
>>> D = 6
>>> Sum = a + B-c * d
>>> Print sum
-19
3. Calculate the number of minutes and seconds in a week.
>>> Weekdays = 7
>>> Weekdays
7
>>> Days = weekdays * 7
>>> Minutes = days * 60
>>> Seconds = minutes * 60
>>> Minutes
2940
>>> Seconds
176400
4. Three people eat at the restaurant and want to share the meal fee. They spent $35.27 in total and wanted to tip 15%. How Should everyone pay,
Programming implementation
>>> Sum = 35.27
>>> Timeout = 0.15
>>> Cost = sum * (1 + rows)
>>> Percost = cost/3
>>> Percost
13.520166666666666

5. Calculate the area and perimeter of a 12.5 m X m rectangular room
>>> S = 12.5*16.7
>>> S
208.75
>>> L = 12.5*2 + 16.7*2
>>> L
58.4

6. How can I get a decimal result of 9/2?
>>> A = 9
>>> A/= 2
>>>
4
>>> A = 9.0
>>> A/= 2
>>>
4.5

7. python computing 7*7*7*7*7. How many writing methods can be used?
>>> A = 7
>>> B = pow (7, 4)
>>> B
2401
>>> C = 7 ** 4
>>> C
2401
>>> D = 7*7*7*7
>>> D
2401

8. Write a program to convert the temperature from Fahrenheit to Celsius. The conversion formula is C = 5/9 * (F-32)
>>> F = int (raw_input ("Enter the Fahrenheit temperature :"))
Enter the Fahrenheit temperature: 90
>>> C = (F-32)/(9/5)
>>> C
58

9. A mall is selling at a lower price. If the purchase amount is between 50-100 yuan (including 50 yuan and 10% yuan), a discount will be given. If
If the purchased amount is greater than 100 yuan, a 20% discount will be given. Write a program, ask about the purchase price, and then show the discount (10% or 20%) and
Final price.
>>> Money = int (raw_input ("Enter the amount :"))
Enter the amount: 90
>>> If money in range (50,101 ):
... Discount = 0.1
... Finalmoney = money * (1-discount)
... Elif money >=100:
... Discount = 0.2
... Finalmoney = money * (1-discount)
...
>>> Print finalmoney
81.0

>>> Money = int (raw_input ("Enter the amount :"))
Enter the amount: 90
>>> If money in range (50,101 ):
... Discount = 0.1
... Finalmoney = money * (1-discount)
... Elif money >=100:
... Discount = 0.2
... Finalmoney = money * (1-discount)
...
>>> Print finalmoney
81.0
>>> Print discount
0.1

10. Determine whether a number n can be divided by 3 and 5 at the same time.
>>> N = int (raw_input ("determines whether a number n can be divided by 3 and 5 at the same time :"))
Determine whether a number n can be divided by 3 and 5 at the same time: 89
>>> If n % 3 = 0 and n % 5 = 0:
... Print "% s" % "n can divide 3 and 5"
... Else:
... Print "% s" % "n cannot be divided by 3 or 5"
...
N cannot be divided by 3 or 5
>>>
11. Calculate 1 + 2 + 3 + .... + 100
>>> Sum = 0
>>> For I in range (1,101 ):
... Sum + = I
>>> Print sum
5050

12. Exchange the values of two variables

>>> A = 12
>>> B = 34
>>> A = B
>>>
34
>>> B =
>>> A, B = 12, 34
>>> B, a = a, B
>>> B
12
>>>
34
>>>
...
13 A football team is looking for a young girl between the ages of 10 and 12 (including the ages of 10 and 12) to join. Write a program to ask the user's gender (m indicates male,
F indicates female) and age. Then, a message is displayed indicating whether the person can join the team. After 10 queries, the total number of people meeting the conditions is displayed.
>>> I = 0
>>> While I <= 10:
... Sex = raw_input ("please input sex :")
... Age = int (raw_input ("please input age :"))
... I + = 1
... If sex = "female ":
... If age> = 10 and age <= 12:
... Print u "this girl can join the team"
... Sum + = 1
... Elif age <10 and age <12:
... Print u "this girl's age does not meet the requirements"
... Elif sex = "male ":
... Print u "We want gender to be girl"
>>> Print u "% s" % sum for eligible children

14 during a long trip, there was still 200 km between a gas station and the next gas station, and the distance between each gas station was 200 km later. Compile a program
Determine whether to refuel here in sequence, or wait until the next gas station to refuel.
The program asks the following questions:
1) How big is the fuel tank of your car? Unit: litre
2) The amount of remaining oil in the current fuel tank is calculated by percentage. For example, half is 0.5.
3) How far can your car go with oil per litre (km)
Tip:
The fuel tank contains 5 liters of buffer oil to prevent inaccurate oil tables.

>>> Car_gas_volume = 200
>>> Car_current_gas_volume = 150
>>> Car_100_gas_consume = 10
>>> Car_gas_gap_distance = 200
>>> Print u "Please fuel at the % s gas station" % int (car_current_gas_volume-5)/car_100_gas_co
Nsume * 100/car_gas_gap_distance)
Please refuel at 7th Gas Stations

15 existing breads, hot dogs, tomato sauce, mustard sauce, and onions. The number shows how many kinds of ordering combinations are available. The bread must be ordered, 0 is not ordered, and 1 is ordered, for example, 10000,
Indicates that only bread is ordered.
>>> Bread = ["1"]
>>> Hotdog = ["0", "1"]
>>> Mustard = ["0", "1"]
>>> Onion = ["0", "1"]
>>> Combination = []
>>> For B in bread:
... For h in hotdog:
... For t in tomato_jam:
... For m in mustard:
... For o in onion:
... Combination. append (B + h + t + m + o)
...
>>> Print combination
['123', '123', '123', '123', '123', '123', '123', '123', '123'
, '123', '123', '123', '123', '123', '123', '123']

>>> For per_combination in combination:
... Print per_combination
...
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111
16 based on the above question: Give the calories (custom) of each type of food, and then calculate the total calories of each combination

>>> Bread_policlie = 1
>>> Tomato_jam_policlie = 1
>>> Mustard_policlie = 1
>>> Onion_policlie = 1
>>>>>> For per_combination in combination:
... Calorie_count = 0
... For category in per_combination.split ():
... If category [0] = "1 ":
... Calorie_count + = bread_policlie
... If category [1] = "1 ":
... Calorie_count + = tomato_jam_policlie
... If category [2] = "1 ":
... Calorie_count + = mustard_policlie
... If category [3] = "1 ":
... Calorie_count + = onion_policlie
... Print category, "total calories in the combination: % d" % carolie_count, u "qika"
...
The total number of calories in a 10000 combination is: calories.
The total number of calories in a 10001 combination is: calories.
The total number of calories in a 10010 combination is calories.
The total number of calories in a 10011 combination is calories.
The total number of calories in a 10100 combination is calories.
The total number of calories in a 10101 combination is calories.
The total number of calories in a 10110 combination is calories.
The total number of calories in a 10111 combination is calories.
The total number of calories in a 11000 combination is calories.
The total number of calories in a 11001 combination is calories.
The total number of calories in a 11010 combination is calories.
The total number of calories in a 11011 combination is calories.
The total number of calories in a 11100 combination is calories.
The total number of calories in a 11101 combination is calories.
The total number of calories in a 11110 combination is calories.
The total number of calories in a 11111 combination is calories.

17 enter 5 names, sorted and Output
>>> Name = ["Job", "Ruth", "David", "Tom", "Renee"]
>>> Name. sort ()
>>> Name
['David', 'job', 'renee ', 'ruth', 'Tom ']
>>> Sorted (name)
['David', 'job', 'renee ', 'ruth', 'Tom ']
>>> Sorted (name, reverse = true)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> Sorted (name, reverse = True)
['Tom ', 'ruth', 'renee', 'job', 'David']

18. Implement a simple word Base
Function:
You can add words and meanings. When the added word already exists, let the user know;
You can search for words. When a word does not exist, let the user know;
You can delete a word. When the deleted word does not exist, let the user know;
The above functions can be operated without restriction until the user enters bye to exit the program.

>>> Dic = {}
>>> Help = '''
... 1: add a word
... 2: find a word
... 3: delete a word
... 4: say byebye '''
>>> While 1:
... Command = int (raw_input ("please input a command :"))
... If command = 1:
... Word = raw_input ("please input a word what you want to add :")
... If dic. has_key (word ):
... Print "the word has existed! "
... Continue
... Dic [word] = word
... If command = 2:
... Word = raw_input ("please input a word what you want to find :")
... If dic. has_key (word ):
... Print dic [word]
... Continue
... Print "the word is not found! "
... If command = 3:
... Word = raw_input ("please input a word what you want to delete :")
... If dic. has_key (word ):
... Del dic [word]
... Print "deletion is done! "
... Continue
... Print "the word what you want to add is not found! "
... If command = 4:
... Print "say byebye"
... Break
...
Please input a command: 1
Please input a word what you want to add: K
Please input a command: 1
Please input a word what you want to add: K
The word has existed!
Please input a command: 2
Please input a word what you want to find: L
The word is not found!
Please input a command: 3
Please input a word what you want to delete: K
Deletion is done!
Please input a command: 4
Say byebye
19. Enter a positive integer and output the factorial result.
>>> N = int (raw_input ("please input a number, calcuate it's factor :"))
Please input a number, calcuate it's factor: 3
>>> Factor = 1
>>> For I in range (1, n + 1 ):
... Factor = factor * I
...
2
6
20 calculate deposit interest
Four methods are available:
Current period, with an annual interest rate of r1;
One-year fixed interest, with an annual interest rate of r2;
Deposit for the second half-year period, with an annual rate of r3
Two-year fixed interest, with an annual interest rate of r4
The existing Principal is 1000 RMB. Calculate the principal and interest obtained by four methods one year later.
Tip: principal and interest = Principal + principal * annual interest rate * deposit period

>>> R1 = 0.0035
>>> R2 = 0.035
>>> R3 = 0.0155
>>> R4 = 0.04
>>> Deposit = 1000
>>> Print u "total current year earnings: % s" % (deposit * (1 + r1 ))
Total annual current earnings: 1003.5
>>> Print u "one-year cycle total earnings: % s" % (deposit * (1 + r2)
...)
Total income from one-year cycle: 1035.0
>>> Print u "total revenue for the half-year cycle: % s" % (deposit * (1 + r3/2 ))
Total revenue for the half-year period: 1007.75
>>> Print u "two-year cycle total earnings: % s" % (deposit * (1 + r4) ** 2 ))
Total income from the two-year cycle: 1081.6
21
Enter three numbers separated by commas (,) and output the maximum number.
A = [1, 2, 3]

>>> Max ()
3

22. Enter a year and whether the output is a leap year.
Is the condition of a leap year:
The year can be 4 integers but cannot be fully divided by 100, or 400 is a leap year.
>>> Leapyear = int (raw_input ("pleas input leap year :"))
Pleas input leap year: 1984
>>> If leapyear % 4 === 0 and leapyear % 100! = 0:
... Print ("% d is leap year" % leapyear)
... Elif leapyear % 400 = 0:
... Print ("% d is also leap year" % leapyear)
... Else:
... Print ("% d is not leap year" % leapyear)
...
1984 is leap year

23. Calculate the maximum approximate numbers of two positive integers m and n.
>>> A = int (raw_input ("please input A number :"))
Please input A number: 16
>>> 16
16
>>> B = int (raw_input ("please input B number :"))
Please input B number: 24
>>> Su = []
>>> For I in range (1, smaller + 1 ):
... If a % I = 0 and B % I = 0:
... Su. append (I)
...
>>> L = len (su)
>>> P = su L-1]
>>> Su
[1, 2, 4, 8]
>>> P
8

>>> A = int (raw_input ("please input A number :"
Please input A number: 16
>>> 16
16
>>> B = int (raw_input ("please input B number :"))
Please input B number: 24
>>> Su = []
>>> For I in
>>> For I in range (1, bigger + 1 ):
... If a % I = 0 and B % I = 0:
... Su. append (I)
...
>>> Su
[1, 2, 4, 8]
>>> L = len (su)
>>> P = su L-1]
>>> P
8

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.