Smaltosite.files.wordpress.com



1. Python 3.6.2 (v3.6.2:5fd33b5, Jul ?8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> name="Francis Kebo">>> name[0]'F'>>> name[3]'n'>>> name[len(name)]Traceback (most recent call last):?File "<pyshell#3>", line 1, in <module>??? name[len(name)]IndexError: string index out of range>>> name[len(name) -1]'o'>>> name[-1]'o'>>>Yes, it makes sense.2. Because the position that equals the string’s length results in an error.3. >>> fileList=["mylife.txt", "myprogram.exe", "yourfile.txt"]>>> for fileName in fileList:???if ".txt" in fileName:????? print(fileName)????? mylife.txtyourfile.txt>>>The keyword makes it more simple so that the left operand becomes target substring and the right is the string to be searched.4."""file.binarytodecimal.pyconverte a string of bits to a decimal integer."""bstring= input("Enter a string of bits: ")decimal= 0exponent= len(bstring)-1for digit in bstring:??? decimal= decimal + int(digit) * 2 ** exponent??? exponent= exponent - 1print("The integer value is", decimal)Python 3.6.2 (v3.6.2:5fd33b5, Jul ?8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>>===================== RESTART: C:/Classes/csci115/bi.py =====================Enter a string of bits: 1111The integer value is 15>>>===================== RESTART: C:/Classes/csci115/bi.py =====================Enter a string of bits: 101The integer value is 5>>>We can code an algorithm for the conversion of a binary number to the equivalent decimal number as a Python script.5."""File: decimaltobinary.pyconverte a decimal to a string of bits."""decimal= int(input("Enter a decimal integer: "))if decimal ==0:??? print(0)else:??? print("Quotient Remainder Binary")??? bstring= ""??? while decimal >0:?????? remainder= decimal % 2?????? decimal= decimal // 2?????? bstring= str(remainder)+ bstring?????? print("%5d%8d%12s" % (decimal, remainder, bstring))Python 3.6.2 (v3.6.2:5fd33b5, Jul ?8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>>RESTART: C:/Users/acer/AppData/Local/Programs/Python/Python36-32/decimaltobinary.pyEnter a decimal integer: 34Quotient Remainder Binary??17 ????? 0 ????????? 0??? 8 ????? 1 ???????? 10??? 4 ????? 0 ??????? 010??? 2 ????? 0 ?????? 0010??? 1 ????? 0 ????? 00010??? 0 ????? 1 ???? 100010>>>The script expects a non-negative decimal integer as an input and prints the equivalent bit string. The script checks first for a 0 and prints the string 0 as a special case.????? 6. >>> s= " Hi there, python language!">>> len(s)27>>> s.center(11)' Hi there, python language!'>>> s.count('e')3>>> s.endswith("there!")False>>> s.startswith("hi")False>>> s.find('the')4>>> s.isalpha()False>>> "326".isdigit()True>>> words= s.split()>>> words['Hi', 'there,', 'python', 'language!']>>> "".join(words)'Hithere,pythonlanguage!'>>> " ".join(words)'Hi there, python language!'>>> s.lower()' hi there, python language!'>>> s.upper()' HI THERE, PYTHON LANGUAGE!'>>> s.replace('i', 'o')' Ho there, python language!'>>> "Hi there there, python language!".strip()'Hi there there, python language!'>>>Yes, it makes sense. There is not much difference.7. import randomf= open("integers.txt", 'w')for count in range(500):??? number= random.randint(1, 500)??? f.write(str(number)+ "\n")f.close()Nothing is really happening. Python 3.6.2 (v3.6.2:5fd33b5, Jul ?8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.===================== RESTART: C:\Classes\csci115\143.py =====================>>>8.f = open ("integers.txt", 'r')sum= 0for line in f:??? wordlist= line.strip()??? for word in wordlist:?????? number= int(line)?????? sum+= numberprint("The sum is", sum)Python 3.6.2 (v3.6.2:5fd33b5, Jul ?8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>>==================== RESTART: C:/Classes/csci115/okay.py ====================The sum is 366279>>> ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download