[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-682215-105":59,"doc-detail-682215-en":130,"doc-similar-682215":149},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","integers-and-floats-python-programming-style-guidance","Integers and floats - Python programming style guidance","","Guidance on Python programming style and common pitfalls, structured around core topics such as representing numeric data as integers versus floats, avoiding equality checks with floating-point values, and applying correct type casting for integer division. It also covers selecting appropriate loop constructs (for vs while) based on iteration counts or boolean conditions, simplifying boolean logic with idiomatic if/else usage, and using docstrings to document intent for new classes and functions.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":30,"@type":76,"position":81},"https://docshare.wps.com/document/technology/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/integers-and-floats-python-programming-style-guidance/682215/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/integers-and-floats-python-programming-style-guidance/682215.png","ImageObject",300,407,{"name":92,"@type":93},"Terk","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-11-18","2026-11-06",true,{"@type":102,"interactionType":103,"userInteractionCount":24},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"Why can comparing floats for equality lead to errors in Python?","Question",{"text":112,"@type":113},"Because many decimal values cannot be represented exactly as floating-point numbers, so computations like incrementing by 0.1 may never equal an expected target such as 0.3.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"When should you use a for loop versus a while loop?",{"text":117,"@type":113},"Use a for loop when the number of iterations is known or described by iterating over elements in an iterable. Use a while loop when the loop depends on a boolean condition or an “until” style stopping rule.",{"name":119,"@type":110,"acceptedAnswer":120},"How can you simplify boolean logic with if/else in Python?",{"text":121,"@type":113},"Avoid redundant checks like comparing to True explicitly. Instead, place the function call directly in the if condition or return the boolean value directly, using not when you need the inverted result.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},682215,1793963004,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":29,"category_name":30,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":24,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":52,"language":139,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":140,"faqs":141,"seo_title":142,"seo_description":67,"update_tm":129,"read_time":143,"preview_page_count":8,"preview_urls":144},1099525198933,"https://ap-avatar.wpscdn.com/davatar_155a257f0dc6eb9ab79c44ca47cae57d","Table of Contents:  \n1. Integers and floats  \n2. for vs. while loops  \n3. Checking boolean conditions with if/else  \n4. Docstrings  \n5. Changing collections while iterating over them  \n6. Directly Accessing Instance Variables  \n7. Calling a superclass constructor from a subclass  \n8. Pitfalls of storing custom objects in data structures  \n9. Which data structure should I use?  \n1. Integers and floats  \nBe careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.  \nFor a detailed explanation of how Python represents floats, read Section 3.4 (pp. 29-31) of your textbook. In short, floats are represented as factors of powers of two, and many decimal numbers cannot be exactly represented in this manner. The most common problem you'll find is trying to check for equality with a floating point number that you are changing. Say that you have a while loop that increments a float by 0 . 1, and you want this whole loop to continue until a control variable equals 0.3. You might find that setting up your loop in the following manner produces an infinite loop:  \nvariable = 0.0  \nwhile variable != 0 .3:  \nvariable += 0.1  \nThat's because 0. 1+0 . 1+0 .1 is not equal to 0.3. 0.1 cannot be precisely represented as a floating point number, so 0. 1+0 . 1+0 .1 = 0.30000000000000004. When you are trying to take simple counts of elements, always use integers to avoid this type of problem.  \nAdditionally, make sure that you typecast between floats and integers at the appropriate time. If you want to do integer division of 3 over 5, writing float(3/5) won't produce the desired result. Remember that Python evaluates inner expressions first, so your code will actually do integer division of 3/5, then cast the result to a float. Instead, you should cast one (or both) of the integers to a float before doing the division-in this case, float(3)/5 or 3/float(5) . You'll usually run into this problem when taking averages or calculating percentages.  \n2. for vs. while loops  \nChoosing the right loop type makes your code more legible and can also help prevent bugs. Everything that can be written with a for loop can be written with a while loop, but while loops can solve some problems that for loops don't address easily. You should usually write for loops  \nwhen possible.  \nIn general, use for loops when you know the number of iterations you need to do-e.g. , 500 trials, one operation per character in a string, or an action on every element in a list. If you can describe the problem you're trying to solve in terms of each or every element in an iterable object, aim for a for loop. Using a for loop when possible will decrease the risk of writing an infinite loop and will generally prevent you from running into errors with incrementing counter variables.  \nExample (print “hello” 500 times):  \nfor i in xrange(500): print 'hello'  \nExample (add 1 to every element in the list): my_list = [5, 2, 7, -4, 0] for i in xrange(len(my_list)):  \nmy_list[i] += 1  \nIf you're instead iterating for a certain condition to be satisfied, you want to use a while loop. While loops are useful when you can define the number of iterations in terms of a boolean variable. If you are waiting for a user to enter an input correctly or are waiting for a randomly generated value to exceed a certain amount, you'll want to use a while loop. Problems that can be described using \"until\" should use while loops.  \nExample (loop until the user enters a positive number):  \nnum = float(raw_input('Enter a positive number: '))  \nwhile num \u003C= 0 .0:  \nnum = float(raw_input('Enter a POSITIVE number: '))  \nExample (loop until the randomly generated number is greater than 0.5): import random  \nnum = random .random()  \nwhile num \u003C= 0 .5:  \nnum = random .random()  \nTo improve the average-case performance of your code, you can sometimes exit out of loops as soon as you find your answer; you'll find many loops that are used to find","cbCaiv1hxZAphNkn","https://ap.wps.com/l/cbCaiv1hxZAphNkn","pdf",660278,"English","# Integers and floats\n## for vs. while loops\n## Checking boolean conditions with if/else\n## Docstrings","[{\"question\":\"Why can comparing floats for equality lead to errors in Python?\",\"answer\":\"Because many decimal values cannot be represented exactly as floating-point numbers, so computations like incrementing by 0.1 may never equal an expected target such as 0.3.\"},{\"question\":\"When should you use a for loop versus a while loop?\",\"answer\":\"Use a for loop when the number of iterations is known or described by iterating over elements in an iterable. Use a while loop when the loop depends on a boolean condition or an “until” style stopping rule.\"},{\"question\":\"How can you simplify boolean logic with if/else in Python?\",\"answer\":\"Avoid redundant checks like comparing to True explicitly. Instead, place the function call directly in the if condition or return the boolean value directly, using not when you need the inverted result.\"}]","Integers and floats - Python programming style guidance | PDF",25,[145],{"page_no":8,"url":146,"width":147,"height":148},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-06/2dfd1c39c029ede9bc11897570b029e7.webp",983,1272,{"list":150,"token":66},[151,157,163,170,176,182,189,195,202,209],{"doc_id":152,"doc_title":153,"file_type":137,"home_preview_img_url":154,"slug":155,"view_count":24,"category_name":30,"create_tm":156},689117,"Basic Python Syntax","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230001514097/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=ZOgvxHso5FXngertDFwT7CHVZrY%3D","basic-python-syntax",1794096795,{"doc_id":158,"doc_title":159,"file_type":137,"home_preview_img_url":160,"slug":161,"view_count":34,"category_name":30,"create_tm":162},670917,"Python 3 Cheat Sheet - concise reference","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229988704071/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=mv2IAFNroHc5C0DpqfV7DPKSook%3D","python-3-cheat-sheet-concise-reference",1793572698,{"doc_id":164,"doc_title":165,"file_type":137,"home_preview_img_url":166,"slug":167,"view_count":168,"category_name":30,"create_tm":169},423014,"THE JAVA LANGUAGE CHEAT SHEET - quick reference","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229922611453/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=rC2m7T0KRaC%2FYl9x2adzXCTpmTg%3D","the-java-language-cheat-sheet-quick-reference",14,1790629749,{"doc_id":171,"doc_title":172,"file_type":137,"home_preview_img_url":173,"slug":174,"view_count":24,"category_name":30,"create_tm":175},670915,"Python Cheat Sheet - Quick Reference","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229988353347/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=Dtgv2IDMeIaUfVBn6OBAzDnFYe8%3D","python-cheat-sheet-quick-reference-670915",1793572692,{"doc_id":177,"doc_title":178,"file_type":137,"home_preview_img_url":179,"slug":180,"view_count":19,"category_name":30,"create_tm":181},697975,"Python Variable Types - Variable Assignment, Multiple Assignment, and Standard Data Types","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146715/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=nTGpabk93AAerCCNolDEoGGaCT8%3D","python-variable-types-variable-assignment-multiple-assignment-and-standard-data-types",1794446554,{"doc_id":183,"doc_title":184,"file_type":137,"home_preview_img_url":185,"slug":186,"view_count":187,"category_name":30,"create_tm":188},347394,"C Programming Language - 2nd Edition (OCR) - Chapter Outline","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229911501529/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=%2BBv09F3D9KBYnjovh466bC3Ncbs%3D","c-programming-language-2nd-edition-ocr-chapter-outline",15,1790066766,{"doc_id":190,"doc_title":191,"file_type":137,"home_preview_img_url":192,"slug":193,"view_count":39,"category_name":30,"create_tm":194},655958,"Software Analysis and Design Tools - Data Flow Diagrams, Structure Charts, HIPO and Structured English","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229974780779/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=XWuP5WCH59ej5thoK2L5b2jgoo0%3D","software-analysis-and-design-tools-data-flow-diagrams-structure-charts-hipo-and-structured-english",1792965096,{"doc_id":196,"doc_title":197,"file_type":137,"home_preview_img_url":198,"slug":199,"view_count":200,"category_name":30,"create_tm":201},115355,"Claude AI Bible - The Complete Guide to Mastering Claude, Claude Code, Claude Cowork, MCP, AI Agents, Automation & Advanced Prompting","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229831893665/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=KH3%2BJJsENHsoFUbUTjX8k6HvtwI%3D","claude-ai-bible-the-complete-guide-to-mastering-claude-claude-code-claude-cowork-mcp-ai-agents-automation-advanced-prompting",945,1785472614,{"doc_id":203,"doc_title":204,"file_type":137,"home_preview_img_url":205,"slug":206,"view_count":207,"category_name":30,"create_tm":208},185,"XNX Universal Transmitter Installation Manual (Complete Guide) [Honeywell Official]","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229693710157/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=WwRRrQ7%2BYCbjCKVUTe0RPjLW698%3D","xnx-universal-transmitter-installation-manual",865,1776135954,{"doc_id":210,"doc_title":211,"file_type":137,"home_preview_img_url":212,"slug":213,"view_count":214,"category_name":30,"create_tm":215},299143,"THE CANDLESTICK TRADING BIBLE - Trading Guide","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229905832418/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796212319&Signature=%2BcOw2GrbBu09QgCdehIXMKHwSz4%3D","the-candlestick-trading-bible-trading-guide",819,1789767270]