Python learning the first module exercises

Source: Internet
Author: User
Tags logical operators

#!/usr/bin/python
#-*-Coding:utf-8-*-

# # # First set of questions
# # 1. Explain the meaning of "=", "= =", "+ =" respectively (oral)
# = is an assignment statement, assigning one variable value to another
# = = for conditional judgment, determine whether two values are equal
# + = is an assignment meaning a+=1 is a=a+1

# 2.
# Two variable value relationships? Oral
#-N1 = 123456
#-n2 = N1
# N1 gives a value of 123456 and then N1 address store 123456 memory address to N2 meaning N1 n2 points to 123456 this address.

# 3. Please write down the number of digits (oral) used by Utf-8 and GBK respectively for the "Road Flying Learning City"
# Utf-8 accounted for 12 bytes gbk accounted for 8 bytes 12 bytes equivalent to 96 bits 8 bytes 64 bits

# 4. Brief description of several data types in Python (dictate)
# data types are divided into: basic types, Datasets.
# Basic types include: numeric types (shaping, long integer, float, plural), String type, Boolean type.
# Data Set types include: List, Ganso, Dictionary, collection.
#
# 5. What are the variable and immutable data types? Oral
# mutable: String, List, mutable collection, dictionary value
# Immutable: Numbers, strings, Booleans, Ganso, dictionary keys, immutable collections
#
# 6.
# meta-fathers, lists, dictionaries have no length limits? (oral)
# No, defined according to memory size.
#
# 7.
# list [' Alex ', ' Egon ', ' Yuan ', ' wusir ', ' 666 '] (programming)
# info = [' Alex ', ' Egon ', ' Yuan ', ' wusir ', ' 666 ']
#-1. Replace 666 with 999
# Info[-1] = ' 999 '
# Print (info)

#-2. Get the "Yuan" index
# Print (Info.index (' Yuan '))

#-3. Assuming that there are several elements ahead, the Shard gets the last three elements
# print (info[-3:])

# 8. Split the string "www.luffycity.com" into a list: li=[' www ', ' luffycity ', ' com ' (programming)
# li= ' www.luffycity.com '
# Print (Li.split ('. '))

# # 9. To add and revise a dictionary (programming)
#-{"Development": "Development Brother", "Op": "Ops", "Operate": "Operation Fairy", "UI": "UI Fairy"}
# dic={"Development": "Development Little Brother", "Op": "Ops brother", "Operate": "Operation Fairy", "UI": "UI Fairy"}
# #增加
# dic[' cleaning ']= ' auntie '
# Print (DIC)
# #删除
# del dic[' sweep ')
# Print (DIC)
# # Change
# dic[' UI ']= ' UI '
# Print (DIC)
# #查
# Print (Dic.get (' UI '))

# 10. Calculating 1+2+3...+98+99+100 (Programming questions)
# a = range (100)
# count = 0
# for I in A:
# count = Count+i
# Print (count)

# 11. Create Fun template programs (programming questions)
# Requirements: Waiting for the user to enter a name, place, hobby, according to the user's name and hobbies to carry out arbitrary reality
# such as: Beloved cute xxx, favorite in XXX place dry xxx

# NAME = input (' Input name: ')
# place = input (' Input location: ')
# likes = input (' Input hobby: ')
# print (' beloved lovely%s, favorite%s '% dry '% (name,place,likes))

# 12. Write a three-time authentication (programming)
# 1. Implement user input user name and password, when the user name is seven or Alex and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
# name = ' Senven '
# name1 = ' Alex '
# password = ' 123 '
# count=0
# While Count < 3:
# name_ = input ("Please enter user name:")
# password_= input ("Please enter password:")
# if (name = = Name_ or name1 = = name_) and Password_ = = password:
# print (' Login successful ')
# break
# Else:
# count + = 1
# print (' Login failed%s times, more than 3 times '% ' (count))


# # # Second set of questions
# # 1. Explain ' and ', ' or ', ' not ' (dictate)
# and C voice are the same as or not, and,or,not are logical operators
# and means true if both are true
# or indicates that both have a Boolean true and the result is true and the reverse is false
# not indicates an inverse such as x is True,not X is False

# 2. The difference between tuples and lists (dictate)
# tuples are also called read-only lists, immutable, and unordered.
# The list is ordered and can be modified.

# 3. Enumerate a bunch of cases where the Boolean value is False (dictate)
# [],{},none,false, ', digital 0,-0
# Print (bool ([]))

# 4. The difference between Python2 and Python3 for long (longer shaping).
# there are python2 int and Long,int 32-bit systems with a maximum of 32 bits and a long beyond 32 bits. The 64-bit system is 64-bit.
# All of the python3 are int.
# long does not refer to the positioning width in python.

# 5. Ganso and List differences (dictate)
# tuples are also called read-only lists, immutable, and unordered.
# The list is ordered and can be modified.

# 6. Variable naming conventions? Oral
# The first letter cannot be a number, and the variable name can only contain English, numerals, and underscores _

# # 7. Explain the differences between Unicode and utf-8 (or the role of Unicode in Python3) (dictate)
# Unicode is also called the Universal Code, covering all the world's encodings, all characters and symbols are represented by a minimum of 16 bits (2 bytes), and other encodings have a corresponding mapping relationship.
# Utf-8 is optimized for Unicode, uft-8 occupies only 1 bytes for English, 2 bytes for European characters, 3 bytes for East Asian characters, and 4 bytes for special characters.
# in Python3 the default in memory storage code is Unicode, in order to facilitate people in different countries to view the code.

# 8. Memory Space Assessment. Oral
#
#-Ask if the ID of info changed before and after the change
#-Ask for changes before and after info[' Alex ' [' Hobbies ']
# Does the ID change
#
# info = {
# ' Egon ': {' pwd ': ' 123 ', ' hobbies ': [' play ', ' music ', ' read '},
# ' Alex ': {' pwd ': ' 1234 ', ' hobbies ': [' music ', ' read '},
# ' Wupeiqi ': {' pwd ': ' 135 ', ' hobbies ': [' read ',]},
# }
# info[' Alex ' [' Hobbies '].append (' play ')

# The memory address will not change.

# 9. Cut the string "luffycity" to "Luffy", "City" (programming)

# info= ' luffycity '
# print (Info[0:5])
# print (info[5:])

# 10. Guessing Age games (programming questions)

# age = 26
# while 1:
# user_guess = input ("Guess Age:")
# if user_guess.isdigit () = = True:
# if Age = = Int (user_guess):
# print ("Congratulations! Guess right ")
# break
# elif Int (user_guess) > Age:
# print ("Guess Big, try again")
# Else:
# print ("Guess small, try again")

# 11. 1~100 all the even-numbered and (also odd and, using while Loop writes) between the two (programming questions).

# count1 = Count = 0
# while Count < 100:
# count+= 1
# if count%2 = = 0:
# count1 = count1 + count
# Print (COUNT1)

# 12. Receiving a 100-point score from the keyboard (0~100) requires the output of its corresponding grade a~e. Among them, 90 points above is ' A ', 80~89 is divided into ' B ', 70~79 is divided into ' C ', 60~69 is divided into ' D ', and 60 points below is ' E '. (Programming Questions)

# score = Int (input (' Enter score: '))
# If score >= 90:
# print ("score is a")
# elif score >= 80:
# print ("score is B")
# elif score >= 70:
# print ("score is C")
# elif score >= 60:
# print ("score is D")
# Else:
# print ("score is E")

# # # Third set of questions

# # 1. Difference between "is" and "= =" (oral)

# is to compare the address, = = is the ratio.
# is to determine if a variable is from the same object at the same address.
# = = Determines whether the value is equal.

# 2. What are the considerations for declaring variables? Oral

# Note the rules for variable definitions:
# 1. Variable names can only be any combination of letters, numbers, or underscores.
# 2. The first character of a variable name cannot be a number.
# 3. The keyword cannot be declared as a variable name.
# variable names should not be too long, it is best not to use Chinese and pinyin, there can not be confused variable names, such as the name does not mean.


# 3. Depth copy-Reference and copy (), deepcopy () differences (dictate)

# 1. Reference assignment, point to the same list address, if a list is re-assigned to another list, B still points to the previous address, B list does not change
# If the values in the A list change, then B will change as well.
# 2. When using copy (), the equivalent of the entire list includes the contents of the complete copy to you, not to the same address,
# a The value changes in a, B list does not follow the change. However, if the a list is a nested list, the values in the nested lists change, and the nested in the copy list B
# The list will also change, because the list in the table is pointing to the same address, which is equivalent to 1.
# 3. If you use Deepcopy (), the number of addresses in the list will be different, so change the number of nested lists in the table A and B will not change.

# 4. Each data type is ordered separately? Or is it disorderly? Oral

# Ordered: list, tuple, string
# unordered: Dictionaries, collections

# 5. Enter a year to determine whether the year is a leap years and output the results. (The program can be reused using while True) (programming questions)
# Note: A year in which one of the following two conditions is met is a leap years. (1) can be divisible by 4 but not divisible by 100. (2) can be divisible by 400.

# while True:
# year = input ("Enter years to determine if leap age: (Enter Q exit)")
# if year.isdigit () = = True:
# if (int (year)%4 = = 0 and int (year)%100! = 0) or (int (year)%400 = = 0):
# Print ("You entered a leap year:%s"% (year))
# Else:
# print ("You are not entering a leap year")
# elif Year = = ' Q ':
# break

# 6. Has the following string: N = "Luke City" (programming question)
#
#-Converts a string into a utf-8 character encoding and converts the converted bytes to the Utf-8 character encoding
#-Converts a string into a GBK character encoding and converts the converted bytes to the Utf-8 character encoding

# n = "lu Fei learning City"
# n1 = N.encode (' Utf-8 ')
# Print (N1)
# n = n1.decode (' Utf-8 ')
# print (n)
# N2 = N.encode (' GBK ')
# Print (N2)
# n = n2.decode (' GBK '). Encode (' Utf-8 ')
# print (n)

# # 7. Connect each element in the list [' Alex ', ' Steven ', ' Egon '] to a string using ' \_ ' (programming)
# info=[' Alex ', ' Steven ', ' Egon ']
# s1= ' \_ '
# Print (S1.join (info))

# 8. Custom two, and seek the intersection, the collection, the difference set. Programming
# a = {1, 2, 3, 4, 5}
# b = {2, 4, 6, 8, 10}
# print (A & B)
# print (A | b)
# print (A-B)

# 9. All numbers within 100 that cannot be divisible by 3 are placed in the list sum3=[] and the sums and averages of these numbers are calculated. (when calculating, tell the learner that sum is a keyword and cannot be used as a variable) (programmed)
# info = range (100)
# sum3 = []
# for I in info:
# if I% 3 = = 0:
# sum3.append (i)
# count = 0
# for I in SUM3:
# count = Count + I
# Print (SUM3)
# print ("sum:%s"% (count))

# 10. Print the following form according to the contents of the Dictionary (programming)
# You can not print the effect of neat alignment, mainly for students to get key and value.
# {"Development": "Development Little Brother", "Op": "Ops brother", "Operate": "Operation Fairy", "UI": "UI Fairy"}
# The effect is as follows:
# 1. The UI was removed
# 2. Modified the operate
#
# ```
# Development---Development small brother
# OP---ops little brother
# Operate---Little Fairy
# ```

# info = {"Development": "Development Little Brother", "Op": "Ops brother", "Operate": "Operation Fairy", "UI": "UI Fairy"}
# del info[' UI ')
# info[' Operate ']= ' little Fairies '
# for k in info:
# print (K,info[k])

# 11. Assuming that the one-year periodic interest rate is 3.25%, to calculate the need for too many teenagers, 10,000 yuan of one-year fixed deposit can double the coupon? (Programming Questions)
# money = 10000
# interest = Money
# count = 0
# while True:
# count + = 1
# interest = interest * (1 + 3.25/100)
# if interest//money = = 2:
# Print (count)
# break

# 12.
# The list is added to the search (can be indexed, whether the value exists in the list). Programming
#
#-[' Alex ', ' Egon ', ' Wusir ', ' Wen ', ' Jing ']

# info = [' Alex ', ' Egon ', ' Wusir ', ' Wen ', ' Jing ')
# #增加
# info.append (' Hessen ')
# Print (info)
# #删除
# info.remove (' Wen ')
# Print (info)
# del Info[-1]
# Print (info)
# #修改
# info[1]= ' Hessen '
# Print (info)
# #查找
# Print (Info.index (' Hessen '))

# # # Remaining questions
# 1. Guessing games. Preset an integer between 0~9, let the user guess and enter the number of guesses, if the number is greater than the preset, the display is "too big", less than the preset number, the display "too small", so loop, until the number of guesses, show "Congratulations!" You guessed it! (Programming Questions)
# number = 8
# while 1:
# user_guess = input ("Guess Age:")
# if user_guess.isdigit () = = True:
# if number = = Int (user_guess):
# print ("Congratulations! You guessed it! ")
# break
# elif Int (user_guess) > Number:
# print ("Too big")
# Else:
# print ("Too small")

# 2. Write a Python program, enter two numbers, compare their sizes, and output the larger of them. (Programming Questions)
# print ("Enter two number comparison size")
# info = []
# while Len (info) < 2:
# number = input (' Input: ')
# if number.isdigit () = = False:
# Print ("Input error, please enter Number")
# continue
# Else:
# info.append (number)
# Maxnumber=int (Info[0])
# for I in info:
# if int (i) > MaxNumber:
# maxnumber = Int (i)
# print ("%s larger"% (MaxNumber))
#

# 3. There are four numbers: 1, 2, 3, 4, how many three digits can be made up of each other without repeating numbers? What's the number? (Programming Questions)
# program Analysis: Can be filled in hundreds, 10 digits, single digit numbers are 1, 2, 3, 4. Make all the permutations and then remove the permutations that do not meet the criteria.
# program Source code:
#
# n = [1,2,3,4]
# n1 = []
# i = j = k = 0
# count = 0
# While I < 4:
# N2 = n[i]*100
# i+=1
# while J<4:
# n3 = n2+n[j]*10
# j+=1
# while K<4:
# n1.append (N3+n[k])
# k+=1
# Else:
# k=0
# Else:
# j=0
# n2=n1.copy ()
# for Index,i in Enumerate (N1):
# a = n1[index]//100
# a1 = N1[index]%100//10
# a3 = n1[index]%10
# If A = = A1 or a = = a3 or A1 = = A3:
# N2.remove (i)
# print (Len (n2), N2)

# 4.
# # A company uses a public telephone to pass data, the data is a four-bit integer, is encrypted during delivery, the encryption rules are as follows: Each digit is added with 5, and then the remainder is replaced by 10, then the first and fourth bits are exchanged, the second and third bits are exchanged. (Programming Questions)
# data = input ("Input 4-digit:")
# data = List (". Join (data)")
# for Index,i in Enumerate (data):
# Data[index] = (int (i) + 5)% 10
# Print (data)
# Data.reverse ()
# Print (data)

#
# 5. The number of primes within 100 is calculated. (Programming Questions)
# n1 = List (range (2,100))
# n2 =[]
# i = 0
# count = 0
# count1 = 0
# While Count < 98:
# if n1[i]% n1[count] = = 0:
# count1+= 1
# if Count = = and Count1 = = 1:
# n2.append (N1[i])
# count+= 1
# if Count = = 98:
# i+= 1
# Count1 =count = 0
# If I = = 98:
# break
# Print (sum (n2))



# 6. Binary, decimal, hexadecimal conversion (computational problems, let learners use calculators, do not use the built-in method of programming)
# decimal into binary: Bin ()
# decimal to 16 binary: Hex ()
# Decimal to octal: Oct ()
# binary to 10 binary: 1010: equivalent to 8+2=10
# hexadecimal to binary: such as 0x9a: equivalent to 10011010 and then 10 binary is the corresponding
# 1 1 1 1 1 1 1 1
# 128 64 32 16 8 4 2 1 10011010=128+16+8+2=154

# 7. Default character encoding for Python2 and Python3 (dictate)
# python2 Default character encoding is ASCII
# python3 default encoding is Unicode

# 8. Floating-point conversion shaping (oral)
# Remove the number after the decimal point and take an integer.

# 9. Calculates the length of the "21days" string. (Dictation/programming)
# Print (Len (' 21days '))

# 10. Ganso, list, dictionary there is no length limit? (oral)
# no

# 11. Properties of the collection (dictate)
# deterministic: The element must be hash. Cross-specific: elements are not identical. Disordered nature.

# 12. The bonuses awarded by the Enterprise are based on the profit percentage. If the profit (I) is less than or equal to $100,000, the bonus may be raised by 10%;
# profit above 100,000 yuan, less than 200,000 yuan, less than 100,000 yuan in part by 10% commission, higher than 100,000 yuan portion, can commission 7.5%;
# 200,000 to 400,000, above the portion of 200,000 yuan, can commission 5%, 400,000 to 600,000 is higher than the portion of 400,000 yuan, can commission 3%;
# 600,000 to 1 million, higher than 600,000 yuan portion, can commission 1.5%, higher than 1 million yuan, the portion of more than 1 million yuan by 1% Commission,
# Enter the current month profit I from the keyboard, the total bonus should be issued? Programming
# while True:
# money = input (' Profit: ')
# if money.isdigit () = = True:
# if INT (money) <= 100000:
# Print ("Bonus:%s"% (int (money) *10/100))
# elif Int (money) <= 200000:
# Print ("Bonus:%s"% (((int (money)-100000) *7.5/100) +100000*10/100))
# elif Int (money) <= 400000:
# Print ("Bonus:%s"% (((int (money)-200000) * 5/100)))
# elif Int (money) <= 600000:
# Print ("Bonus:%s"% (((int (money)-400000) * 3/100)))
# elif Int (money) <= 1000000:
# Print ("Bonus:%s"% (((int (money)-600000) * 1.5/100)))
# Else:
# Print ("Bonus:%s"% (((int (money)-600000) * 1/100)))
# break
# Else:
# money = input (' input error, please re-enter: ')

Python learning the first module exercises

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.