{"id":743,"date":"2021-04-29T12:18:17","date_gmt":"2021-04-29T06:48:17","guid":{"rendered":"https:\/\/cbsepython.in\/?p=743"},"modified":"2023-12-28T09:56:12","modified_gmt":"2023-12-28T04:26:12","slug":"for-loop-in-python","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/for-loop-in-python\/","title":{"rendered":"For loop in Python Notes for Class 11 and 12"},"content":{"rendered":"<h3><span style=\"color: #000000;\">For loop in Python<\/span><\/h3>\n<p><span style=\"color: #000000;\">The for statement is used to iterate\/repeat itself over a range of values or a sequence. The for loop is executed for each of these items in the range. These values can be either numeric, or, as we shall see in the successive chapters, elements of a data structure like a string, list, or tuple.<\/span><\/p>\n<p><span style=\"color: #000000;\">With every iteration of the loop, the control variable checks whether each of the values in the range has been traversed or not. When all the items in the range are exhausted, the body of the loop is not executed anymore; the control is then transferred to the statement immediately following the for loop.<\/span><\/p>\n<p><span style=\"color: #000000;\">In for loop, it is known in advance how many times the loop is going to be executed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\"><code>for x in (iteratable object): <\/code><\/span><\/p>\n<p><span style=\"color: #000000;\"><code>#Statement to be executed<\/code><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">The range() function<\/span><\/h3>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">Range([start],stop[,step])<\/span><\/p>\n<table style=\"width: 95.4281%;\">\n<tbody>\n<tr>\n<td style=\"width: 49.3088%;\" width=\"319\"><span style=\"color: #000000;\">Command<\/span><\/td>\n<td style=\"width: 70.8141%;\" width=\"319\"><span style=\"color: #000000;\">Output<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 49.3088%;\" width=\"319\"><span style=\"color: #000000;\">&gt;&gt;&gt;range(10)<\/span><\/td>\n<td style=\"width: 70.8141%;\" width=\"319\"><span style=\"color: #000000;\">[0,1,2,3,4,5,6,7,8,9]<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 49.3088%;\" width=\"319\"><span style=\"color: #000000;\">&gt;&gt;&gt;range(1,11)<\/span><\/td>\n<td style=\"width: 70.8141%;\" width=\"319\"><span style=\"color: #000000;\">[1,2,3,4,5,6,7,8,9,10]<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 49.3088%;\" width=\"319\"><span style=\"color: #000000;\">&gt;&gt;&gt;range(0,10,2)<\/span><\/td>\n<td style=\"width: 70.8141%;\" width=\"319\"><span style=\"color: #000000;\">[0,2,4,6,8]<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 49.3088%;\" width=\"319\"><span style=\"color: #000000;\">&gt;&gt;&gt;range(0,-9,-1)<\/span><\/td>\n<td style=\"width: 70.8141%;\" width=\"319\"><span style=\"color: #000000;\">[0,-1,-2,-3,-4,-5,-6,-7,-8]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"><span style=\"color: #000000;\">for x in range(5):\r\n    print(x)\r\n<\/span><\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\"><code>0<\/code><\/span><br \/>\n<span style=\"color: #000000;\"><code>1<\/code><\/span><br \/>\n<span style=\"color: #000000;\"><code>2<\/code><\/span><br \/>\n<span style=\"color: #000000;\"><code>3<\/code><\/span><br \/>\n<span style=\"color: #000000;\"><code>4<\/code><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"><span style=\"color: #000000;\"># program to display student's marks from record\r\nstudent_name = 'James'\r\nmarks = {'James': 90, 'Jules': 55, 'Arthur': 77}\r\nfor student in marks:\r\n    if student == student_name:\r\n        print(marks[student])\r\n        break\r\nelse:\r\n    print('No entry with that name found.')\r\n<\/span><\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\"><code>90<\/code><\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"><span style=\"color: #000000;\"># Program to iterate through a list using indexing\r\nmySubjects = ['Maths', 'Physics', 'Computer Science']\r\n# iterate over the list using index\r\nfor i in range(len(mySubjects)):\r\n    print(\"I like\", mySubjects[i])\r\n<\/span><\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\"><code>I like Maths<\/code><\/span><br \/>\n<span style=\"color: #000000;\"><code>I like Physics<\/code><\/span><br \/>\n<span style=\"color: #000000;\"><code>I like Computer Science<\/code><\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Also Check: <a style=\"color: #000000;\" href=\"https:\/\/cbsepython.in\/python-programs-for-printing-pyramid-patterns\/\">Programs for printing pyramid patterns<\/a><\/span><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>For loop in Python The for statement is used to iterate\/repeat itself over a range of values or a sequence. The for loop is executed for each of these items in the range. These values can be either numeric, or, as we shall see in the successive chapters, elements of a data structure like a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-743","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/743\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}