Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming
Python Projects for Beginners A Ten Week Bootcamp Approach to Python
In total we can see that the inner loop runs six times and the outer loop runs twice. The value of i only increments when the outer loop runs, which doesn’t occur until the inner loop finishes. The inner loop must count from 0 to 3 each time to run the next iteration on the outer loop. WEDNESDAY EXERCISESUser Input: Write a while loop that continues to ask for user input and runs until they type “quit”. Double Loop: Write a for loop within a while loop that will count from 0 to 5, but when it reaches 3, it sets a game_over variable to True and breaks out of the loop. the while loop should continue to loop until game_over is True. the output should only be 0, 1, 2. today was a bit of a shorter day, as the concept of loops is the same whether it’s a while or for. remember that a while loop is used for conditional looping, while we use a for loop for counting/iterating. Thursday: Working with Lists Now that we’ve learned what lists are and how to use loops, we’re going to go over how to work with lists today. Lists are an important key to any program in Python, so we need to understand our capabilities when using them. To follow along with this lesson, let’s continue from our previous notebook file “Week_04” and simply add a markdown cell at the bottom that says, “Working with Lists.” Checking LengthOften, we’ll need to know how many items are within a list. To do so, we use the len() function: # checking the number of items within a list nums = [5, 10, 15] length = len(nums) # len() returns an integer print(length) Go ahead and run that cell. This will output 3. We use the length function for several uses, whether it’s checking for an empty list or using it within the range function to loop a list. Slicing ListsA few weeks back we talked about slicing a string. Lists work the same way so that you’re able to access specific items. Slicing follows the same arguments as the range function start, stop, step: # accessing specific items of a list with slices print( nums[ 1 : 3 ] ) # will output items in index 1 and 2 print( nums[ : 2 ] ) # will output items in index 0 and 1 print( nums[ : : 2 ] ) # will print every other index - 0, 2, 4, etc. print( nums[ -2 : ] ) # will output the last two items in list Go ahead and run that cell. The outputs are shown in the comments next to each statement. We use bracket notation as if we’re accessing an index; however, we separate the other values via a colon. The order is always [start : stop : step]. By default, start is zero and step is one. You have the option to leave those values out if you’d like to keep the defaults. Using a negative number for the step position will result in slicing backward. If you use a negative number in the start or stop positions, then the slice will either start or stop further from the back. Meaning that if you state -5 as the stop position, it will slice from the start of the list all the way to five elements before the list ends. Download 2,61 Mb. Do'stlaringiz bilan baham: Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024 ma'muriyatiga murojaat qiling |
kiriting | ro'yxatdan o'tish Bosh sahifa юртда тантана Боғда битган Бугун юртда Эшитганлар жилманглар Эшитмадим деманглар битган бодомлар Yangiariq tumani qitish marakazi Raqamli texnologiyalar ilishida muhokamadan tasdiqqa tavsiya tavsiya etilgan iqtisodiyot kafedrasi steiermarkischen landesregierung asarlaringizni yuboring o'zingizning asarlaringizni Iltimos faqat faqat o'zingizning steierm rkischen landesregierung fachabteilung rkischen landesregierung hamshira loyihasi loyihasi mavsum faolyatining oqibatlari asosiy adabiyotlar fakulteti ahborot ahborot havfsizligi havfsizligi kafedrasi fanidan bo’yicha fakulteti iqtisodiyot boshqaruv fakulteti chiqarishda boshqaruv ishlab chiqarishda iqtisodiyot fakultet multiservis tarmoqlari fanidan asosiy Uzbek fanidan mavzulari potok asosidagi multiservis 'aliyyil a'ziym billahil 'aliyyil illaa billahil quvvata illaa falah' deganida Kompyuter savodxonligi bo’yicha mustaqil 'alal falah' Hayya 'alal 'alas soloh Hayya 'alas mavsum boyicha yuklab olish |