<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8253077814651020859</id><updated>2026-04-14T18:31:24.690+05:30</updated><category term="python example"/><category term="python tutorial"/><category term="learn python"/><category term="list"/><category term="hackerrank-solution"/><category term="basic"/><category term="Error"/><category term="Google Kick Start"/><category term="Techgig code"/><category term="array"/><category term="python coding challenge"/><category term="Build with me"/><category term="Interviews problems"/><category term="python project"/><category term="blog"/><category term="Do Here Your First Step to Struts Using Netbeans"/><category term="General"/><category term="Java"/><category term="news"/><category term="programming"/><title type='text'>Docodehere</title><subtitle type='html'>DoCodeHere is your all-in-one programming resource hub — explore tutorials for every programming language, online compilers, coding tools, and practical guides to improve your coding skills and problem-solving....</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://www.docodehere.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>bharat ranpariya</name><uri>http://www.blogger.com/profile/10752465129162350604</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>153</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-496403847119922696</id><published>2025-11-19T16:18:00.007+05:30</published><updated>2025-11-19T16:41:26.867+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="news"/><title type='text'>TikTok Introduces New Controls to Manage AI-Generated Content in the For You Feed</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;TikTok has introduced a major update that gives users direct control over how much AI-generated content they want to see in their For You feed. This may seem like a small tweak, but it represents a big step in how social media platforms are adapting to the rise of artificial intelligence in online creativity.&lt;/div&gt;&lt;p data-end=&quot;1259&quot; data-start=&quot;776&quot;&gt;In recent years, AI tools have made it incredibly easy for anyone to create videos, animations, characters, and even entire stories with little to no technical skills. Platforms like TikTok have naturally become hotspots for such content. But with this growth also comes concern—some users love AI videos, while others prefer original human-created content. TikTok’s latest update tries to strike the perfect balance by letting people customize their feed based on their preferences.&lt;/p&gt;&lt;p data-end=&quot;1450&quot; data-start=&quot;1261&quot;&gt;Below, we break down everything you need to know about this update, why TikTok is doing it, how invisible watermarking works, and what this means for the future of digital content creation.&lt;/p&gt;&lt;hr data-end=&quot;1455&quot; data-start=&quot;1452&quot; /&gt;&lt;h2 data-end=&quot;1510&quot; data-start=&quot;1457&quot;&gt;&lt;strong data-end=&quot;1510&quot; data-start=&quot;1460&quot;&gt;A New Way to Personalize the TikTok Experience&lt;/strong&gt;&lt;/h2&gt;&lt;p data-end=&quot;1901&quot; data-start=&quot;1512&quot;&gt;TikTok’s new &lt;strong data-end=&quot;1564&quot; data-start=&quot;1525&quot;&gt;AI-Generated Content (AIGC) control&lt;/strong&gt; is integrated into the “Manage Topics” section within the Content Preferences menu. This section already allows users to fine-tune the frequency of different categories they see—such as sports, dance, food, entertainment, lifestyle, and more. Now, AI-generated videos are treated as a distinct category with their own adjustable slider.&lt;/p&gt;&lt;h3 data-end=&quot;1931&quot; data-start=&quot;1903&quot;&gt;&lt;strong data-end=&quot;1931&quot; data-start=&quot;1907&quot;&gt;How the Slider Works&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;2057&quot; data-start=&quot;1932&quot;&gt;After opening:&lt;br /&gt;
&lt;strong data-end=&quot;1997&quot; data-start=&quot;1947&quot;&gt;Settings → Content Preferences → Manage Topics&lt;/strong&gt;,&lt;br data-end=&quot;2001&quot; data-start=&quot;1998&quot; /&gt;
users will find a slider labeled “AI-generated content.”&lt;/p&gt;&lt;p data-end=&quot;2090&quot; data-start=&quot;2059&quot;&gt;Depending on the user’s choice:&lt;/p&gt;&lt;ul data-end=&quot;2267&quot; data-start=&quot;2091&quot;&gt;
&lt;li data-end=&quot;2143&quot; data-start=&quot;2091&quot;&gt;
&lt;p data-end=&quot;2143&quot; data-start=&quot;2093&quot;&gt;Moving the slider down means &lt;strong data-end=&quot;2141&quot; data-start=&quot;2122&quot;&gt;less AI content&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;2186&quot; data-start=&quot;2144&quot;&gt;
&lt;p data-end=&quot;2186&quot; data-start=&quot;2146&quot;&gt;Moving it up means &lt;strong data-end=&quot;2184&quot; data-start=&quot;2165&quot;&gt;more AI content&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;2267&quot; data-start=&quot;2187&quot;&gt;
&lt;p data-end=&quot;2267&quot; data-start=&quot;2189&quot;&gt;Keeping it balanced means a mixture of both human-made and AI-generated videos&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p data-end=&quot;2417&quot; data-start=&quot;2269&quot;&gt;This helps users control how much exposure they have to AI visuals, which have become extremely common since the rise of advanced generative models.&lt;/p&gt;&lt;hr data-end=&quot;2422&quot; data-start=&quot;2419&quot; /&gt;&lt;h2 data-end=&quot;2473&quot; data-start=&quot;2424&quot;&gt;&lt;strong data-end=&quot;2473&quot; data-start=&quot;2427&quot;&gt;Why TikTok is Introducing This Feature Now&lt;/strong&gt;&lt;/h2&gt;&lt;p data-end=&quot;2622&quot; data-start=&quot;2475&quot;&gt;This update comes at a time when AI-generated videos are more popular than ever. Major tech companies are experimenting with AI-only content feeds:&lt;/p&gt;&lt;ul data-end=&quot;2835&quot; data-start=&quot;2624&quot;&gt;
&lt;li data-end=&quot;2713&quot; data-start=&quot;2624&quot;&gt;
&lt;p data-end=&quot;2713&quot; data-start=&quot;2626&quot;&gt;&lt;strong data-end=&quot;2649&quot; data-start=&quot;2626&quot;&gt;Meta launched Vibes&lt;/strong&gt;, a dedicated feed filled entirely with AI-created short videos.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;2835&quot; data-start=&quot;2714&quot;&gt;
&lt;p data-end=&quot;2835&quot; data-start=&quot;2716&quot;&gt;&lt;strong data-end=&quot;2740&quot; data-start=&quot;2716&quot;&gt;OpenAI launched Sora&lt;/strong&gt;, a platform allowing people to generate highly realistic videos using artificial intelligence.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p data-end=&quot;3076&quot; data-start=&quot;2837&quot;&gt;These platforms have inspired creators to experiment more with AI. Many TikTok users now post AI-generated scenes involving historical events, imaginary scenarios, celebrity mashups, futuristic cities, and even animated storytelling clips.&lt;/p&gt;&lt;p data-end=&quot;3332&quot; data-start=&quot;3078&quot;&gt;While these videos can be fun and visually impressive, some users feel overwhelmed by the amount of AI content and prefer authentic videos recorded by real people. TikTok’s new control aims to strike a healthy balance between creativity and authenticity.&lt;/p&gt;&lt;hr data-end=&quot;3337&quot; data-start=&quot;3334&quot; /&gt;&lt;h2 data-end=&quot;3402&quot; data-start=&quot;3339&quot;&gt;&lt;strong data-end=&quot;3402&quot; data-start=&quot;3342&quot;&gt;Ensuring Transparency: Invisible Watermarking Technology&lt;/strong&gt;&lt;/h2&gt;&lt;p data-end=&quot;3715&quot; data-start=&quot;3404&quot;&gt;One of the biggest challenges TikTok faces is making sure that users can clearly differentiate between videos created by humans and videos generated by AI tools. TikTok already uses &lt;strong data-end=&quot;3614&quot; data-start=&quot;3586&quot;&gt;C2PA Content Credentials&lt;/strong&gt;, a global standard that adds digital metadata indicating whether a piece of content is AI-generated.&lt;/p&gt;&lt;p data-end=&quot;3851&quot; data-start=&quot;3717&quot;&gt;However, there’s a catch:&lt;br data-end=&quot;3745&quot; data-start=&quot;3742&quot; /&gt;
Metadata can be lost or removed if someone downloads, edits, or reuploads the content on another platform.&lt;/p&gt;&lt;p data-end=&quot;3934&quot; data-start=&quot;3853&quot;&gt;To solve this, TikTok is testing a more robust system—&lt;strong data-end=&quot;3933&quot; data-start=&quot;3907&quot;&gt;invisible watermarking&lt;/strong&gt;.&lt;/p&gt;&lt;h3 data-end=&quot;3975&quot; data-start=&quot;3936&quot;&gt;&lt;strong data-end=&quot;3975&quot; data-start=&quot;3940&quot;&gt;What Is Invisible Watermarking?&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;4178&quot; data-start=&quot;3976&quot;&gt;Invisible watermarking embeds a hidden, unremovable signature inside the video. Users cannot see it with the naked eye, but TikTok’s systems can instantly identify whether the video was created with AI.&lt;/p&gt;&lt;p data-end=&quot;4191&quot; data-start=&quot;4180&quot;&gt;This means:&lt;/p&gt;&lt;ul data-end=&quot;4397&quot; data-start=&quot;4192&quot;&gt;
&lt;li data-end=&quot;4265&quot; data-start=&quot;4192&quot;&gt;
&lt;p data-end=&quot;4265&quot; data-start=&quot;4194&quot;&gt;AI-generated videos uploaded from other apps cannot hide their origin&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4336&quot; data-start=&quot;4266&quot;&gt;
&lt;p data-end=&quot;4336&quot; data-start=&quot;4268&quot;&gt;Content creators cannot remove the label by editing or reuploading&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4397&quot; data-start=&quot;4337&quot;&gt;
&lt;p data-end=&quot;4397&quot; data-start=&quot;4339&quot;&gt;The platform can keep track of AI videos more accurately&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p data-end=&quot;4475&quot; data-start=&quot;4399&quot;&gt;This is crucial for transparency, misinformation prevention, and user trust.&lt;/p&gt;&lt;p data-end=&quot;4525&quot; data-start=&quot;4477&quot;&gt;TikTok will first apply invisible watermarks to:&lt;/p&gt;&lt;ul data-end=&quot;4646&quot; data-start=&quot;4526&quot;&gt;
&lt;li data-end=&quot;4590&quot; data-start=&quot;4526&quot;&gt;
&lt;p data-end=&quot;4590&quot; data-start=&quot;4528&quot;&gt;Videos created using its own tools (e.g., &lt;strong data-end=&quot;4587&quot; data-start=&quot;4570&quot;&gt;AI Editor Pro&lt;/strong&gt;)&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4646&quot; data-start=&quot;4591&quot;&gt;
&lt;p data-end=&quot;4646&quot; data-start=&quot;4593&quot;&gt;Videos uploaded with C2PA metadata already attached&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p data-end=&quot;4736&quot; data-start=&quot;4648&quot;&gt;Over time, TikTok may expand this watermarking system to more tools and content formats.&lt;/p&gt;&lt;hr data-end=&quot;4741&quot; data-start=&quot;4738&quot; /&gt;&lt;h2 data-end=&quot;4803&quot; data-start=&quot;4743&quot;&gt;&lt;strong data-end=&quot;4803&quot; data-start=&quot;4746&quot;&gt;AI Literacy Fund: Helping People Understand AI Safely&lt;/strong&gt;&lt;/h2&gt;&lt;p data-end=&quot;5025&quot; data-start=&quot;4805&quot;&gt;To support responsible and informed use of artificial intelligence, TikTok has also announced a &lt;strong data-end=&quot;4932&quot; data-start=&quot;4901&quot;&gt;$2 million AI literacy fund&lt;/strong&gt;. This initiative will work with experts, educators, and nonprofits to help users understand:&lt;/p&gt;&lt;ul data-end=&quot;5183&quot; data-start=&quot;5026&quot;&gt;
&lt;li data-end=&quot;5042&quot; data-start=&quot;5026&quot;&gt;
&lt;p data-end=&quot;5042&quot; data-start=&quot;5028&quot;&gt;How AI works&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;5083&quot; data-start=&quot;5043&quot;&gt;
&lt;p data-end=&quot;5083&quot; data-start=&quot;5045&quot;&gt;How to identify AI-generated content&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;5131&quot; data-start=&quot;5084&quot;&gt;
&lt;p data-end=&quot;5131&quot; data-start=&quot;5086&quot;&gt;How to stay safe while consuming AI visuals&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;5183&quot; data-start=&quot;5132&quot;&gt;
&lt;p data-end=&quot;5183&quot; data-start=&quot;5134&quot;&gt;How to critically evaluate what they see online&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p data-end=&quot;5458&quot; data-start=&quot;5185&quot;&gt;One of the first confirmed partners is &lt;strong data-end=&quot;5242&quot; data-start=&quot;5224&quot;&gt;Girls Who Code&lt;/strong&gt;, a nonprofit dedicated to empowering the next generation of young women in tech. TikTok hopes this initiative will help build a more informed online community as AI becomes increasingly integrated into social media.&lt;/p&gt;&lt;hr data-end=&quot;5463&quot; data-start=&quot;5460&quot; /&gt;&lt;h2 data-end=&quot;5518&quot; data-start=&quot;5465&quot;&gt;&lt;strong data-end=&quot;5518&quot; data-start=&quot;5468&quot;&gt;How This Update Benefits Everyday TikTok Users&lt;/strong&gt;&lt;/h2&gt;&lt;h3 data-end=&quot;5558&quot; data-start=&quot;5520&quot;&gt;&lt;strong data-end=&quot;5558&quot; data-start=&quot;5524&quot;&gt;1. More Control Over Your Feed&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;5782&quot; data-start=&quot;5559&quot;&gt;For users who find AI videos repetitive, unrealistic, or overwhelming, the new slider offers a simple way to reduce them. For others who enjoy AI creativity, effects, and storytelling, it&#39;s an opportunity to see more of it.&lt;/p&gt;&lt;h3 data-end=&quot;5814&quot; data-start=&quot;5784&quot;&gt;&lt;strong data-end=&quot;5814&quot; data-start=&quot;5788&quot;&gt;2. Better Transparency&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;5972&quot; data-start=&quot;5815&quot;&gt;Invisible watermarking ensures that videos made with AI tools are accurately identified, reducing confusion and preventing misleading content from spreading.&lt;/p&gt;&lt;h3 data-end=&quot;6025&quot; data-start=&quot;5974&quot;&gt;&lt;strong data-end=&quot;6025&quot; data-start=&quot;5978&quot;&gt;3. Better Protection Against Misinformation&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;6194&quot; data-start=&quot;6026&quot;&gt;AI-generated videos sometimes look extremely realistic. Clear labeling helps viewers avoid being deceived by fake events, fake celebrity videos, or misleading AI edits.&lt;/p&gt;&lt;h3 data-end=&quot;6232&quot; data-start=&quot;6196&quot;&gt;&lt;strong data-end=&quot;6232&quot; data-start=&quot;6200&quot;&gt;4. Supports Creative Freedom&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;6394&quot; data-start=&quot;6233&quot;&gt;Instead of banning or restricting AI content, TikTok is giving users the power to decide what they want to see. This supports creativity while maintaining trust.&lt;/p&gt;&lt;h3 data-end=&quot;6430&quot; data-start=&quot;6396&quot;&gt;&lt;strong data-end=&quot;6430&quot; data-start=&quot;6400&quot;&gt;5. Education and Awareness&lt;/strong&gt;&lt;/h3&gt;&lt;p data-end=&quot;6559&quot; data-start=&quot;6431&quot;&gt;The AI literacy fund ensures that users—especially young audiences—learn the importance of digital safety and critical thinking.&lt;/p&gt;&lt;hr data-end=&quot;6564&quot; data-start=&quot;6561&quot; /&gt;&lt;h2 data-end=&quot;6616&quot; data-start=&quot;6566&quot;&gt;&lt;strong data-end=&quot;6616&quot; data-start=&quot;6569&quot;&gt;The Future of AI on TikTok and Social Media&lt;/strong&gt;&lt;/h2&gt;&lt;p data-end=&quot;6909&quot; data-start=&quot;6618&quot;&gt;The introduction of AI content controls shows that TikTok understands how important personalization is in today’s digital world. As AI technology becomes more advanced, more creators will experiment with it. This means platforms must find ways to maintain trust while encouraging creativity.&lt;/p&gt;&lt;p data-end=&quot;7072&quot; data-start=&quot;6911&quot;&gt;TikTok’s approach—a mix of user control, transparency tools, improved labeling, and educational initiatives—may become a model for other platforms in the future.&lt;/p&gt;&lt;p data-end=&quot;7088&quot; data-start=&quot;7074&quot;&gt;We can expect:&lt;/p&gt;&lt;ul data-end=&quot;7248&quot; data-start=&quot;7089&quot;&gt;
&lt;li data-end=&quot;7127&quot; data-start=&quot;7089&quot;&gt;
&lt;p data-end=&quot;7127&quot; data-start=&quot;7091&quot;&gt;More advanced AI detection methods&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;7158&quot; data-start=&quot;7128&quot;&gt;
&lt;p data-end=&quot;7158&quot; data-start=&quot;7130&quot;&gt;More AI tools for creators&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;7199&quot; data-start=&quot;7159&quot;&gt;
&lt;p data-end=&quot;7199&quot; data-start=&quot;7161&quot;&gt;Better community education around AI&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;7248&quot; data-start=&quot;7200&quot;&gt;
&lt;p data-end=&quot;7248&quot; data-start=&quot;7202&quot;&gt;Increased user control over feed preferences&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p data-end=&quot;7373&quot; data-start=&quot;7250&quot;&gt;The coming years will likely reshape how people interact with online content, and TikTok is preparing for that shift early.&lt;/p&gt;&lt;hr data-end=&quot;7378&quot; data-start=&quot;7375&quot; /&gt;&lt;h2 data-end=&quot;7401&quot; data-start=&quot;7380&quot;&gt;&lt;strong data-end=&quot;7401&quot; data-start=&quot;7383&quot;&gt;Final Thoughts&lt;/strong&gt;&lt;/h2&gt;&lt;p data-end=&quot;7849&quot; data-start=&quot;7403&quot;&gt;TikTok’s new AI content control update is not just a small feature—it’s a significant improvement in how users manage their experience and how the platform responds to the rapid growth of AI-generated media. By allowing users to adjust the amount of AI content they see, enhancing labeling accuracy with invisible watermarks, and launching an AI literacy fund, TikTok is taking meaningful steps to balance creativity, safety, and personalization.&lt;/p&gt;&lt;p&gt;

























































&lt;/p&gt;&lt;p data-end=&quot;8035&quot; data-start=&quot;7851&quot;&gt;As AI becomes more deeply integrated into digital platforms, features like these will play a key role in building trust and helping users navigate the evolving world of online content.&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/496403847119922696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2025/11/tiktok-introduces-new-controls-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/496403847119922696'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/496403847119922696'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2025/11/tiktok-introduces-new-controls-to.html' title='TikTok Introduces New Controls to Manage AI-Generated Content in the For You Feed'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-7505836262384692965</id><published>2025-10-30T17:13:00.008+05:30</published><updated>2025-10-30T17:23:04.022+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Build with me"/><title type='text'>How to Build an AI Email Reply Generator Chrome Extension with JavaScript</title><content type='html'>&lt;p&gt;Wrtitng email reply form an gpt required too much too and fro, example, if i want to generate a email reply, then first i have to copy the all content of email conversion then go to gpt and then paste it, and then asked gpt to generate the email reply then its give response, copy those response and again come to our gmail page and paste it, and some time happen that format of reply can be disttrub, so that we have to also correct those format. So, instead of doing these to and fro, as a developer, why don&#39;t we create an extension that has an embedded button in the reply box, and by clicking it, within a second, it will generate the reply based on all previous conversations?&lt;/p&gt;&lt;p&gt;In this article, we will go through creating your Chrome browser extension that integrates directly into the Gmail interface to generate an email reply. This &quot;Email AI&quot; will have two powerful features:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;AI Reply Generator:&lt;/b&gt; A button that reads the context of an email conversation and uses an AI (like &lt;b&gt;Google&#39;s Gemini&lt;/b&gt;) to generate a relevant, professional reply.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Template Inserter:&lt;/b&gt; A dropdown menu to instantly insert pre-written templates for common emails like birthday wishes or leave applications, etc.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;This project is a fantastic way to learn about developing a Chrome extension, DOM manipulation, and integrating AI for our personal use in our application. Let&#39;s get started!&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Prerequisites&lt;/h3&gt;&lt;p&gt;Before we dive in, make sure you have the following:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;A Code Editor IDE like VS Code.&lt;/li&gt;&lt;li&gt;Basic knowledge of JavaScript, HTML, and CSS.&lt;/li&gt;&lt;li&gt;An API key for an AI model (we will be using Google&#39;s Gemini, which has a generous free tier).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Project Structure: The Core Files&lt;/h2&gt;&lt;p&gt;Our extension will be surprisingly simple, consisting of just two primary files:&lt;/p&gt;&lt;p&gt;1.&amp;nbsp; &lt;b&gt;manifest.json:&lt;/b&gt; The configuration file. It tells Chrome what our extension is, what permissions it needs, and which scripts to run.&lt;/p&gt;&lt;p&gt;2.&amp;nbsp; &lt;b&gt;content.js: &lt;/b&gt;Our main JavaScript file. This script will be injected into the Gmail page to add our buttons and handle all the logic. This basically contains the logic, or use can say it&#39;s a backend.&lt;/p&gt;&lt;p&gt;Let&#39;s create a new folder for our project and add these two empty files to it.&lt;/p&gt;&lt;h3&gt;Step 1: Configuring the&amp;nbsp;manifest.json&amp;nbsp;File&lt;/h3&gt;&lt;p&gt;The&amp;nbsp;&lt;b&gt;manifest.json &lt;/b&gt;file is the heart of any Chrome extension. It defines the extension&#39;s capabilities and permissions. Let&#39;s break down what each part does.&lt;/p&gt;&lt;p&gt;Copy the following code into your &lt;b&gt;`manifest.json` &lt;/b&gt;file:&lt;/p&gt;&lt;p&gt;&lt;b&gt;json&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



  &lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
{
  &quot;name&quot;: &quot;Email Writer Assistant&quot;,
  &quot;description&quot;: &quot;AI-powered email reply generator and template tool.&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;manifest_version&quot;: 3,
  &quot;permissions&quot;: [
    &quot;activeTab&quot;,
    &quot;storage&quot;
  ],
  &quot;host_permissions&quot;: [
    &quot;*://mail.google.com/*&quot;,
    &quot;https://generativelanguage.googleapis.com/*&quot;
  ],
  &quot;content_scripts&quot;: [
    {
      &quot;js&quot;: [
        &quot;content.js&quot;
      ],
      &quot;matches&quot;: [
        &quot;*://mail.google.com/*&quot;
      ],
      &quot;run_at&quot;: &quot;document_end&quot;
    }
  ],
  &quot;action&quot;: {
    &quot;default_title&quot;: &quot;Email Writer Assistant&quot;
  }
}
&lt;/pre&gt;




&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Breaking Down the Manifest:&lt;/h4&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;name, description, version:&lt;/b&gt; Self-explanatory metadata for our Email reply generator extension.&lt;/li&gt;&lt;li&gt;&lt;b&gt;manifest_version:&lt;/b&gt; &lt;b&gt;3&lt;/b&gt;: Specifies that we are using the latest and most secure version of the Chrome extension platform. In simple words its specifes the extension version.&lt;/li&gt;&lt;li&gt;&lt;b&gt;permissions:&lt;/b&gt; We request &lt;b&gt;&quot;storage&quot;&lt;/b&gt; to potentially save custom templates in the future and &lt;b&gt;&quot;activeTab&quot;&lt;/b&gt; for general interaction.&lt;/li&gt;&lt;li&gt;&lt;b&gt;host_permissions: &lt;/b&gt;This is crucial for security.&amp;nbsp;&lt;b&gt;:*//mail.google.com/*: &lt;/b&gt;Grants our extension permission to run on Gmail.&lt;/li&gt;&lt;li&gt;&lt;b&gt;https://generativelanguage.googleapis.com/*:&lt;/b&gt; Grants permission to make API calls to Google&#39;s Gemini AI.&amp;nbsp;&lt;/li&gt;&lt;li&gt;&lt;b&gt;content_scripts:&lt;/b&gt; This is where we tell Chrome to inject our &lt;b&gt;content.js&lt;/b&gt; file into any page that matches the *://mail.google.com/* pattern. In simple words it tell the extension to run this file.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Writing the Logic for our Extension - The content.js Script&lt;/h3&gt;&lt;div&gt;This is where the magic happens. Our content script will watch for the Gmail compose window to open, then inject our custom buttons and define their functionality. So, using Xpath we will find the place where to inject buttons using &lt;b&gt;javascript DOM&lt;/b&gt;, and we will fetch the content of the mail using javascript dom and then we will pass those content with prompt to the gemini api using javascript. And get the response from it and pase it into the reply box using javascript dom.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Copy the following code into your content.js file. We have adapted it to call the Gemini API directly.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;JavaScript&lt;/div&gt;&lt;div&gt;
  
  
  &lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
  
console.log(&quot;Email Writer Extension - Content Script Loaded&quot;);
// --- Helper Functions to Create UI Elements ---
function createAIButton() {
    const button = document.createElement(&#39;div&#39;);
    button.className = &#39;T-I J-J5-Ji aoO v7 T-I-atl L3&#39;; // Mimic Gmail&#39;s button style
    button.style.marginRight = &#39;8px&#39;;
    button.innerHTML = &#39;AI Reply&#39;;
    button.setAttribute(&#39;role&#39;, &#39;button&#39;);
    button.setAttribute(&#39;data-tooltip&#39;, &#39;Generate AI Reply&#39;);
    return button;
}



function createTemplateDropdown() {
    const dropdown = document.createElement(&#39;select&#39;);
    dropdown.className = &#39;T-I J-J5-Ji aoO v7 T-I-atl L3 template-dropdown&#39;;
    dropdown.style.marginRight = &#39;8px&#39;;
    dropdown.setAttribute(&#39;role&#39;, &#39;button&#39;);
    dropdown.setAttribute(&#39;data-tooltip&#39;, &#39;Insert Template&#39;);

    // Default option
    const defaultOption = document.createElement(&#39;option&#39;);
    defaultOption.text = &#39;Templates&#39;;
    defaultOption.disabled = true;
    defaultOption.selected = true;
    dropdown.appendChild(defaultOption);

    // Hardcoded templates
    const templates = [
        { name: &quot;Birthday Wish&quot;, subject: &quot;Happy Birthday ??&quot;, body: &quot;Dear [Name],\n\nWishing you a very Happy Birthday! ????\n\nMay your day be filled with joy, love, and laughter.\n\nBest regards,\n[Your Name]&quot; },
        { name: &quot;Leave Application&quot;, subject: &quot;Leave Application Request&quot;, body: &quot;Dear [Manager&#39;s Name],\n\nI would like to request leave from [start date] to [end date] due to [reason].\n\nKindly approve my request.\n\nBest regards,\n[Your Name]&quot; },
        { name: &quot;Thank You Note&quot;, subject: &quot;Thank You&quot;, body: &quot;Dear [Recipient],\n\nI just wanted to say thank you for your support and guidance. I truly appreciate it.\n\nBest regards,\n[Your Name]&quot; },
    ];

    templates.forEach(template =&gt; {
        const option = document.createElement(&#39;option&#39;);
        option.value = template.name;
        option.text = template.name;
        option.dataset.subject = template.subject;
        option.dataset.body = template.body;
        dropdown.appendChild(option);
    });

    dropdown.addEventListener(&#39;change&#39;, (e) =&gt; {
        const selected = e.target.selectedOptions[0];
        const subject = selected.dataset.subject;
        const body = selected.dataset.body;
        const subjectBox = document.querySelector(&#39;input[name=&quot;subjectbox&quot;]&#39;);
        if (subjectBox) { subjectBox.value = subject; }
        const composeBox = document.querySelector(&#39;[role=&quot;textbox&quot;][g_editable=&quot;true&quot;]&#39;);
        if (composeBox) {
            composeBox.innerHTML = &quot;&quot;; // clear old content
            composeBox.focus();
            document.execCommand(&#39;insertText&#39;, false, body);
        }
        e.target.selectedIndex = 0; // Reset dropdown
    });
    return dropdown;

}

// --- Core Logic Functions ---
function getEmailContent() {
    // This function tries to find the content of the email being replied to.
    const quote = document.querySelector(&#39;.gmail_quote&#39;);
    if (quote) return quote.innerText.trim();

    // Fallback for different Gmail layouts
    const content = document.querySelector(&#39;.a3s.aiL&#39;);
    if (content) return content.innerText.trim();
    return &#39;&#39;;
}

function findComposeToolbar() {

    // Gmail&#39;s class names can change. This looks for the toolbar using several potential selectors.
    return document.querySelector(&#39;.gU.Up&#39;) || document.querySelector(&#39;.aDh&#39;);
}

async function generateAIReply(emailContent) {
    const API_KEY = &#39;AIzaSyCXpVrzhqsDB5uXowkjftJsE2OYCpKHr3o&#39;; // IMPORTANT: Replace with your actual key
    const API_URL = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY}`;
    const prompt = `Generate a professional email reply for the following email content. Please don&#39;t generate a subject line. Email to reply to:\n\n---\n${emailContent}\n---`;
    try {
        const response = await fetch(API_URL, {
            method: &#39;POST&#39;,
            headers: { &#39;Content-Type&#39;: &#39;application/json&#39; },
            body: JSON.stringify({
                contents: [{ parts: [{ text: prompt }] }]
            })
        });
        if (!response.ok) {
            throw new Error(`API request failed with status ${response.status}`);
        }
        const data = await response.json();
        // Navigate through the Gemini response structure to get the text
        return data.candidates[0].content.parts[0].text;

    } catch (error) {
        console.error(&quot;Error generating AI reply:&quot;, error);
        return &quot;Sorry, I was unable to generate a reply. Please try again.&quot;;
    }

}


function injectButtons() {
    // Avoid duplicate buttons
    document.querySelector(&#39;.ai-reply-button&#39;)?.remove();
    document.querySelector(&#39;.template-dropdown&#39;)?.remove();
    const toolbar = findComposeToolbar();
    if (!toolbar) {
        console.log(&quot;Compose toolbar not found, will retry.&quot;);
        return;
    }

    // Always add the Templates dropdown
    const dropdown = createTemplateDropdown();
    toolbar.insertBefore(dropdown, toolbar.firstChild);

    // Only add the &quot;AI Reply&quot; button if we are replying to an email
    const emailContent = getEmailContent();
    if (emailContent &amp;&amp; emailContent.length &gt; 0) {
        const button = createAIButton();
        button.classList.add(&#39;ai-reply-button&#39;);
        button.addEventListener(&#39;click&#39;, async () =&gt; {
            button.innerHTML = &#39;Generating...&#39;;
            button.disabled = true;
            const generatedReply = await generateAIReply(emailContent);
            const composeBox = document.querySelector(&#39;[role=&quot;textbox&quot;][g_editable=&quot;true&quot;]&#39;);

            if (composeBox) {
                composeBox.focus();
                // We use execCommand for broad compatibility in content-editable divs
                document.execCommand(&#39;insertText&#39;, false, &#39;\n\n&#39; + generatedReply);
            }
            button.innerHTML = &#39;AI Reply&#39;;
            button.disabled = false;
        });
        toolbar.insertBefore(button, toolbar.firstChild);
    }
}

// --- Observer to Detect When the Compose Window Opens ---
const observer = new MutationObserver((mutations) =&gt; {
    for (const mutation of mutations) {

        // Check if a new node was added that looks like a compose window
        const composeWindowAdded = Array.from(mutation.addedNodes).some(node =&gt;
            node.nodeType === Node.ELEMENT_NODE &amp;&amp; node.querySelector(&#39;.gU.Up, .aDh&#39;)
        );

        if (composeWindowAdded) {
            console.log(&quot;Compose Window Detected, injecting buttons.&quot;);

            // Wait a moment for Gmail&#39;s UI to fully render before injecting
            setTimeout(injectButtons, 500);
            return; // No need to check other mutations
        }
    }
});

// Start observing the entire document for changes
observer.observe(document.body, {
    childList: true,
    subtree: true
});
&lt;/pre&gt;
  
  
  
  &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;IMPORTANT:&lt;/b&gt; Remember to replace &lt;b&gt;&#39;YOUR_GEMINI_API_KEY&#39;&lt;/b&gt; with your actual API key from the Google AI Studio. We will using the free version of the gemini api.&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Loading and Testing Your Extension&lt;/h3&gt;&lt;div&gt;Now that the code is ready, let&#39;s load it into Chrome: Follows the following steps to use our own extension.&lt;/div&gt;&lt;div&gt;Open Chrome and navigate to &lt;b&gt;chrome://extensions&lt;/b&gt;.&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjXO29I_7aOieS80Xo6WfSyUitbxl_p7o3uTNUt9svt8HAvvN3wLgkF3iPtwuImlpJGuuae1oeL6tqp5t17ZHNud2vLnHlYReh_QpmfXN2GvmrL_toHdVWtd-Xr1ogct8jC8jiQ5ZpcaQr78f_BvnvSYJpy1shBNveyHVSSGI-Kmz95ZjyTlvoPB8C2wMA&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;914&quot; data-original-width=&quot;1898&quot; height=&quot;309&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjXO29I_7aOieS80Xo6WfSyUitbxl_p7o3uTNUt9svt8HAvvN3wLgkF3iPtwuImlpJGuuae1oeL6tqp5t17ZHNud2vLnHlYReh_QpmfXN2GvmrL_toHdVWtd-Xr1ogct8jC8jiQ5ZpcaQr78f_BvnvSYJpy1shBNveyHVSSGI-Kmz95ZjyTlvoPB8C2wMA=w642-h309&quot; width=&quot;642&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Turn on the &lt;b&gt;&quot;Developer mode&quot;&lt;/b&gt; toggle in the top-right corner.&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi0vMF4K9NXlqN2uQ3WXT6FruxfyzNtBLJLK_1dXDk8kI2r4Mf8VaDlPcECoYNLet_-WyNLvZlnJDR0M6LeYOecM7tcBt5FWot0zqG7Hclv7DtC4xHipf2VDyNZMQwZ0DNIXD6T00SKGQOW0-RE3Zx-2ndfBl7-cAKrWljTafA3UFr1ms2Bjipcx4qdGmg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;491&quot; data-original-width=&quot;965&quot; height=&quot;330&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi0vMF4K9NXlqN2uQ3WXT6FruxfyzNtBLJLK_1dXDk8kI2r4Mf8VaDlPcECoYNLet_-WyNLvZlnJDR0M6LeYOecM7tcBt5FWot0zqG7Hclv7DtC4xHipf2VDyNZMQwZ0DNIXD6T00SKGQOW0-RE3Zx-2ndfBl7-cAKrWljTafA3UFr1ms2Bjipcx4qdGmg=w648-h330&quot; width=&quot;648&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Click the &lt;b&gt;&quot;Load unpacked&quot;&lt;/b&gt; button.&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgFfpyG-iH3-RnkIS3q7t96zVswR-liLwU3wIvHrU5JdRIWl7eBFPl_7AMH_RJgvOm4sVZQphrl4rlkGW7sflt_5Fd9W5kQQfzxOH9oXHnVLBkgw0GmSvFqL2bdKnH6BEzx_E87mKpiQiHkR5PMasK4-bpKvQ3EIYOOHGY94HM76PvteYnSnGWByQOt0kk&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;587&quot; data-original-width=&quot;1281&quot; height=&quot;295&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgFfpyG-iH3-RnkIS3q7t96zVswR-liLwU3wIvHrU5JdRIWl7eBFPl_7AMH_RJgvOm4sVZQphrl4rlkGW7sflt_5Fd9W5kQQfzxOH9oXHnVLBkgw0GmSvFqL2bdKnH6BEzx_E87mKpiQiHkR5PMasK4-bpKvQ3EIYOOHGY94HM76PvteYnSnGWByQOt0kk=w643-h295&quot; width=&quot;643&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Select the folder where you saved your &lt;b&gt;manifest.json &lt;/b&gt;and &lt;b&gt;content.js &lt;/b&gt;files. Make sure to uplaod the whole folder.&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgN5V5eL2BsvII8Mruy05_DkdzQ0NOz7OQ2izaBvwGqXgrQwXE38EEYnRGbUsnBzrOaF2bFjt71XV2K6j2hvYrAVq-dv1atQcBQDdvXybTIBEMH_OFaBGFZHsalr2ExsecD_ObE01tKxW1BfLlYyMfNLgGowBmsgOQXaTAJgsBK7CNou4nD8ab7rFXF2p4&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;451&quot; data-original-width=&quot;747&quot; height=&quot;389&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgN5V5eL2BsvII8Mruy05_DkdzQ0NOz7OQ2izaBvwGqXgrQwXE38EEYnRGbUsnBzrOaF2bFjt71XV2K6j2hvYrAVq-dv1atQcBQDdvXybTIBEMH_OFaBGFZHsalr2ExsecD_ObE01tKxW1BfLlYyMfNLgGowBmsgOQXaTAJgsBK7CNou4nD8ab7rFXF2p4=w645-h389&quot; width=&quot;645&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If everything is correct, your &lt;b&gt;&quot;Email Writer Assistant&quot;&lt;/b&gt; will appear in the list.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiu912Y3tQAOMU6sJBqlttdnRBmk1gphFSZz86psATT0Xy5WGwAruimE-fNlQrDu8k8L34D-W4JeVIEizAK1r42Bs0zj5ymiTrpsx0Hl-REHO0HDLDrgArYyvbn1ww48Zr253SyDwix7_Fy6mZ7Hg3bVL4ri3b-uMKap46N_57w7N5JTW6n2hr-3NXaG0c&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;699&quot; data-original-width=&quot;1866&quot; height=&quot;243&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiu912Y3tQAOMU6sJBqlttdnRBmk1gphFSZz86psATT0Xy5WGwAruimE-fNlQrDu8k8L34D-W4JeVIEizAK1r42Bs0zj5ymiTrpsx0Hl-REHO0HDLDrgArYyvbn1ww48Zr253SyDwix7_Fy6mZ7Hg3bVL4ri3b-uMKap46N_57w7N5JTW6n2hr-3NXaG0c=w647-h243&quot; width=&quot;647&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now, go to Gmail, refresh the page, and click &quot;&lt;b&gt;Compose&lt;/b&gt;.&quot; You should see the &quot;&lt;b&gt;Templates&lt;/b&gt;&quot; dropdown in the compose toolbar.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi6nKLhoqjahPgkJPzSZRbrV-jUdfkEjhQHgIZ5HC1TwXwWKXfoVn_l8gMtiNSnhsI1wQw1AoH28_5W_M2ePJtQOhs6ZLdlXVNwfQcVhWx4MeFWktEn1wcjILfIMFkO0Tr2TjeYu2Otir3MsYHvV50kZWb15VlVupUOjGy60w6I75d6saxUIYyRSn25YJY&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;631&quot; data-original-width=&quot;680&quot; height=&quot;596&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi6nKLhoqjahPgkJPzSZRbrV-jUdfkEjhQHgIZ5HC1TwXwWKXfoVn_l8gMtiNSnhsI1wQw1AoH28_5W_M2ePJtQOhs6ZLdlXVNwfQcVhWx4MeFWktEn1wcjILfIMFkO0Tr2TjeYu2Otir3MsYHvV50kZWb15VlVupUOjGy60w6I75d6saxUIYyRSn25YJY=w643-h596&quot; width=&quot;643&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If you click &quot;&lt;b&gt;Reply&lt;/b&gt;&quot; on an existing email, both the &quot;&lt;b&gt;AI Reply&lt;/b&gt;&quot; button and the &quot;&lt;b&gt;Templates&lt;/b&gt;&quot; dropdown will appear!&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi_0m6-ISdcgxtFmQMWE_h1TmTDCYd1Hhbo6GqNWEzyyoooEU2Ly_kp9K2yfCx4I5PowDaa5RgjO4YlEIiVkfJoUc75UIQ5mHPdCvcZBKoH20a2up5AvG7GLGUa13fOJ2JdkqwcEoCZHmwTyInDnvuGtF0BBdIq38fFdwT9IhtQfFid7T-oc0Du1v99RLU&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;858&quot; data-original-width=&quot;1792&quot; height=&quot;306&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi_0m6-ISdcgxtFmQMWE_h1TmTDCYd1Hhbo6GqNWEzyyoooEU2Ly_kp9K2yfCx4I5PowDaa5RgjO4YlEIiVkfJoUc75UIQ5mHPdCvcZBKoH20a2up5AvG7GLGUa13fOJ2JdkqwcEoCZHmwTyInDnvuGtF0BBdIq38fFdwT9IhtQfFid7T-oc0Du1v99RLU=w640-h306&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Potential Enhancements and Next Steps&lt;/h2&gt;&lt;div&gt;This is a great starting point, but you can take it much further. Here are some ideas:&lt;/div&gt;&lt;div&gt;&lt;b&gt;Tone Selection: &lt;/b&gt;Add another dropdown to let the user choose a tone (e.g., &quot;Professional,&quot; &quot;Casual,&quot; &quot;Enthusiastic&quot;) and pass it to the AI prompt.&lt;/div&gt;&lt;div&gt;&lt;b&gt;Custom Templates:&lt;/b&gt; Use chrome.storage.sync to allow users to save their own templates that sync across their devices.&lt;/div&gt;&lt;div&gt;&lt;b&gt;Settings Page: &lt;/b&gt;Create a dedicated options page for managing the API key and custom templates.&lt;/div&gt;&lt;div&gt;&lt;b&gt;Improved UI:&lt;/b&gt; Design custom icons for your buttons to make them look more polished.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusion&lt;/h2&gt;&lt;div&gt;Congratulations! You have successfully built a powerful AI Email Assistant for Gmail using JavaScript. You&#39;ve learned how to create a Chrome extension, interact with a web page&#39;s DOM, and integrate a powerful AI model to automate a common task.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This project not only saves you time but also serves as a solid foundation for building even more complex and useful browser automation tools. Happy coding!&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/7505836262384692965/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2025/10/build-ai-email-reply-generator-extension.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7505836262384692965'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7505836262384692965'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2025/10/build-ai-email-reply-generator-extension.html' title='How to Build an AI Email Reply Generator Chrome Extension with JavaScript'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEjXO29I_7aOieS80Xo6WfSyUitbxl_p7o3uTNUt9svt8HAvvN3wLgkF3iPtwuImlpJGuuae1oeL6tqp5t17ZHNud2vLnHlYReh_QpmfXN2GvmrL_toHdVWtd-Xr1ogct8jC8jiQ5ZpcaQr78f_BvnvSYJpy1shBNveyHVSSGI-Kmz95ZjyTlvoPB8C2wMA=s72-w642-h309-c" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-7793627313251829882</id><published>2025-10-27T18:32:00.002+05:30</published><updated>2025-10-27T18:34:53.799+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Build with me"/><title type='text'>How to Build a Resume Analysis Web App with Spring Boot, React, and Gemini API: A Step-by-Step Guide</title><content type='html'>&lt;p&gt;In today&#39;s competitive job market, recruiters and hiring managers are often inundated with hundreds of resumes for a single position. Manually screening each one is a time-consuming and tedious task. What if you could build a smart application to automate this process, extracting key information from the resume and providing a concise analysis of the resume by providing an ATS score, pros, cons etc.&lt;/p&gt;&lt;p&gt;This is where the power of modern technology comes in. By combining a robust backend build with Java, Spring Boot, a dynamic frontend build with React.js, and a cutting-edge AI model, you can create a powerful Resume Analysis web application.&lt;/p&gt;&lt;p&gt;In this comprehensive article, we will walk you through building a full-stack resume analysis app from the scratch. We will use Spring Boot for the backend REST API, ReactJS for the interactive frontend, and Google&#39;s powerful Gemini API for the intelligent text analysis.&lt;/p&gt;&lt;p&gt;This project is not only an addition to your portfolio but also a practical knowledge of integrating generative AI into web applications.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Why This Tech Stack?&lt;/h3&gt;&lt;p&gt;Before we dive into the code, let&#39;s understand why this combination of technologies is an excellent choice for our project.&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Spring Boot&lt;/h4&gt;&lt;p&gt;Spring Boot is a leading framework for building enterprise-grade Java applications. Its key advantages include:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Rapid Development:&lt;/b&gt; Auto-configuration and an opinionated setup drastically reduce boilerplate code.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Robust Ecosystem:&lt;/b&gt; Seamless integration with a vast library of tools for security, data access, and more.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Scalability:&lt;/b&gt; Built for creating scalable and maintainable microservices and REST APIs.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;ReactJS&lt;/h4&gt;&lt;p&gt;React is a declarative and component-based JavaScript library for building user interfaces.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Component-Based:&lt;/b&gt; Allows you to build encapsulated components that manage their own state, making complex UIs manageable.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Rich User Experience:&lt;/b&gt; Enables the creation of fast, responsive, and dynamic single-page applications (SPAs).&lt;/li&gt;&lt;li&gt;&lt;b&gt;Strong Community:&lt;/b&gt; Backed by Facebook and a massive community, offering a wealth of resources and libraries.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Google Gemini API&lt;/h4&gt;&lt;p&gt;Gemini is Google&#39;s latest and most capable family of multimodal AI models. For our app, it&#39;s the engine that provides the intelligence.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Advanced Understanding: &lt;/b&gt;Gemini can process and understand large contexts of text, making it perfect for analyzing a full resume.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Generative Power: &lt;/b&gt;We can instruct it with a specific prompt to extract skills, summarize experience, and check for specific qualifications.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Easy Integration:&lt;/b&gt; Provides a straightforward REST API that can be easily called from our Spring Boot backend.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Project Architecture Overview&lt;/h2&gt;&lt;p&gt;Our application will have a simple but effective architecture:&lt;/p&gt;&lt;p&gt;&lt;b&gt;ReactJS Frontend:&lt;/b&gt; The user will upload a resume in a PDF format through a simple web interface. And file clicking, upload the result will show like ATS score, pros, cons, and recommendations for improvement.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Spring Boot Backend: &lt;/b&gt;The frontend sends the resume to a REST endpoint on our Spring Boot server, and then it parses the PDF file and fetches its content it and sends that content with a &lt;b&gt;prompt &lt;/b&gt;to the &lt;b&gt;Gemini API&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Gemini API Integration:&lt;/b&gt; The backend takes the resume text, wraps it in a carefully crafted prompt, and sends it to the Google Gemini API.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Response Flow:&lt;/b&gt; Gemini processes the request and returns a structured JSON analysis. Our backend then forwards this analysis back to the React frontend to be displayed to the user.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Prerequisites&lt;/h2&gt;&lt;p&gt;Before you begin, ensure you have the following installed:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Java Development Kit (JDK) 17 or later&lt;/li&gt;&lt;li&gt;Apache Maven&lt;/li&gt;&lt;li&gt;Node.js and npm&lt;/li&gt;&lt;li&gt;An IDE of your choice (e.g., IntelliJ IDEA, VS Code)&lt;/li&gt;&lt;li&gt;A Google AI Studio API Key for the Gemini API. You can get one for free from the Google AI for Developers website.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 1: Building the Spring Boot Backend&lt;/h2&gt;&lt;p&gt;Let&#39;s start by creating the backend service that will communicate with the Gemini API and frontend.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 1: Initialize the Spring Boot Project&lt;/h3&gt;&lt;p&gt;Go to &lt;a href=&quot;http://start.spring.io&quot; target=&quot;_blank&quot;&gt;start.spring.io&lt;/a&gt; and configure your project with the following settings:&lt;/p&gt;&lt;p&gt;&lt;b&gt;Project: &lt;/b&gt;Maven&lt;/p&gt;&lt;p&gt;&lt;b&gt;Language:&lt;/b&gt; Java 17 or later&lt;/p&gt;&lt;p&gt;&lt;b&gt;Spring Boot:&lt;/b&gt; The latest stable version&lt;/p&gt;&lt;p&gt;&lt;b&gt;Group:&lt;/b&gt; com.resumeanalyzer&lt;/p&gt;&lt;p&gt;&lt;b&gt;Artifact:&lt;/b&gt; resume-analyzer&lt;/p&gt;&lt;p&gt;&lt;b&gt;Dependencies: &lt;/b&gt;Add Spring Web&lt;/p&gt;&lt;p&gt;Download the generated ZIP file, extract it, and open it in your IDE.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Configure the Gemini API&lt;/h3&gt;&lt;p&gt;First, let&#39;s add your Gemini API key to the configuration file. Never hardcode API keys in your source code.&lt;/p&gt;&lt;p&gt;Open &lt;b&gt;src/main/resources/application.properties&lt;/b&gt; and add the following lines:&lt;/p&gt;&lt;p&gt;&lt;b&gt;properties&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;# Gemini API Configuration
gemini.api.key=YOUR_API_KEY
gemini.project.id=resume-analyzer
gemini.location=us-central1
&lt;/pre&gt;





&lt;p&gt;&lt;/p&gt;&lt;p&gt;Replace &lt;b&gt;YOUR_API_KEY_HERE&lt;/b&gt; with the key you obtained from Google AI Studio.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Create the DTOs (Data Transfer Objects)&lt;/h3&gt;&lt;p&gt;We need to create a class that represents the structure of the result that comes from the Google Gemini api.&lt;/p&gt;&lt;p&gt;Create a DTO package. Inside it, create an &lt;b&gt;AnalysisResult.java &lt;/b&gt;record to hold the incoming resume text.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;




&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.resumeanalyzer.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = &quot;resume_analyses&quot;)
public class AnalysisResult {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String fileName;

    @Column(nullable = false)
    private String overallScore;

    @Column(columnDefinition = &quot;TEXT&quot;)
    private String summary;

    @ElementCollection
    @CollectionTable(name = &quot;analysis_pros&quot;, joinColumns = @JoinColumn(name = &quot;analysis_id&quot;))
    @Column(name = &quot;pro&quot;, columnDefinition = &quot;TEXT&quot;)
    private List&amp;lt;String&amp;gt; pros;

    @ElementCollection
    @CollectionTable(name = &quot;analysis_cons&quot;, joinColumns = @JoinColumn(name = &quot;analysis_id&quot;))
    @Column(name = &quot;con&quot;, columnDefinition = &quot;TEXT&quot;)
    private List&amp;lt;String&amp;gt; cons;

    @ElementCollection
    @CollectionTable(name = &quot;analysis_recommendations&quot;, joinColumns = @JoinColumn(name = &quot;analysis_id&quot;))
    @Column(name = &quot;recommendation&quot;, columnDefinition = &quot;TEXT&quot;)
    private List&amp;lt;String&amp;gt; recommendations;

    @Column(nullable = false)
    private LocalDateTime analyzedAt;

    public AnalysisResult(String fileName, String overallScore, String summary, 
                         List&amp;lt;String&amp;gt; pros, List&amp;lt;String&amp;gt; cons, List&amp;lt;String&amp;gt; recommendations, 
                         LocalDateTime analyzedAt) {
        this.fileName = fileName;
        this.overallScore = overallScore;
        this.summary = summary;
        this.pros = pros;
        this.cons = cons;
        this.recommendations = recommendations;
        this.analyzedAt = analyzedAt;
    }
} 
&lt;/pre&gt;
  





&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 4: Build the Service Layer&lt;/h3&gt;&lt;p&gt;The service layer will contain the core logic for parsing the content from PDF and communicating with the Gemini API and getting a response, and converting it into a proper DTO structure.&lt;/p&gt;&lt;p&gt;Create a service package and add a &lt;b&gt;ResumeAnalysisService.java &lt;/b&gt;class.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.resumeanalyzer.service;

import com.resumeanalyzer.model.AnalysisResult;
import com.resumeanalyzer.model.ResumeAnalysis;
import com.resumeanalyzer.repository.ResumeAnalysisRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class ResumeAnalysisService {

    @Value(&quot;${gemini.api.key}&quot;)
    private String apiKey;

    @Autowired
    private ResumeAnalysisRepository resumeAnalysisRepository;

    private final RestTemplate restTemplate = new RestTemplate();
    private static final String UPLOAD_DIR = &quot;uploads/resumes&quot;;
    private static final String GEMINI_API_URL = &quot;https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent&quot;;

    public AnalysisResult analyzeResume(MultipartFile file) throws IOException {
        createUploadDirectory();
        String fileName = UUID.randomUUID() + &quot;_&quot; + file.getOriginalFilename();
        Path filePath = Paths.get(UPLOAD_DIR, fileName);
        
        try {
            Files.copy(file.getInputStream(), filePath);
            String resumeText = extractTextFromPdf(filePath.toFile());
            AnalysisResult result = analyzeWithGemini(resumeText, fileName);
            
            ResumeAnalysis analysis = new ResumeAnalysis(
                file.getOriginalFilename(),
                result.getOverallScore(),
                result.getSummary()
            );
            resumeAnalysisRepository.save(analysis);
            
            return result;
        } finally {
            Files.deleteIfExists(filePath);
        }
    }

    private void createUploadDirectory() throws IOException {
        Path path = Paths.get(UPLOAD_DIR);
        if (!Files.exists(path)) {
            Files.createDirectories(path);
        }
    }

    private String extractTextFromPdf(File file) throws IOException {
        try (PDDocument document = PDDocument.load(file)) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }

    private AnalysisResult analyzeWithGemini(String resumeText, String fileName) {
        String prompt = String.format(
            &quot;Analyze this resume and provide a detailed analysis in EXACTLY this format:\n&quot; +
            &quot;1. Overall Score: [score]/100\n&quot; +
            &quot;2. Summary: [2-3 sentence summary]\n&quot; +
            &quot;3. Pros:\n- [Strength 1]\n- [Strength 2]\n- [Strength 3]\n&quot; +
            &quot;4. Cons:\n- [Weakness 1]\n- [Weakness 2]\n- [Weakness 3]\n&quot; +
            &quot;5. Recommendations:\n- [Suggestion 1]\n- [Suggestion 2]\n- [Suggestion 3]\n\n&quot; +
            &quot;Resume Content:\n%s&quot;, 
            resumeText.substring(0, Math.min(resumeText.length(), 15000))
        );


        Map&amp;lt;String, Object&amp;gt; requestBody = new HashMap&amp;lt;&amp;gt;();
        Map&amp;lt;String, Object&amp;gt; content = new HashMap&amp;lt;&amp;gt;();
        Map&amp;lt;String, String&amp;gt; textPart = new HashMap&amp;lt;&amp;gt;();
        textPart.put(&quot;text&quot;, prompt);
        
        content.put(&quot;parts&quot;, Collections.singletonList(textPart));
        requestBody.put(&quot;contents&quot;, Collections.singletonList(content));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(&quot;x-goog-api-key&quot;, apiKey);

        try {
            ResponseEntity&amp;lt;Map&amp;gt; response = restTemplate.exchange(
                GEMINI_API_URL,
                HttpMethod.POST,
                new HttpEntity&amp;lt;&amp;gt;(requestBody, headers),
                Map.class
            );

            return parseGeminiResponse(response.getBody(), fileName);
        } catch (Exception e) {
            log.error(&quot;Gemini API error: {}&quot;, e.getMessage());
            return getFallbackAnalysis(fileName);
        }
    }

    private AnalysisResult parseGeminiResponse(Map&amp;lt;String, Object&amp;gt; response, String fileName) {
        if (response == null || !response.containsKey(&quot;candidates&quot;)) {
            log.warn(&quot;Invalid Gemini response structure&quot;);
            return getFallbackAnalysis(fileName);
        }

        try {
            Map&amp;lt;String, Object&amp;gt; candidate = ((List&amp;lt;Map&amp;lt;String, Object&amp;gt;&amp;gt;) response.get(&quot;candidates&quot;)).get(0);
            Map&amp;lt;String, Object&amp;gt; content = (Map&amp;lt;String, Object&amp;gt;) candidate.get(&quot;content&quot;);
            String analysisText = (String) ((Map&amp;lt;String, Object&amp;gt;) ((List&amp;lt;?&amp;gt;) content.get(&quot;parts&quot;)).get(0)).get(&quot;text&quot;);
            
            log.debug(&quot;Gemini raw analysis+++++++++++++++++++++++++++++++:\n{}&quot;, response);
            return parseAnalysisText(analysisText, fileName);
        } catch (Exception e) {
            log.error(&quot;Response parsing failed: {}&quot;, e.getMessage());
            return getFallbackAnalysis(fileName);
        }
    }

    private AnalysisResult parseAnalysisText(String analysisText, String fileName) {
        
        String[] sections = analysisText.split(&quot;\\d+\\.\\s&quot;);
        
      
        if (sections.length &amp;lt; 6) {
            log.warn(&quot;Unexpected section count: {}&quot;, sections.length);
            return parseAnalysisTextFallback(analysisText, fileName);
        }
        
        String score = extractScore(sections[1]);
        String summary = extractSummary(sections[2]);
        List&amp;lt;String&amp;gt; pros = extractListItems(sections[3]);
        List&amp;lt;String&amp;gt; cons = extractListItems(sections[4]);
        List&amp;lt;String&amp;gt; recommendations = extractListItems(sections[5]);
        
        return new AnalysisResult(
            fileName,
            score,
            summary,
            pros,
            cons,
            recommendations,
            LocalDateTime.now()
        );
    }
    
    private String extractScore(String section) {
       
        Pattern pattern = Pattern.compile(&quot;(\\d+/100)&quot;);
        Matcher matcher = pattern.matcher(section);
        return matcher.find() ? matcher.group(1) : &quot;N/A&quot;;
    }
    
    private String extractSummary(String section) {
        String summary = section.replaceFirst(&quot;Summary[:\\s]*&quot;, &quot;&quot;);
        return summary.trim().replaceAll(&quot;\n&quot;, &quot; &quot;);
    }
    
    private List&amp;lt;String&amp;gt; extractListItems(String section) {
        List&amp;lt;String&amp;gt; items = new ArrayList&amp;lt;&amp;gt;();
        String[] lines = section.split(&quot;\n&quot;);
        
        for (String line : lines) {
            line = line.trim();
            
            if (line.isEmpty() || 
                line.startsWith(&quot;Pros:&quot;) || 
                line.startsWith(&quot;Cons:&quot;) || 
                line.startsWith(&quot;Recommendations:&quot;)) {
                continue;
            }
            
          
            if (line.startsWith(&quot;- &quot;) || line.startsWith(&quot;* &quot;)) {
                items.add(line.substring(2).trim());
            } 
        
            else if (!line.contains(&quot;:&quot;) &amp;amp;&amp;amp; line.length() &amp;gt; 10) {
                items.add(line);
            }
        }
        return items;
    }
    
    private AnalysisResult parseAnalysisTextFallback(String analysisText, String fileName) {
        log.warn(&quot;Using fallback parser for analysis text&quot;);
      
        String score = &quot;N/A&quot;;
        Pattern pattern = Pattern.compile(&quot;(\\d+/100)&quot;);
        Matcher matcher = pattern.matcher(analysisText);
        if (matcher.find()) {
            score = matcher.group(1);
        }
        
        String summary = &quot;&quot;;
        String[] parts = analysisText.split(&quot;2\\.\\s*Summary[:\\s]*&quot;, 2);
        if (parts.length &amp;gt; 1) {
            summary = parts[1].split(&quot;3\\.\\s*Pros[:\\s]*&quot;)[0].trim();
        }
        
        return new AnalysisResult(
            fileName,
            score,
            summary,
            extractListItemsFallback(analysisText, &quot;Pros&quot;),
            extractListItemsFallback(analysisText, &quot;Cons&quot;),
            extractListItemsFallback(analysisText, &quot;Recommendations&quot;),
            LocalDateTime.now()
        );
    }
    
    private List&amp;lt;String&amp;gt; extractListItemsFallback(String text, String sectionName) {
        List&amp;lt;String&amp;gt; items = new ArrayList&amp;lt;&amp;gt;();
        Pattern pattern = Pattern.compile(
            Pattern.quote(sectionName) + &quot;[\\s:]*\\n(-[^\\n]+)&quot;
        );
        Matcher matcher = pattern.matcher(text);
        
        while (matcher.find()) {
            items.add(matcher.group(1).substring(1).trim());
        }
        return items.isEmpty() ? Collections.emptyList() : items;
    }

    private AnalysisResult getFallbackAnalysis(String fileName) {
        return new AnalysisResult(
            fileName,
            &quot;75/100&quot;,
            &quot;Generated fallback analysis due to service issues&quot;,
            Arrays.asList(&quot;Strong technical background&quot;, &quot;Good education history&quot;),
            Arrays.asList(&quot;Limited work experience&quot;, &quot;Could use more quantifiable achievements&quot;),
            Arrays.asList(&quot;Add more metrics to experience&quot;, &quot;Include relevant certifications&quot;),
            LocalDateTime.now()
        );
    }

    public List&amp;lt;ResumeAnalysis&amp;gt; getUserAnalyses() {
        return resumeAnalysisRepository.findAll();
    }
}
&lt;/pre&gt;




&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 5: Create the REST Controller&lt;/h3&gt;&lt;p&gt;Finally, create an endpoint to expose this service. Create a controller package with a &lt;b&gt;ResumeAnalysisController.java &lt;/b&gt;class.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;




&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.resumeanalyzer.controller;

import com.resumeanalyzer.model.AnalysisResult;
import com.resumeanalyzer.model.ResumeAnalysis;
import com.resumeanalyzer.service.ResumeAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(&quot;/api&quot;)
@CrossOrigin(origins = &quot;http://localhost:3000&quot;)
public class ResumeAnalysisController {

    @Autowired
    private ResumeAnalysisService resumeAnalysisService;

    @PostMapping(&quot;/analyze&quot;)
    public ResponseEntity&amp;lt;AnalysisResult&amp;gt; analyzeResume(@RequestParam(&quot;file&quot;) MultipartFile file) {
        try {
            AnalysisResult result = resumeAnalysisService.analyzeResume(file);
            return ResponseEntity.ok(result);
        } catch (IOException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping(&quot;/analyses&quot;)
    public ResponseEntity&amp;lt;List&amp;lt;ResumeAnalysis&amp;gt;&amp;gt; getUserAnalyses() {
        List&amp;lt;ResumeAnalysis&amp;gt; analyses = resumeAnalysisService.getUserAnalyses();
        return ResponseEntity.ok(analyses);
    }
} 
&lt;/pre&gt;





&lt;p&gt;&lt;/p&gt;&lt;p&gt;The &lt;b&gt;@CrossOrigin&lt;/b&gt; annotation is crucial for allowing our React app (which will run on port &lt;b&gt;3000&lt;/b&gt;) to communicate with our backend (running on port &lt;b&gt;8080&lt;/b&gt;).&lt;/p&gt;&lt;p&gt;Your backend is now ready! You can run it from your IDE or by using the command &lt;b&gt;mvn spring-boot:run&lt;/b&gt;.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 2: Building the ReactJS Frontend&lt;/h2&gt;&lt;p&gt;Now, let&#39;s create the user interface where users can upload their resumes and make sure the design can be varied according to your choices.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 1: Initialize the React App&lt;/h3&gt;&lt;p&gt;Open your terminal, navigate to your desired project directory (outside the Spring Boot project), and run:&lt;/p&gt;&lt;p&gt;&lt;b&gt;Bash&lt;/b&gt;&lt;/p&gt;&lt;p&gt;




&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;npx create-react-app resume-analyzer-ui
cd resume-analyzer-ui
&lt;/pre&gt;




&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Install Axios&lt;/h3&gt;&lt;p&gt;We&#39;ll use Axios to make HTTP requests to our backend.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Bash&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;npm install axios
&lt;/pre&gt;



&lt;p&gt;&lt;/p&gt;&lt;p&gt;Step 3: Create ResumeAnalysis.jsx for handling the resume and there response.&lt;/p&gt;&lt;p&gt;&lt;b&gt;React&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import { useState, useCallback } from &quot;react&quot;
import { useDropzone } from &quot;react-dropzone&quot;
import axios from &quot;axios&quot;
import {
    FiUpload,
    FiFile,
    FiAlertTriangle,
    FiClipboard,
    FiCheckCircle,
    FiXCircle,
    FiZap,
    FiRefreshCw,
    FiDownload,
} from &quot;react-icons/fi&quot;

const ResumeAnalysis = () =&amp;gt; {
  const [file, setFile] = useState(null)
  const [loading, setLoading] = useState(false)
  const [analysis, setAnalysis] = useState(null)
  const [error, setError] = useState(null)

  const onDrop = useCallback(async (acceptedFiles) =&amp;gt; {
    const selectedFile = acceptedFiles[0]
    if (selectedFile) {
      setFile(selectedFile)
      setLoading(true)
      setError(null)

      const formData = new FormData()
      formData.append(&quot;file&quot;, selectedFile)

      try {
        const response = await axios.post(&quot;http://localhost:8080/api/analyze&quot;, formData, {
          headers: {
            &quot;Content-Type&quot;: &quot;multipart/form-data&quot;,
          },
        })
        setAnalysis(response.data)
      } catch (err) {
        setError(err.response?.data?.message || &quot;An error occurred while analyzing the resume&quot;)
      } finally {
        setLoading(false)
      }
    }
  }, [])

  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    onDrop,
    accept: {
      &quot;application/pdf&quot;: [&quot;.pdf&quot;],
    },
    multiple: false,
  })

  const resetAnalysis = () =&amp;gt; {
    setFile(null)
    setAnalysis(null)
    setError(null)
  }

  return (
    &amp;lt;div className=&quot;min-h-screen bg-gradient-primary px-12 [all:unset]&quot;&amp;gt;
      &amp;lt;div className=&quot;max-w-6xl  px-4 sm:px-6 lg:px-8 py-8&quot;&amp;gt;
         
        &amp;lt;div className=&quot;text-center mb-12&quot;&amp;gt;
          &amp;lt;div className=&quot;inline-flex items-center justify-center w-20 h-20 bg-white/20 rounded-full mb-6 backdrop-blur-glass&quot;&amp;gt;
            &amp;lt;FiFile size={40} className=&quot;text-white&quot; /&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;h1 className=&quot;text-5xl sm:text-6xl font-bold text-white mb-4 drop-shadow-lg&quot;&amp;gt;AI Resume Analyzer&amp;lt;/h1&amp;gt;
          &amp;lt;p className=&quot;text-xl text-white/90 max-w-2xl mx-auto&quot;&amp;gt;
            Get instant, AI-powered insights to optimize your resume and land your dream job
          &amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;

        {!analysis &amp;amp;&amp;amp; (
          &amp;lt;div className=&quot;glass-effect rounded-3xl p-8 mb-8 shadow-card animate-fade-in&quot;&amp;gt;
            &amp;lt;div
              {...getRootProps()}
              className={`relative transition-all duration-300 ease-in-out border-2 border-dashed rounded-2xl p-12 text-center cursor-pointer group ${
                isDragActive
                  ? &quot;border-primary-500 bg-primary-50 scale-[1.02] shadow-card-hover&quot;
                  : &quot;border-gray-300 hover:border-primary-400 hover:bg-gray-50/50&quot;
              }`}
            &amp;gt;
              &amp;lt;input {...getInputProps()} /&amp;gt;
              &amp;lt;div className=&quot;flex flex-col items-center&quot;&amp;gt;
                &amp;lt;div
                  className={`w-20 h-20 rounded-full flex items-center justify-center mb-6 transition-all duration-300 ${
                    isDragActive
                      ? &quot;bg-primary-100 scale-110&quot;
                      : &quot;bg-gray-100 group-hover:bg-primary-50 group-hover:scale-105&quot;
                  }`}
                &amp;gt;
                  &amp;lt;FiUpload
                    size={32}
                    className={`transition-colors duration-300 ${
                      isDragActive ? &quot;text-primary-600&quot; : &quot;text-gray-400 group-hover:text-primary-500&quot;
                    }`}
                  /&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;h3 className=&quot;text-2xl font-semibold text-gray-900 mb-2&quot;&amp;gt;
                  {isDragActive ? &quot;Drop your resume here!&quot; : &quot;Upload your resume&quot;}
                &amp;lt;/h3&amp;gt;
                &amp;lt;p className=&quot;text-gray-600 mb-6 text-lg&quot;&amp;gt;Drag and drop your PDF file here, or click to browse&amp;lt;/p&amp;gt;
                &amp;lt;button className=&quot;inline-flex items-center px-8 py-4 bg-gradient-primary text-white font-semibold rounded-xl hover:shadow-lg transform hover:scale-105 transition-all duration-200 focus:outline-none focus:ring-4 focus:ring-primary-200&quot;&amp;gt;
                  &amp;lt;FiUpload size={20} className=&quot;mr-2&quot; /&amp;gt;
                  Choose File
                &amp;lt;/button&amp;gt;
                &amp;lt;p className=&quot;text-sm text-gray-500 mt-4&quot;&amp;gt;Only PDF files are supported • Max 10MB&amp;lt;/p&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;

            {file &amp;amp;&amp;amp; (
              &amp;lt;div className=&quot;mt-6 p-4 bg-success-50 border border-success-200 rounded-xl animate-slide-up &quot;&amp;gt;
                &amp;lt;div className=&quot;flex items-center&quot;&amp;gt;
                  &amp;lt;FiCheckCircle size={24} className=&quot;text-success-500 mr-3&quot; /&amp;gt;
                  &amp;lt;div&amp;gt;
                    &amp;lt;p className=&quot;text-success-800 font-semibold&quot;&amp;gt;File uploaded successfully&amp;lt;/p&amp;gt;
                    &amp;lt;p className=&quot;text-success-600 text-sm&quot;&amp;gt;{file.name}&amp;lt;/p&amp;gt;
                  &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
              &amp;lt;/div&amp;gt;
            )}
          &amp;lt;/div&amp;gt;
        )}

    
        {loading &amp;amp;&amp;amp; (
          &amp;lt;div className=&quot;glass-effect rounded-3xl p-12 text-center mx-12 mb-8 shadow-card animate-fade-in&quot;&amp;gt;
            &amp;lt;div className=&quot;w-16 h-16 border-4 border-primary-200 border-t-primary-600 rounded-full animate-spin mx-auto mb-6&quot;&amp;gt;&amp;lt;/div&amp;gt;
            &amp;lt;h3 className=&quot;text-2xl font-semibold text-gray-900 mb-2&quot;&amp;gt;Analyzing Your Resume&amp;lt;/h3&amp;gt;
            &amp;lt;p className=&quot;text-gray-600 text-lg&quot;&amp;gt;Our AI is reviewing your resume for optimization opportunities...&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        )}

        {error &amp;amp;&amp;amp; (
          &amp;lt;div className=&quot;glass-effect rounded-3xl p-8 mb-8 shadow-card animate-fade-in&quot;&amp;gt;
            &amp;lt;div className=&quot;bg-danger-50 border border-danger-200 rounded-xl p-6&quot;&amp;gt;
              &amp;lt;div className=&quot;flex items-center&quot;&amp;gt;
                &amp;lt;FiAlertTriangle size={24} className=&quot;text-danger-500 mr-3&quot; /&amp;gt;
                &amp;lt;div&amp;gt;
                  &amp;lt;h4 className=&quot;text-danger-800 font-semibold&quot;&amp;gt;Analysis Failed&amp;lt;/h4&amp;gt;
                  &amp;lt;p className=&quot;text-danger-600&quot;&amp;gt;{error}&amp;lt;/p&amp;gt;
                &amp;lt;/div&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        )}

        {analysis &amp;amp;&amp;amp; (
          &amp;lt;div className=&quot;glass-effect rounded-3xl overflow-hidden shadow-card animate-fade-in&quot; style={{margin:&#39;40px&#39;}}&amp;gt;
 
            &amp;lt;div className=&quot;bg-gradient-success p-8 text-white&quot;&amp;gt;
              &amp;lt;div className=&quot;flex justify-between items-center flex-wrap gap-4&quot;&amp;gt;
                &amp;lt;div&amp;gt;
                  &amp;lt;h2 className=&quot;text-3xl font-bold mb-2&quot;&amp;gt;Analysis Complete&amp;lt;/h2&amp;gt;
                  &amp;lt;p className=&quot;text-green-100 text-lg&quot;&amp;gt;Here&#39;s your detailed resume breakdown&amp;lt;/p&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div className=&quot;text-right&quot;&amp;gt;
                  &amp;lt;div className=&quot;text-5xl font-bold&quot;&amp;gt;{analysis.overallScore}&amp;lt;/div&amp;gt;
                  &amp;lt;div className=&quot;text-green-100&quot;&amp;gt;Overall Score&amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;

            &amp;lt;div className=&quot;p-8&quot;&amp;gt;
     
              {analysis.summary &amp;amp;&amp;amp; (
                &amp;lt;div className=&quot;mb-8&quot;&amp;gt;
                  &amp;lt;h3 className=&quot;text-xl font-semibold text-gray-900 mb-4 flex items-center&quot;&amp;gt;
                    &amp;lt;FiClipboard size={24} className=&quot;text-info-500 mr-3&quot; /&amp;gt;
                    Executive Summary
                  &amp;lt;/h3&amp;gt;
                  &amp;lt;div className=&quot;bg-info-50 border-l-4 border-info-400 p-6 rounded-r-xl&quot;&amp;gt;
                    &amp;lt;p className=&quot;text-gray-700 leading-relaxed text-lg&quot;&amp;gt;{analysis.summary}&amp;lt;/p&amp;gt;
                  &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
              )}

     
              &amp;lt;div className=&quot;grid grid-cols-1 lg:grid-cols-3 gap-6&quot;&amp;gt;
   
                &amp;lt;div className=&quot;bg-success-50 border border-success-200 rounded-2xl p-6 hover:shadow-lg transition-shadow duration-300&quot;&amp;gt;
                  &amp;lt;div className=&quot;flex items-center mb-4&quot;&amp;gt;
                    &amp;lt;FiCheckCircle size={24} className=&quot;text-success-500 mr-3&quot; /&amp;gt;
                    &amp;lt;h3 className=&quot;text-xl font-semibold text-success-800&quot;&amp;gt;Strengths&amp;lt;/h3&amp;gt;
                  &amp;lt;/div&amp;gt;
                  &amp;lt;ul className=&quot;space-y-3&quot;&amp;gt;
                    {analysis.pros?.length &amp;gt; 0 ? (
                      analysis.pros.map((item, index) =&amp;gt; (
                        &amp;lt;li key={index} className=&quot;flex items-start text-success-700&quot;&amp;gt;
                          &amp;lt;span className=&quot;text-success-500 mr-3 mt-1 text-lg&quot;&amp;gt;•&amp;lt;/span&amp;gt;
                          &amp;lt;span className=&quot;text-sm leading-relaxed&quot;&amp;gt;{item}&amp;lt;/span&amp;gt;
                        &amp;lt;/li&amp;gt;
                      ))
                    ) : (
                      &amp;lt;li className=&quot;text-success-600 italic text-sm&quot;&amp;gt;No strengths identified&amp;lt;/li&amp;gt;
                    )}
                  &amp;lt;/ul&amp;gt;
                &amp;lt;/div&amp;gt;

             
                &amp;lt;div className=&quot;bg-danger-50 border border-danger-200 rounded-2xl p-6 hover:shadow-lg transition-shadow duration-300&quot;&amp;gt;
                  &amp;lt;div className=&quot;flex items-center mb-4&quot;&amp;gt;
                    &amp;lt;FiXCircle size={24} className=&quot;text-danger-500 mr-3&quot; /&amp;gt;
                    &amp;lt;h3 className=&quot;text-xl font-semibold text-danger-800&quot;&amp;gt;Areas to Improve&amp;lt;/h3&amp;gt;
                  &amp;lt;/div&amp;gt;
                  &amp;lt;ul className=&quot;space-y-3&quot;&amp;gt;
                    {analysis.cons?.length &amp;gt; 0 ? (
                      analysis.cons.map((item, index) =&amp;gt; (
                        &amp;lt;li key={index} className=&quot;flex items-start text-danger-700&quot;&amp;gt;
                          &amp;lt;span className=&quot;text-danger-500 mr-3 mt-1 text-lg&quot;&amp;gt;•&amp;lt;/span&amp;gt;
                          &amp;lt;span className=&quot;text-sm leading-relaxed&quot;&amp;gt;{item}&amp;lt;/span&amp;gt;
                        &amp;lt;/li&amp;gt;
                      ))
                    ) : (
                      &amp;lt;li className=&quot;text-danger-600 italic text-sm&quot;&amp;gt;No issues identified&amp;lt;/li&amp;gt;
                    )}
                  &amp;lt;/ul&amp;gt;
                &amp;lt;/div&amp;gt;

                &amp;lt;div className=&quot;bg-info-50 border border-info-200 rounded-2xl p-6 hover:shadow-lg transition-shadow duration-300&quot;&amp;gt;
                  &amp;lt;div className=&quot;flex items-center mb-4&quot;&amp;gt;
                    &amp;lt;FiZap size={24} className=&quot;text-info-500 mr-3&quot; /&amp;gt;
                    &amp;lt;h3 className=&quot;text-xl font-semibold text-info-800&quot;&amp;gt;Recommendations&amp;lt;/h3&amp;gt;
                  &amp;lt;/div&amp;gt;
                  &amp;lt;ul className=&quot;space-y-3&quot;&amp;gt;
                    {analysis.recommendations?.length &amp;gt; 0 ? (
                      analysis.recommendations.map((item, index) =&amp;gt; (
                        &amp;lt;li key={index} className=&quot;flex items-start text-info-700&quot;&amp;gt;
                          &amp;lt;span className=&quot;text-info-500 mr-3 mt-1 text-lg&quot;&amp;gt;•&amp;lt;/span&amp;gt;
                          &amp;lt;span className=&quot;text-sm leading-relaxed&quot;&amp;gt;{item}&amp;lt;/span&amp;gt;
                        &amp;lt;/li&amp;gt;
                      ))
                    ) : (
                      &amp;lt;li className=&quot;text-info-600 italic text-sm&quot;&amp;gt;No recommendations available&amp;lt;/li&amp;gt;
                    )}
                  &amp;lt;/ul&amp;gt;
                &amp;lt;/div&amp;gt;
              &amp;lt;/div&amp;gt;

              &amp;lt;div className=&quot;mt-8 flex justify-center gap-4 flex-wrap&quot;&amp;gt;
                
                &amp;lt;button
                  onClick={resetAnalysis}
                  className=&quot;inline-flex items-center px-8 py-4 bg-white border-2 border-gray-300 text-gray-700 font-semibold rounded-xl hover:bg-gray-50 hover:shadow-lg transform hover:scale-105 transition-all duration-200&quot;
                &amp;gt;
                  &amp;lt;FiRefreshCw size={20} className=&quot;mr-2&quot; /&amp;gt;
                  Analyze Another
                &amp;lt;/button&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        )}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default ResumeAnalysis
&lt;/pre&gt;


&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Running the Full Application&lt;/h2&gt;&lt;p&gt;Start the Backend: Run your Spring Boot application. It will start on &lt;b&gt;http://localhost:8080&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;Start the Frontend: In your resume-analyzer-ui directory, run the command npm start. Your browser will open to&lt;b&gt; http://localhost:3000&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;Now, you can upload a PDF resume file, and within seconds, you will see an analysis generated by the Gemini API.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Output:&lt;/h2&gt;&lt;div&gt;As we already discussed, the design should be changed according to your requirements.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjmG5wMDKoBMxhPZrzYNJZ7x_aCnCrowV0HqidHH6PFbMXl31js06JIpsDgSfyt5wKUzAVwiB_Lz3esVxMyDxmEC7PQoo2yd9NPaQUxTZO50_rsIMUntNJGOlYX8Y9m5wwjIAF1Z-PNRHg8zdYPVpXbELkWYqH1Nt0A2fwsXn7G99A4EpiJXcJ1N9ZXyag&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;801&quot; data-original-width=&quot;1910&quot; height=&quot;263&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjmG5wMDKoBMxhPZrzYNJZ7x_aCnCrowV0HqidHH6PFbMXl31js06JIpsDgSfyt5wKUzAVwiB_Lz3esVxMyDxmEC7PQoo2yd9NPaQUxTZO50_rsIMUntNJGOlYX8Y9m5wwjIAF1Z-PNRHg8zdYPVpXbELkWYqH1Nt0A2fwsXn7G99A4EpiJXcJ1N9ZXyag=w629-h263&quot; width=&quot;629&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEg37gGFbTbZZVlt-xwBgDOZiGWQvj8eBv8D2haHaokN3jzQ3Mh6xhBbUVQkih_lrxp6CStw21-61YSi5Vu3l2UGmEfkS9OZztXG3GQIUvIe16bbWmJ2An6uEyaoEw-Kbt7mR9sLgQtQwhZnGOgEmFfr8JGTkoxicsc-GjisycEtcGrU7MozCLdvbRwt7x8&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;744&quot; data-original-width=&quot;1919&quot; height=&quot;240&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEg37gGFbTbZZVlt-xwBgDOZiGWQvj8eBv8D2haHaokN3jzQ3Mh6xhBbUVQkih_lrxp6CStw21-61YSi5Vu3l2UGmEfkS9OZztXG3GQIUvIe16bbWmJ2An6uEyaoEw-Kbt7mR9sLgQtQwhZnGOgEmFfr8JGTkoxicsc-GjisycEtcGrU7MozCLdvbRwt7x8=w619-h240&quot; width=&quot;619&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiLFkLybYF7I-KQKNIbXTHraJQV7GxjrARVQRo8lgYCjnYDioxHwrCpspM8Rzmqp5w4rwueNojT757ai9qTRbl10otHxZcV03GrrQ6kDStAZ_sx9aFbqUwtH4XubXJpOffprN_BZ5-bWkCNmxOvtXw2SR-7ZlcV_3F4hapBWur1JB3-H9uXoDGEyLharvI&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;798&quot; data-original-width=&quot;1912&quot; height=&quot;265&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiLFkLybYF7I-KQKNIbXTHraJQV7GxjrARVQRo8lgYCjnYDioxHwrCpspM8Rzmqp5w4rwueNojT757ai9qTRbl10otHxZcV03GrrQ6kDStAZ_sx9aFbqUwtH4XubXJpOffprN_BZ5-bWkCNmxOvtXw2SR-7ZlcV_3F4hapBWur1JB3-H9uXoDGEyLharvI&quot; width=&quot;636&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Potential Enhancements&lt;/h2&gt;&lt;p&gt;This is a fantastic starting point. Here are some ways you can enhance this project:&lt;/p&gt;&lt;p&gt;&lt;b&gt;Database Integration:&lt;/b&gt; Save the analysis results to a database like PostgreSQL.&lt;/p&gt;&lt;p&gt;&lt;b&gt;User Accounts:&lt;/b&gt; Implement Spring Security to allow users to sign up and view their history of analyzed resumes.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Improved UI:&lt;/b&gt; Use a component library like Material-UI or Ant Design to create a more sophisticated user interface.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Congratulations! You have successfully built a full-stack resume analysis web application using Spring Boot, ReactJS, and the Google Gemini API. This project demonstrates how you can integrate powerful generative AI capabilities into traditional web applications to create truly intelligent and useful tools. By mastering this stack, you are well-equipped to build the next generation of smart software.&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/7793627313251829882/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2025/10/how-to-build-resume-analysis-web-app.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7793627313251829882'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7793627313251829882'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2025/10/how-to-build-resume-analysis-web-app.html' title='How to Build a Resume Analysis Web App with Spring Boot, React, and Gemini API: A Step-by-Step Guide'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEjmG5wMDKoBMxhPZrzYNJZ7x_aCnCrowV0HqidHH6PFbMXl31js06JIpsDgSfyt5wKUzAVwiB_Lz3esVxMyDxmEC7PQoo2yd9NPaQUxTZO50_rsIMUntNJGOlYX8Y9m5wwjIAF1Z-PNRHg8zdYPVpXbELkWYqH1Nt0A2fwsXn7G99A4EpiJXcJ1N9ZXyag=s72-w629-h263-c" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-2618652525583044818</id><published>2025-10-26T18:03:00.004+05:30</published><updated>2025-10-26T18:03:47.219+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Build with me"/><title type='text'>Mastering JPA Relationships: A Complete Guide with a Real-World Project</title><content type='html'>&lt;p&gt;In this article, We will go throught all the important concept of the JPA Mapping with across the tables present in the databases.&lt;/p&gt;&lt;p&gt;So, lets first see about Hibernate, Hibernate, are the backbone of data persistence in the Java world. While basic &lt;a href=&quot;https://www.docodehere.com/2025/10/crud-spring-boot-react-mysql-guide.html&quot; target=&quot;_blank&quot;&gt;CRUD operations&lt;/a&gt; are straightforward because only do the basic operation like create,read, update and delete with only table, things get tricky when you start modeling real-world connections between your data or uses multiple tables the are realted to each other or you can say that are mapped together. NullPointerExceptions, infinite loops during JSON serialization, and confusing database schemas often stem from misunderstood JPA relationships.&lt;/p&gt;&lt;p&gt;So, in this article we will learn terms like mappedBy, CascadeType, or @JoinTable, and many more.&lt;/p&gt;&lt;p&gt;Using this article, we not only learn this terms, instead we will build the real-world application to get practical knowledge of it. We will build a mini blog application where we try to understood but multiple data relation with each other.&lt;/p&gt;&lt;p&gt;By the end of this article, you will have understanding of the following:&lt;/p&gt;&lt;p&gt;&lt;b&gt;@OneToOne: &lt;/b&gt;For linking a user to their profile. Every Users have one profile.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@OneToMany &lt;/b&gt;&amp;amp; &lt;b&gt;@ManyToOne:&lt;/b&gt; For Linking a user to the posts that they have written.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@ManyToMany:&lt;/b&gt; For linking posts to their corresponding mulitple tags.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Project Setup: The Blogging Platform&lt;/h2&gt;&lt;p&gt;We are building a simple blog application. Where we will create our core entities like: User, UserProfile, Post, and Tag. We will use these entity to demonstrate each relationship type of JPA.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Let&#39;s starts,&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;1. The @OneToOne Relationship: User and UserProfile&lt;/h3&gt;&lt;p&gt;A one-to-one relationship is the simplest realtionship annotation. In our blog, we want each User to have exactly one UserProfile containing extra details like a bio or a profile picture URL.&lt;/p&gt;&lt;p&gt;&lt;b&gt;The Concept:&lt;/b&gt; One User record corresponds to one UserProfile record.&lt;/p&gt;&lt;p&gt;&lt;b&gt;The User Entity&lt;/b&gt;&lt;/p&gt;&lt;p&gt;We will create the User Entity, with Id which is unique and can be generated automatic, username, password.&lt;/p&gt;&lt;p&gt;The &quot;owning&quot; side of the relationship is the entity that holds the &lt;b&gt;foreign key&lt;/b&gt;. In our case, we&#39;ll add a &lt;b&gt;user_profile_id&lt;/b&gt; column to our users table. Means User table have a foriegn key which make a realtionship with the profile table.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
// src/main/java/com/yourblog/model/User.java
import jakarta.persistence.*;
// other imports

@Entity
@Table(name = &quot;users&quot;)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String username;

    private String password;

    // The OneToOne mapping starts here
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = &quot;user_profile_id&quot;, referencedColumnName = &quot;id&quot;)
    private UserProfile userProfile;

    // Getters and Setters
}
&lt;/pre&gt;


&lt;/p&gt;&lt;p&gt;&lt;b&gt;The UserProfile Entity &lt;/b&gt;(Referenced Side)&lt;/p&gt;&lt;p&gt;We will create the UserProfile Entity which contain id, bioand profile picture url.&lt;/p&gt;&lt;p&gt;The other side of the relationship uses the mappedBy attribute to indicate that the relationship is already configured by the User entity. It&#39;s means we told that the relation is already defined by the user entity.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
// src/main/java/com/yourblog/model/User.java
import jakarta.persistence.*;
// other imports

@Entity
@Table(name = &quot;users&quot;)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String username;

    private String password;

    // The OneToOne mapping starts here
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = &quot;user_profile_id&quot;, referencedColumnName = &quot;id&quot;)
    private UserProfile userProfile;

    // Getters and Setters
}
&lt;/pre&gt;

&lt;/p&gt;&lt;p&gt;&lt;b&gt;Key Points Explained:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;@OneToOne:&lt;/b&gt; Declares the relationship.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@JoinColumn(name = &quot;user_profile_id&quot;):&lt;/b&gt; Specifies the foreign key column in the users table. This makes User the owner of the relationship.&lt;/p&gt;&lt;p&gt;&lt;b&gt;mappedBy = &quot;userProfile&quot;:&lt;/b&gt; On the UserProfile side, this tells JPA, &quot;Don&#39;t create another foreign key. The configuration for this relationship is already defined by the userProfile field in the User entity.&quot;&lt;/p&gt;&lt;p&gt;&lt;b&gt;cascade = CascadeType.ALL: &lt;/b&gt;A convenient feature. If you save a User, its associated UserProfile is also saved. If you delete a User, the UserProfile is deleted too.&lt;/p&gt;&lt;p&gt;&lt;b&gt;orphanRemoval = true:&lt;/b&gt; If you set user.setUserProfile(null), the previously associated UserProfile will be deleted from the database because it no longer has an owner.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;2. The @OneToMany &amp;amp; @ManyToOne Relationships: User and Post&lt;/h3&gt;&lt;p&gt;This is the most common type of relationship. A single user can write many blog posts, but each blog post is written by only one user.&lt;/p&gt;&lt;p&gt;&lt;b&gt;The Concept:&lt;/b&gt; A single User record can be associated with multiple Post records.&lt;/p&gt;&lt;p&gt;&lt;b&gt;The Post Entity&lt;/b&gt; (The &quot;Many&quot; Side and Owning Side)&lt;/p&gt;&lt;p&gt;The &quot;Many&quot; side almost always owns the relationship because it&#39;s more logical for the posts table to have a &lt;b&gt;user_id column&lt;/b&gt; than for the users table to hold a list of post IDs.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


  &lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
// src/main/java/com/yourblog/model/Post.java
import jakarta.persistence.*;
// other imports

@Entity
@Table(name = &quot;posts&quot;)
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String title;

    @Lob // For large text fields
    private String content;

    // The ManyToOne mapping starts here
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = &quot;user_id&quot;, nullable = false)
    private User author;

    // Getters and Setters
}
&lt;/pre&gt;


&lt;/p&gt;&lt;p&gt;&lt;b&gt;The User Entity &lt;/b&gt;(The &quot;One&quot; Side)&lt;/p&gt;&lt;p&gt;Now, we update the User entity to hold a list of all their posts.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
// src/main/java/com/yourblog/model/User.java
// ... existing User code ...

@Entity
@Table(name = &quot;users&quot;)
public class User {
    // ... id, username, userProfile ...

    // The OneToMany mapping starts here
    @OneToMany(
        mappedBy = &quot;author&quot;,
        cascade = CascadeType.ALL,
        orphanRemoval = true
    )
    private List&lt;Post&gt; posts = new ArrayList&lt;&gt;();

    // ... Getters and Setters ...

    // Helper methods to keep both sides of the relationship in sync
    public void addPost(Post post) {
        posts.add(post);
        post.setAuthor(this);
    }

    public void removePost(Post post) {
        posts.remove(post);
        post.setAuthor(null);
    }
}
&lt;/pre&gt;

&lt;/p&gt;&lt;p&gt;&lt;b&gt;Key Points Explained:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;@ManyToOne:&lt;/b&gt; In the Post entity, this declares that many posts can be linked to one user.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@JoinColumn(name = &quot;user_id&quot;): &lt;/b&gt;This creates the user_id foreign key column in the posts table. The Post is the owner of the relationship.&lt;/p&gt;&lt;p&gt;&lt;b&gt;fetch = FetchType.LAZY: &lt;/b&gt;This is a critical performance optimization. When you load a Post, JPA will not automatically load its author. It will only be fetched when you explicitly call &lt;b&gt;post.getAuthor()&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@OneToMany&lt;/b&gt;: In the User entity, this declares that one user can have many posts.&lt;/p&gt;&lt;p&gt;&lt;b&gt;mappedBy = &quot;author&quot;: &lt;/b&gt;This is essential! It tells JPA that the Post entity is the owner and the relationship is mapped by the author field in the Post class. Without this, JPA would try to create a third &quot;join table,&quot; which is not what we want here.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;3. The @ManyToMany Relationship: Post and Tag&lt;/h3&gt;&lt;p&gt;This is the most complex relationship. A single post can have many tags (e.g., &quot;Java,&quot; &quot;JPA,&quot; &quot;Spring&quot;), and a single tag can be applied to many different posts.&lt;/p&gt;&lt;p&gt;&lt;b&gt;The Concept:&lt;/b&gt; You need a third table, known as a join table or link table, to manage these associations. This table will only contain two columns: post_id and tag_id.&lt;/p&gt;&lt;p&gt;The Post and Tag Entities&lt;/p&gt;&lt;p&gt;Let&#39;s first define the simple Tag entity.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


  &lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
// src/main/java/com/yourblog/model/Tag.java
import jakarta.persistence.*;
// other imports

@Entity
@Table(name = &quot;tags&quot;)
public class Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String name;

    @ManyToMany(mappedBy = &quot;tags&quot;)
    private Set&lt;Post&gt; posts = new HashSet&lt;&gt;();

    // Getters and Setters
}
&lt;/pre&gt;


&lt;/p&gt;&lt;p&gt;Now, let&#39;s update the Post entity to be the owner of the relationship.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



  &lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;
// src/main/java/com/yourblog/model/Post.java
// ... existing Post code ...

@Entity
@Table(name = &quot;posts&quot;)
public class Post {
    // ... id, title, content, author ...

    // The ManyToMany mapping starts here
    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(
        name = &quot;post_tags&quot;,
        joinColumns = @JoinColumn(name = &quot;post_id&quot;),
        inverseJoinColumns = @JoinColumn(name = &quot;tag_id&quot;)
    )
    private Set&lt;Tag&gt; tags = new HashSet&lt;&gt;();
    
    // Getters and Setters
}
&lt;/pre&gt;

&lt;/p&gt;&lt;p&gt;&lt;b&gt;Key Points Explained:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;@ManyToMany:&lt;/b&gt; Declares the relationship on both entities.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@JoinTable:&lt;/b&gt; This annotation is used on the owning side (Post in our case) to configure the join table.&lt;/p&gt;&lt;p&gt;&lt;b&gt;name = &quot;post_tags&quot;:&lt;/b&gt; The name of our new join table.&lt;/p&gt;&lt;p&gt;&lt;b&gt;joinColumns:&lt;/b&gt; Defines the foreign key column in the join table that refers to the owning entity (Post).&lt;/p&gt;&lt;p&gt;&lt;b&gt;inverseJoinColumns&lt;/b&gt;: Defines the foreign key column that refers to the other side (Tag).&lt;/p&gt;&lt;p&gt;&lt;b&gt;mappedBy = &quot;tags&quot;:&lt;/b&gt; Just like before, this is used on the non-owning side (Tag) to point back to the field that configured the relationship.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Set vs List:&lt;/b&gt; We use Set here because the order of tags for a post doesn&#39;t matter, and we want to ensure no duplicate tags are associated with a single post.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;How All tables is Created in Databases After Ruuning our backend applicaiton&amp;nbsp;&lt;/h2&gt;&lt;p&gt;&lt;b&gt;user_profiles&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgMMxG82NFjsFsoKtaDxQpJFPZpmOkjjx7QOJaM74cxoXLle8qiETMb-Wldur-39qIRDgqqLKieuoLBhZTW6owvS9VGP76oQDPIfQ8zGfdFb2l5R6z-Ka1gHtNbgjwkxjMkpbU3WkHHAYESRDfDgzY2WaYyeB3iOPfyAbRJrebQTwrlhdCqc-U10_4Gp3M&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;182&quot; data-original-width=&quot;1016&quot; height=&quot;111&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgMMxG82NFjsFsoKtaDxQpJFPZpmOkjjx7QOJaM74cxoXLle8qiETMb-Wldur-39qIRDgqqLKieuoLBhZTW6owvS9VGP76oQDPIfQ8zGfdFb2l5R6z-Ka1gHtNbgjwkxjMkpbU3WkHHAYESRDfDgzY2WaYyeB3iOPfyAbRJrebQTwrlhdCqc-U10_4Gp3M=w616-h111&quot; width=&quot;616&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;users&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgLtwzcY1SbxomhsrbsCmbgKMsIIYMKn9avU_a5ITc3VA7jTMhFIIx2qzFemn-EDbhUHVbZDas4Q9fhgjlEaFWw3Jo7McR_th9wSKMpZzHYid5XXuyPD1ZnS8NsmdA9ZocapwdkLL_I20mGpzZEGjk9Yk5fXM5w2SLMNbNyHJ9UwE7Xoct9Rc8bO0avT4E&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;158&quot; data-original-width=&quot;1039&quot; height=&quot;95&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgLtwzcY1SbxomhsrbsCmbgKMsIIYMKn9avU_a5ITc3VA7jTMhFIIx2qzFemn-EDbhUHVbZDas4Q9fhgjlEaFWw3Jo7McR_th9wSKMpZzHYid5XXuyPD1ZnS8NsmdA9ZocapwdkLL_I20mGpzZEGjk9Yk5fXM5w2SLMNbNyHJ9UwE7Xoct9Rc8bO0avT4E=w620-h95&quot; width=&quot;620&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;tags&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEixH3R7xi-doq08HoZrLK2H45_Wmv5YFs-TfE0whiSRLaSbnhFROsUhiALg4ThAJH5yZXMUEOg6ejLeHiaTkGuTZIfJBGh8Pr_Kgw94kw_XVZf2NsaHKmOpPhebC2jsvFbE-oiOYzeBnJFtP5ad3mDntTT9jlOtshT4gWSGx0vDcrVpXEYDEZ2zzrjzGrw&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;256&quot; data-original-width=&quot;1015&quot; height=&quot;156&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEixH3R7xi-doq08HoZrLK2H45_Wmv5YFs-TfE0whiSRLaSbnhFROsUhiALg4ThAJH5yZXMUEOg6ejLeHiaTkGuTZIfJBGh8Pr_Kgw94kw_XVZf2NsaHKmOpPhebC2jsvFbE-oiOYzeBnJFtP5ad3mDntTT9jlOtshT4gWSGx0vDcrVpXEYDEZ2zzrjzGrw=w616-h156&quot; width=&quot;616&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;posts&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiqr_KF9QcVGVZUAJsz77_hyG-TyRvEzLt6SRZcX488Wd-8d2ZzcaPBXRACMPx64qkTDEW4nQ68u5hdK4uKYevcEuT5tr_Mtgv3bqGgJyfnnCQd9SpMBiZXElR3SjUwhvPcZmW2MX8QCx-Y0vPfwuRru-PaOiG0zLJb8ueW5GqoGxD-ynM1p-cu7Rj_Mlg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;216&quot; data-original-width=&quot;1059&quot; height=&quot;125&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiqr_KF9QcVGVZUAJsz77_hyG-TyRvEzLt6SRZcX488Wd-8d2ZzcaPBXRACMPx64qkTDEW4nQ68u5hdK4uKYevcEuT5tr_Mtgv3bqGgJyfnnCQd9SpMBiZXElR3SjUwhvPcZmW2MX8QCx-Y0vPfwuRru-PaOiG0zLJb8ueW5GqoGxD-ynM1p-cu7Rj_Mlg=w615-h125&quot; width=&quot;615&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;posts_tags&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; font-weight: bold; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiRF6364Ph-nFbxN3ftf4xQgxse9RkSqa_VtNjbkXBnT1xDFBkapGETJYrcF1IfniYduirJ_uZhf7PatgYuQDyicn3rD7vUn0k-lGloUeH1yKs-JTevFvsLsvvHdqamJOXXmE8QMi3KvVOzNXmuGqOmXSZuI9MdFKaVkovjAf-vnDseuw-2Xv4K-s2yqdY&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;267&quot; data-original-width=&quot;1027&quot; height=&quot;160&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiRF6364Ph-nFbxN3ftf4xQgxse9RkSqa_VtNjbkXBnT1xDFBkapGETJYrcF1IfniYduirJ_uZhf7PatgYuQDyicn3rD7vUn0k-lGloUeH1yKs-JTevFvsLsvvHdqamJOXXmE8QMi3KvVOzNXmuGqOmXSZuI9MdFKaVkovjAf-vnDseuw-2Xv4K-s2yqdY=w617-h160&quot; width=&quot;617&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusion: From Confusion to Clarity&lt;/h2&gt;&lt;p&gt;By modeling a real-world project, we&#39;ve transformed abstract annotations into a concrete, understandable database structure.&lt;/p&gt;&lt;p&gt;Let&#39;s recap our journey:&lt;/p&gt;&lt;p&gt;&lt;b&gt;@OneToOne&lt;/b&gt; is perfect for optional, singular details, like a User and UserProfile.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@OneToMany &lt;/b&gt;and &lt;b&gt;@ManyToOne&lt;/b&gt; are your workhorses for parent-child relationships, like a User and their Posts. Remember to place the &lt;b&gt;@ManyToOne&lt;/b&gt; on the &quot;child&quot; and make it the owner.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@ManyToMany&lt;/b&gt; handles complex webs of connections, like Posts and Tags, by using a dedicated join table.&lt;/p&gt;&lt;p&gt;Understanding these relationships is the key to unlocking the full power of JPA and building robust, scalable Java applications. Start by identifying the relationships in your own projects and see if you can model them.&lt;/p&gt;&lt;p&gt;What other JPA challenges do you face? Share them in the comments below!&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/2618652525583044818/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2025/10/jpa-mapping-spring-boot.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/2618652525583044818'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/2618652525583044818'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2025/10/jpa-mapping-spring-boot.html' title='Mastering JPA Relationships: A Complete Guide with a Real-World Project'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEgMMxG82NFjsFsoKtaDxQpJFPZpmOkjjx7QOJaM74cxoXLle8qiETMb-Wldur-39qIRDgqqLKieuoLBhZTW6owvS9VGP76oQDPIfQ8zGfdFb2l5R6z-Ka1gHtNbgjwkxjMkpbU3WkHHAYESRDfDgzY2WaYyeB3iOPfyAbRJrebQTwrlhdCqc-U10_4Gp3M=s72-w616-h111-c" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-2879875754584011479</id><published>2025-10-25T20:03:00.001+05:30</published><updated>2025-10-27T17:17:18.967+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Build with me"/><title type='text'>Securing APIs with Spring Security and JWT: A Full-Stack Guide for Spring Boot &amp; React</title><content type='html'>&lt;p&gt;In our previous article on creating a &lt;b&gt;&lt;a href=&quot;https://www.docodehere.com/2025/10/crud-spring-boot-react-mysql-guide.html&quot; target=&quot;_blank&quot;&gt;CRUD application&lt;/a&gt;&lt;/b&gt;, we built a fully functional user management system (CRUD Operation). However, it has a critical vulnerability: all its API endpoints are public. Anyone can create, read, update, or delete users without any checks. It&#39;s time to lock it down.&lt;/p&gt;&lt;p&gt;So, this article will teach you how to implement robust, token-based authentication using &lt;b&gt;Spring Security&lt;/b&gt; and &lt;b&gt;JSON Web Tokens (JWT)&lt;/b&gt;. We will secure our Spring Boot backend application and modify our React frontend to handle user registration, login, and authenticated requests.&lt;/p&gt;&lt;p&gt;By the end of this article, you&#39;ll understand how to protect your application and manage user sessions in a modern, stateless way, which is perfect for Single-Page Applications (SPAs) like React.&lt;/p&gt;&lt;p&gt;So, let&#39;s get started,&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Prerequisites&lt;/h2&gt;&lt;p&gt;In this tutorial, we will implement security in our previous&amp;nbsp;&lt;b&gt;&lt;a href=&quot;https://www.docodehere.com/2025/10/crud-spring-boot-react-mysql-guide.html&quot; target=&quot;_blank&quot;&gt;CRUD application&lt;/a&gt;&lt;/b&gt;. You should have:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;A working Spring Boot + React CRUD application.&lt;/li&gt;&lt;li&gt;Basic knowledge of &lt;b&gt;Spring Boot &lt;/b&gt;and &lt;b&gt;React&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;JDK, Maven, Node.js, and MySQL are installed.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 1: Understanding the JWT Authentication Flow&lt;/h2&gt;&lt;p&gt;Before we write any code, let&#39;s understand how&lt;b&gt; JWT authentication &lt;/b&gt;works. Unlike traditional session-based authentication, JWT is &lt;b&gt;stateless.&lt;/b&gt; This means the server doesn&#39;t need to store session information, which makes our application highly scalable.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Here&#39;s the flow:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Login:&lt;/b&gt; The user sends their username and password to the backend application.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Validation:&lt;/b&gt; The server validates the credentials with our database&#39;s stored credentials.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Token Generation:&lt;/b&gt; If valid, the server generates a &lt;b&gt;signed JWT token&lt;/b&gt;. This token contains user information (like username and roles) and an &lt;b&gt;expiration date&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Token to Client:&lt;/b&gt; The server sends this JWT back to the client.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Token Storage:&lt;/b&gt; The React app stores the JWT, typically in localStorage, which can be used further.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Authenticated Requests: &lt;/b&gt;For every subsequent request to a protected API endpoint, the client includes the JWT in the Authorization header as a Bearer Token, which is used as an authentic key.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Server Verification:&lt;/b&gt; A &lt;b&gt;security filter&lt;/b&gt; on the server intercepts the request, validates the JWT&#39;s signature and expiration, and if the key is valid, then processes the request; otherwise, it gives an error &lt;b&gt;401 unauthorized access&lt;/b&gt;.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 2: Enhancing the Spring Boot Backend for Security&lt;/h2&gt;&lt;p&gt;Let&#39;s start by adding the necessary security layers to our backend application. Follow the necessary steps for your application.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 1: Add New Dependencies&lt;/h3&gt;&lt;p&gt;Open your &lt;b&gt;pom.xml&lt;/b&gt; and add the dependencies for Spring Security and JWT. We&#39;ll use the popular jjwt library. This is the following dependency that we have to add in our pom.xml, you should have to use the latest depency in your application. You can easily find these dependencies from the&amp;nbsp;&lt;a href=&quot;https://mvnrepository.com/&quot;&gt;Maven Repository&lt;/a&gt;&lt;/p&gt;&lt;p&gt;


  &lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;  
  		&amp;lt;dependency&amp;gt;
			&amp;lt;groupid&amp;gt;org.springframework.boot&amp;lt;/groupid&amp;gt;
			&amp;lt;artifactid&amp;gt;spring-boot-starter-security&amp;lt;/artifactid&amp;gt;
		&amp;lt;/dependency&amp;gt;
		&amp;lt;dependency&amp;gt;
			&amp;lt;groupid&amp;gt;io.jsonwebtoken&amp;lt;/groupid&amp;gt;
			&amp;lt;artifactid&amp;gt;jjwt-impl&amp;lt;/artifactid&amp;gt;
			&amp;lt;version&amp;gt;0.12.6&amp;lt;/version&amp;gt;
			&amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
		&amp;lt;/dependency&amp;gt;

		&amp;lt;dependency&amp;gt;
			&amp;lt;groupid&amp;gt;io.jsonwebtoken&amp;lt;/groupid&amp;gt;
			&amp;lt;artifactid&amp;gt;jjwt-jackson&amp;lt;/artifactid&amp;gt;
			&amp;lt;version&amp;gt;0.12.6&amp;lt;/version&amp;gt;
			&amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
		&amp;lt;/dependency&amp;gt;

		&amp;lt;dependency&amp;gt;
			&amp;lt;groupid&amp;gt;io.jsonwebtoken&amp;lt;/groupid&amp;gt;
			&amp;lt;artifactid&amp;gt;jjwt-api&amp;lt;/artifactid&amp;gt;
			&amp;lt;version&amp;gt;0.12.6&amp;lt;/version&amp;gt;
		&amp;lt;/dependency&amp;gt;
     &lt;/pre&gt;
  

&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Configure Application Properties for JWT&lt;/h3&gt;&lt;p&gt;Add a secret key and token expiration time to your application.properties, so that we will use it in our application codebase. And remember, never hardcode secrets in your code!&lt;/p&gt;&lt;p&gt;&lt;b&gt;properties&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;# JWT Configuration
jwt.secret=your-super-secret-key-that-is-long-and-secure-enough
jwt.expiration.ms=86400000 # 24 hours

&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Create JWT Utility Class&lt;/h3&gt;&lt;p&gt;We will create a class that will handle generating, parsing, and validating JWT tokens. So, the code below does the task of generating the JWT token from the username and adds the expiration time to it. And also, we will create a function that will fetch the username from the token. And we will create the validateToken function for validating the token, which will return either true or false based on the authenticity of the token.&lt;/p&gt;&lt;p&gt;&lt;b&gt;src/main/java/com/example/crudbackend/security/JwtTokenProvider.java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.security;

import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JwtTokenProvider {

    @Value(&quot;${jwt.secret}&quot;)
    private String jwtSecret;

    @Value(&quot;${jwt.expiration.ms}&quot;)
    private int jwtExpirationInMs;

    public String generateToken(Authentication authentication) {
        // In a real app, you&#39;d get the user principal
        String username = authentication.getName();
        Date now = new Date();
        Date expiryDate = new Date(now.getTime() + jwtExpirationInMs);

        return Jwts.builder()
                .setSubject(username)
                .setIssuedAt(new Date())
                .setExpiration(expiryDate)
                .signWith(SignatureAlgorithm.HS512, jwtSecret)
                .compact();
    }

    public String getUsernameFromJWT(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(jwtSecret)
                .parseClaimsJws(token)
                .getBody();
        return claims.getSubject();
    }

    public boolean validateToken(String authToken) {
        try {
            Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);
            return true;
        } catch (SignatureException | MalformedJwtException | ExpiredJwtException | UnsupportedJwtException | IllegalArgumentException ex) {
            // Log the error
        }
        return false;
    }
}

&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 4: Create a Custom UserDetailsService&lt;/h3&gt;&lt;p&gt;Spring Security needs to know how to load user data from our MySQL database. We&#39;ll implement the UserDetailsService interface.&lt;/p&gt;&lt;p&gt;So, basically, we have to tell Spring Security that we will consider the email as a username for authentication, and for that, we will implement the custom userDetailService.&lt;/p&gt;&lt;p&gt;First, add a password field to your User entity and update UserRepository to find a user by email.&lt;/p&gt;&lt;p&gt;User.java (add password field):&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;private String password;
&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;UserRepository.java (add new method):&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import java.util.Optional;
// ...
Optional&lt;user&gt; findByEmail(String email);
&lt;/user&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Now, create the service.&lt;/p&gt;&lt;p&gt;src/main/java/com/example/crudbackend/service/CustomUserDetailsService.java&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.service;

import com.example.crudbackend.model.User;
import com.example.crudbackend.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = userRepository.findByEmail(email)
                .orElseThrow(() -&amp;gt; new UsernameNotFoundException(&quot;User not found with email: &quot; + email));

        return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), new ArrayList&amp;lt;&amp;gt;());
    }
}

&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 5: Create the JWT Authentication Filter&lt;/h3&gt;&lt;p&gt;This filter will execute once per request. It will read the JWT from the header, validate it, and set the user&#39;s authentication in the security context.&lt;/p&gt;&lt;p&gt;src/main/java/com/example/crudbackend/security/JwtAuthenticationFilter.java&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.security;

import com.example.crudbackend.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    private JwtTokenProvider tokenProvider;
    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            String jwt = getJwtFromRequest(request);
            if (StringUtils.hasText(jwt) &amp;amp;&amp;amp; tokenProvider.validateToken(jwt)) {
                String username = tokenProvider.getUsernameFromJWT(jwt);
                UserDetails userDetails = customUserDetailsService.loadUserByUsername(username);
                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authentication);
            }
        } catch (Exception ex) {
            logger.error(&quot;Could not set user authentication in security context&quot;, ex);
        }
        filterChain.doFilter(request, response);
    }

    private String getJwtFromRequest(HttpServletRequest request) {
        String bearerToken = request.getHeader(&quot;Authorization&quot;);
        if (StringUtils.hasText(bearerToken) &amp;amp;&amp;amp; bearerToken.startsWith(&quot;Bearer &quot;)) {
            return bearerToken.substring(7);
        }
        return null;
    }
}

&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 6: Configure Spring Security&lt;/h3&gt;&lt;p&gt;This is the main configuration file where we tie everything together. We&#39;ll define which endpoints are public (like login/register) and which are protected.&lt;/p&gt;&lt;p&gt;So, this is the main file, where we will configure the public or protected api, and other authentication things, CORS-related configuration, and many more.&lt;/p&gt;&lt;p&gt;src/main/java/com/example/crudbackend/config/SecurityConfig.java&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.config;

import com.example.crudbackend.security.JwtAuthenticationFilter;
import com.example.crudbackend.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() {
        return new JwtAuthenticationFilter();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean(BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .cors()
            .and()
            .csrf().disable() // We disable CSRF because we are using JWT
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers(&quot;/api/auth/**&quot;).permitAll() // Public endpoints
            .anyRequest().authenticated(); // All other endpoints require authentication

        // Add our custom JWT security filter
        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }
}
&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 7: Create an Authentication Controller&lt;/h3&gt;&lt;p&gt;Finally, we need endpoints for user registration and login.&lt;/p&gt;&lt;p&gt;So, we will create a login and registration api where we take a request from the user and generate and validate the JWT by our predefined function that we will write already in the above files.&lt;/p&gt;&lt;p&gt;And when a user first registers at that time, we have to save the user&#39;s details to the database.&lt;/p&gt;&lt;p&gt;src/main/java/com/example/crudbackend/controller/AuthController.java&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.controller;

import com.example.crudbackend.model.User;
import com.example.crudbackend.payload.LoginRequest;
import com.example.crudbackend.payload.JwtAuthenticationResponse;
import com.example.crudbackend.repository.UserRepository;
import com.example.crudbackend.security.JwtTokenProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = &quot;http://localhost:3000&quot;)
@RestController
@RequestMapping(&quot;/api/auth&quot;)
public class AuthController {

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    UserRepository userRepository;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    JwtTokenProvider tokenProvider;

    @PostMapping(&quot;/login&quot;)
    public ResponseEntity authenticateUser(@RequestBody LoginRequest loginRequest) {
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(loginRequest.getEmail(), loginRequest.getPassword())
        );

        SecurityContextHolder.getContext().setAuthentication(authentication);
        String jwt = tokenProvider.generateToken(authentication);
        return ResponseEntity.ok(new JwtAuthenticationResponse(jwt));
    }

    @PostMapping(&quot;/register&quot;)
    public ResponseEntity registerUser(@RequestBody User signUpRequest) {
        if (userRepository.findByEmail(signUpRequest.getEmail()).isPresent()) {
            return ResponseEntity.badRequest().body(&quot;Email Address already in use!&quot;);
        }

        // Creating user&#39;s account
        User user = new User();
        user.setFirstName(signUpRequest.getFirstName());
        user.setLastName(signUpRequest.getLastName());
        user.setEmail(signUpRequest.getEmail());
        user.setPassword(passwordEncoder.encode(signUpRequest.getPassword()));

        userRepository.save(user);
        return ResponseEntity.ok(&quot;User registered successfully&quot;);
    }
}

// Create simple payload classes for LoginRequest and JwtAuthenticationResponse
// e.g., LoginRequest.java with String email, String password
// e.g., JwtAuthenticationResponse.java with String accessToken

&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;The backend is now secure! All requests to /api/v1/users/** will be rejected with a 401 Unauthorized error unless a valid JWT is provided.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 3: Updating the React Frontend&lt;/h2&gt;&lt;p&gt;Now, our backend is ready now, let&#39;s make our React app aware of authentication.&amp;nbsp;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 1: Create Login/Register Components&lt;/h3&gt;&lt;p&gt;Create new components for login and registration forms. For brevity, we&#39;ll focus on the logic. You can use simple Bootstrap forms.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Update the API Service&lt;/h3&gt;&lt;p&gt;Modify UserService.js to include login/register methods and set up an &lt;b&gt;Axios interceptor&lt;/b&gt;. This interceptor will automatically add the JWT to the header of every API call. This is the most elegant way to handle tokens. So, when the user signs in, it sends the request to the backend, and from the backend, we get a token, and that token we will save to our local storage&lt;/p&gt;&lt;p&gt;src/services/AuthService.js (a new service file)&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import axios from &#39;axios&#39;;

const API_URL = &#39;http://localhost:8080/api/auth/&#39;;

class AuthService {
    login(email, password) {
        return axios
            .post(API_URL + &#39;login&#39;, { email, password })
            .then(response =&amp;gt; {
                if (response.data.accessToken) {
                    localStorage.setItem(&#39;user&#39;, JSON.stringify(response.data));
                }
                return response.data;
            });
    }

    logout() {
        localStorage.removeItem(&#39;user&#39;);
    }

    register(firstName, lastName, email, password) {
        return axios.post(API_URL + &#39;register&#39;, {
            firstName,
            lastName,
            email,
            password
        });
    }

    getCurrentUser() {
        return JSON.parse(localStorage.getItem(&#39;user&#39;));
    }
}

export default new AuthService();
&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Now, let&#39;s create a central place for our authenticated Axios instance.&lt;/p&gt;&lt;p&gt;src/services/api.js&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import axios from &quot;axios&quot;;
import AuthService from &quot;./AuthService&quot;;

const api = axios.create({
  baseURL: &quot;http://localhost:8080/api/v1&quot;,
});

api.interceptors.request.use(
  (config) =&amp;gt; {
    const user = AuthService.getCurrentUser();
    if (user &amp;amp;&amp;amp; user.accessToken) {
      config.headers[&quot;Authorization&quot;] = &quot;Bearer &quot; + user.accessToken;
    }
    return config;
  },
  (error) =&amp;gt; {
    return Promise.reject(error);
  }
);

export default api;
&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Now, modify your UserService.js to use this new api instance instead of the base axios.&lt;/p&gt;&lt;p&gt;src/services/UserService.js (Updated)&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import api from &#39;./api&#39;; // Import the configured api instance

class UserService {
    getUsers() {
        return api.get(&#39;/users&#39;);
    }

    // ... other methods (createUser, updateUser, etc.) also use `api`
    createUser(user) {
        return api.post(&#39;/users&#39;, user);
    }
    // ...
}

export default new UserService();
&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Manage Auth State in the App&lt;/h3&gt;&lt;p&gt;In your main App.js, manage the authentication state. Show login/register links if not logged in, and show the user management UI and a logout button if logged in.&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;// In App.js
import React, { useState, useEffect } from &#39;react&#39;;
import AuthService from &#39;./services/AuthService&#39;;
// ... other imports

function App() {
    const [currentUser, setCurrentUser] = useState(undefined);

    useEffect(() =&amp;gt; {
        const user = AuthService.getCurrentUser();
        if (user) {
            setCurrentUser(user);
        }
    }, []);

    const handleLogout = () =&amp;gt; {
        AuthService.logout();
        setCurrentUser(undefined);
    };

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;nav className=&quot;navbar navbar-expand navbar-dark bg-dark&quot;&amp;gt;
                &amp;lt;div className=&quot;navbar-nav mr-auto&quot;&amp;gt;
                    {/* Other nav items */}
                &amp;lt;/div&amp;gt;

                {currentUser ? (
                    &amp;lt;div className=&quot;navbar-nav ml-auto&quot;&amp;gt;
                        &amp;lt;li className=&quot;nav-item&quot;&amp;gt;
                            &amp;lt;a href=&quot;/login&quot; className=&quot;nav-link&quot; onClick={handleLogout}&amp;gt;
                                Logout
                            &amp;lt;/a&amp;gt;
                        &amp;lt;/li&amp;gt;
                    &amp;lt;/div&amp;gt;
                ) : (
                    &amp;lt;div className=&quot;navbar-nav ml-auto&quot;&amp;gt;
                        &amp;lt;li className=&quot;nav-item&quot;&amp;gt;
                            {/* Link to your login component */}
                        &amp;lt;/li&amp;gt;
                    &amp;lt;/div&amp;gt;
                )}
            &amp;lt;/nav&amp;gt;

            &amp;lt;div className=&quot;container mt-3&quot;&amp;gt;
                {/* Use React Router to show components based on auth state and URL */}
                {currentUser ? (
                    // Show the UserManagementComponent
                ) : (
                    // Show Login/Register components
                )}
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}


&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Output:&lt;/h2&gt;&lt;div&gt;Customize the CSS according to your requirements.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Registration page&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi0AyuKP3Tzn15ODSj5gCuLkgrIr--oeykEwAS5RRWCSwUrJLVUi4lyuF02R2ihJ5CZdrACaI7lV161uSDinV2hNt6MElsQKCNBEodG0iEhbhPSesLWUeKVJ382GOZpsZIkMxpN7lAZ7_gQV5KPW67m1-Vn4kO8sSfC2Cy--N1sked2JRTOfrXexWTAWJg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;722&quot; data-original-width=&quot;778&quot; height=&quot;440&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi0AyuKP3Tzn15ODSj5gCuLkgrIr--oeykEwAS5RRWCSwUrJLVUi4lyuF02R2ihJ5CZdrACaI7lV161uSDinV2hNt6MElsQKCNBEodG0iEhbhPSesLWUeKVJ382GOZpsZIkMxpN7lAZ7_gQV5KPW67m1-Vn4kO8sSfC2Cy--N1sked2JRTOfrXexWTAWJg=w626-h440&quot; width=&quot;626&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;Login Page&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgBnL4ByHy1AaZ0vZme0rQgwswj5kqXTWIKBccCtPpu9-bAfBvPur-EKXQ2Fg60gzDFu1jH7N90ci1Ogdgko6WmGrrbNW5XSLVVXa1UcazMwhhU62NephWns1MdqnQLYVwVbs35pQ4gLFVgu3i3s0DwtfB1FlkIbjUZTsMUbOwFqbHdNSwNKocPA9coq0g&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;526&quot; data-original-width=&quot;669&quot; height=&quot;486&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgBnL4ByHy1AaZ0vZme0rQgwswj5kqXTWIKBccCtPpu9-bAfBvPur-EKXQ2Fg60gzDFu1jH7N90ci1Ogdgko6WmGrrbNW5XSLVVXa1UcazMwhhU62NephWns1MdqnQLYVwVbs35pQ4gLFVgu3i3s0DwtfB1FlkIbjUZTsMUbOwFqbHdNSwNKocPA9coq0g=w618-h486&quot; width=&quot;618&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;After Login&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgqg6HZ_WvnzNM8dEFmAYXIHlTDmVOC8GDwhogP2Sl_RSAUN6byVdPQv0mXzHReGNE-qR-tcAMAkhXXI0ynZuY3X5oxL29iGDKDbPiehyNNEZ_-gIfr__nkt4ojCqdS3EJ2SdyQehwdi9bnaExmLTQeBQp6ZsPbbyl6IA2UDDbbUgL1bq_MoNGB8gLZsDk&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;448&quot; data-original-width=&quot;1312&quot; height=&quot;214&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEgqg6HZ_WvnzNM8dEFmAYXIHlTDmVOC8GDwhogP2Sl_RSAUN6byVdPQv0mXzHReGNE-qR-tcAMAkhXXI0ynZuY3X5oxL29iGDKDbPiehyNNEZ_-gIfr__nkt4ojCqdS3EJ2SdyQehwdi9bnaExmLTQeBQp6ZsPbbyl6IA2UDDbbUgL1bq_MoNGB8gLZsDk=w629-h214&quot; width=&quot;629&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;You have successfully transformed a simple CRUD application into a secure, enterprise-ready system. You learned how to:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Configure Spring Security from the ground up.&lt;/li&gt;&lt;li&gt;Implement stateless authentication using JSON Web Tokens (JWT).&lt;/li&gt;&lt;li&gt;Create public and protected API endpoints.&lt;/li&gt;&lt;li&gt;Hash passwords using BCrypt.&lt;/li&gt;&lt;li&gt;Handle login and registration on the React frontend.&lt;/li&gt;&lt;li&gt;Use Axios interceptors to seamlessly send the JWT with every authenticated request.&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/2879875754584011479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2025/10/spring-boot-security-jwt-react-guide.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/2879875754584011479'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/2879875754584011479'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2025/10/spring-boot-security-jwt-react-guide.html' title='Securing APIs with Spring Security and JWT: A Full-Stack Guide for Spring Boot &amp; React'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEi0AyuKP3Tzn15ODSj5gCuLkgrIr--oeykEwAS5RRWCSwUrJLVUi4lyuF02R2ihJ5CZdrACaI7lV161uSDinV2hNt6MElsQKCNBEodG0iEhbhPSesLWUeKVJ382GOZpsZIkMxpN7lAZ7_gQV5KPW67m1-Vn4kO8sSfC2Cy--N1sked2JRTOfrXexWTAWJg=s72-w626-h440-c" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-8219585549130005162</id><published>2025-10-24T19:27:00.003+05:30</published><updated>2025-10-27T17:13:29.623+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Build with me"/><title type='text'>Creating a CRUD Application with Spring Boot and React: A Step-by-Step Guide with MySQL</title><content type='html'>&lt;p&gt;In modern web development, the combination of a powerful backend framework like Spring Boot and a flexible frontend library like &lt;b&gt;React &lt;/b&gt;is a popular choice for building robust, scalable applications. When you add &lt;b&gt;MySQL&lt;/b&gt;, one of the world&#39;s most popular open-source databases, you have a complete, enterprise-ready technology stack.&lt;/p&gt;&lt;p&gt;This article will walk through the entire process of building a full-stack&amp;nbsp; &lt;b&gt;CRUD&lt;/b&gt; (Create, Read, Update, Delete) application. We&#39;ll create a Spring Boot backend to manage a list of users by creating a REST API, and connect with a React frontend.&lt;/p&gt;&lt;p&gt;By the end of this tutorial, you will have a practical understanding of the&amp;nbsp;&lt;b&gt;REST api&lt;/b&gt; in&lt;b&gt; Spring Boot&lt;/b&gt; and how to use the api in our frontend application.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Prerequisites&lt;/h2&gt;&lt;p&gt;Before we start, make sure you have the following installed:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Java Development Kit (JDK).&lt;/li&gt;&lt;li&gt;Maven or Gradle for dependency management. We&#39;ll use Maven in this guide.&lt;/li&gt;&lt;li&gt;An IDE like IntelliJ IDEA or Eclipse.&lt;/li&gt;&lt;li&gt;Node.js and npm for managing the React project.&lt;/li&gt;&lt;li&gt;MySQL Server and a client like MySQL Workbench.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Let&#39;s first set up our backend application using Spring Boot.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 1: Building the Backend with Spring Boot&lt;/h2&gt;&lt;p&gt;First, we&#39;ll create the Spring Boot application that we will use to create a REST API.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Step 1:&lt;/b&gt; Initialize the Spring Boot Project&lt;/h3&gt;&lt;p&gt;The easiest way to start is with the &lt;a href=&quot;https://start.spring.io/index.html&quot; target=&quot;_blank&quot;&gt;Spring Initializr&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Go to start.spring.io.&lt;/li&gt;&lt;li&gt;Configure your project:&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Project: Maven&lt;/li&gt;&lt;li&gt;Language: Java&lt;/li&gt;&lt;li&gt;Spring Boot: The latest stable version.&lt;/li&gt;&lt;li&gt;Group: com.example&lt;/li&gt;&lt;li&gt;Artifact: crud-backend&lt;/li&gt;&lt;li&gt;Packaging: Jar&lt;/li&gt;&lt;li&gt;Java: choose the latest one&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;Add the following dependencies:&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Spring Web: For building RESTful web applications.&lt;/li&gt;&lt;li&gt;Spring Data JPA: To simplify database interactions using &lt;b&gt;ORM&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;MySQL Driver: The JDBC driver that helps to connect to MySQL.&lt;/li&gt;&lt;li&gt;Lombok: Reduces boilerplate code like getters, setters, and constructors.&lt;/li&gt;&lt;li&gt;DevTools: For development purposes, to not restart the application after changes to the codebase.&lt;/li&gt;&lt;/ul&gt;&lt;/ol&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Click &quot;Generate&quot; to download the project zip file. Unzip it and open it in your IDE.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Configure the MySQL Database Connection&lt;/h3&gt;&lt;p&gt;Open the &lt;b&gt;src/main/resources/application.properties &lt;/b&gt;file and add your MySQL database configuration. Create a database named user_management_db in your MySQL server beforehand.&lt;/p&gt;&lt;p&gt;&lt;b&gt;application.properties&lt;/b&gt;&lt;/p&gt;&lt;p&gt;
&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;# MySQL Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/user
spring.datasource.username=root
spring.datasource.password=root

# JPA/Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
&lt;/pre&gt;



&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;spring.datasource.url:&lt;/b&gt;&amp;nbsp;This tells the database url, and here user is a database name.&lt;/p&gt;&lt;p&gt;&lt;b&gt;spring.datasource.username:&lt;/b&gt;&amp;nbsp;username for accessing the database.&lt;/p&gt;&lt;p&gt;&lt;b&gt;spring.datasource.password:&lt;/b&gt;&amp;nbsp;passwords for accessing the database.&lt;/p&gt;&lt;p&gt;&lt;b&gt;spring.jpa.hibernate.ddl-auto=update:&lt;/b&gt; This tells Hibernate to automatically update the database schema based on your entities. For production, you should use a migration tool like Flyway&amp;nbsp;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Create the Model (Entity)&lt;/h3&gt;&lt;p&gt;Now, let&#39;s define the data model. Create a User class that creates a table of users in the database with the help of Hibernate.&lt;/p&gt;&lt;p&gt;&lt;b&gt;src/main/java/com/example/crudbackend/model/User.java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.model;

import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = &quot;users&quot;)
@Data // Lombok annotation for getters, setters, toString, etc.
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;
    private String lastName;
    private String email;
}
&lt;/pre&gt;



&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;@Entity:&lt;/b&gt; Marks this class as a JPA entity.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@Table(name = &quot;users&quot;): &lt;/b&gt;Specifies the table name in the database.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@Id:&lt;/b&gt; Designates the id field as the primary key.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@GeneratedValue:&lt;/b&gt; Configures the primary key generation strategy. IDENTITY is a good choice for MySQL.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@Data:&lt;/b&gt; A Lombok annotation that generates all the boilerplate for us.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 4: Build the Repository Interface&lt;/h3&gt;&lt;p&gt;The repository is the layer that interacts with the database. &lt;b&gt;Spring Data JPA&lt;/b&gt; helps to interact with the database without writing the SQL query.&lt;/p&gt;&lt;p&gt;Create a UserRepository interface that extends JpaRepository.&lt;/p&gt;&lt;p&gt;&lt;b&gt;src/main/java/com/example/crudbackend/repository/UserRepository.java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;



&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.repository;

import com.example.crudbackend.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository&lt;user long=&quot;&quot;&gt; {
}
&lt;/user&gt;&lt;/pre&gt;




&lt;p&gt;&lt;/p&gt;&lt;p&gt;JpaRepository&amp;lt;User, Long&amp;gt; gives us all the standard CRUD methods that give default such as findAll, findById, save, and deleteById methods to use.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 5: Develop the REST Controller&lt;/h3&gt;&lt;p&gt;The controller exposes our CRUD operations as RESTful API endpoints.&lt;/p&gt;&lt;p&gt;Create a UserController class.&lt;/p&gt;&lt;p&gt;&lt;b&gt;src/main/java/com/example/crudbackend/controller/UserController.java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Java&lt;/b&gt;&lt;/p&gt;&lt;p&gt;




&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;package com.example.crudbackend.controller;

import com.example.crudbackend.model.User;
import com.example.crudbackend.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = &quot;http://localhost:3000&quot;) // Allow requests from React app
@RestController
@RequestMapping(&quot;/api/v1/users&quot;)
public class UserController {

    @Autowired
    private UserRepository userRepository;

    // Get all users
    @GetMapping
    public List&lt;user&gt; getAllUsers() {
        return userRepository.findAll();
    }

    // Create a new user
    @PostMapping
    public User createUser(@RequestBody User user) {
        return userRepository.save(user);
    }

    // Get a single user by id
    @GetMapping(&quot;/{id}&quot;)
    public ResponseEntity&lt;user&gt; getUserById(@PathVariable Long id) {
        User user = userRepository.findById(id)
                .orElseThrow(() -&amp;gt; new RuntimeException(&quot;User not found with id: &quot; + id));
        return ResponseEntity.ok(user);
    }

    // Update a user
    @PutMapping(&quot;/{id}&quot;)
    public ResponseEntity&lt;user&gt; updateUser(@PathVariable Long id, @RequestBody User userDetails) {
        User user = userRepository.findById(id)
                .orElseThrow(() -&amp;gt; new RuntimeException(&quot;User not found with id: &quot; + id));

        user.setFirstName(userDetails.getFirstName());
        user.setLastName(userDetails.getLastName());
        user.setEmail(userDetails.getEmail());

        User updatedUser = userRepository.save(user);
        return ResponseEntity.ok(updatedUser);
    }

    // Delete a user
    @DeleteMapping(&quot;/{id}&quot;)
    public ResponseEntity&lt;void&gt; deleteUser(@PathVariable Long id) {
        User user = userRepository.findById(id)
                .orElseThrow(() -&amp;gt; new RuntimeException(&quot;User not found with id: &quot; + id));

        userRepository.delete(user);
        return ResponseEntity.noContent().build();
    }
}
&lt;/void&gt;&lt;/user&gt;&lt;/user&gt;&lt;/user&gt;&lt;/pre&gt;



&lt;p&gt;&lt;/p&gt;&lt;p&gt;Key Annotations:&lt;/p&gt;&lt;p&gt;&lt;b&gt;@RestController:&lt;/b&gt; Combines @Controller and @ResponseBody, marking this class for RESTful requests.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@RequestMapping(&quot;/api/v1/users&quot;):&lt;/b&gt; Sets the base path for all endpoints in this controller.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@CrossOrigin(origins = &quot;http://localhost:3000&quot;):&lt;/b&gt; This is crucial! It allows our React application (running on port 3000) to make API calls to this backend (running on port 8080), avoiding Cross-Origin Resource Sharing (CORS) errors.&lt;/p&gt;&lt;p&gt;&lt;b&gt;@GetMapping, @PostMapping, @PutMapping, @DeleteMapping:&lt;/b&gt; Map HTTP methods to specific controller actions.&lt;/p&gt;&lt;p&gt;Our backend is now ready! You can run it from your IDE or by using the command mvn spring-boot:run.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Part 2: Building the Frontend with React&lt;/h2&gt;&lt;p&gt;Now, let&#39;s create the frontend where the user can interact with our API.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 1: Initialize the React Project&lt;/h3&gt;&lt;p&gt;Open a new terminal window and run the following command to create a new React app:&lt;/p&gt;&lt;p&gt;&lt;b&gt;Bash&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;npx create-react-app crud-frontend
cd crud-frontend
&lt;/pre&gt;


&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Install Dependencies&lt;/h3&gt;&lt;p&gt;We need Axios to make HTTP requests to our Spring Boot backend and bootstrap for some basic styling.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Bash&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;npm install axios bootstrap
&lt;/pre&gt;



&lt;p&gt;&lt;/p&gt;&lt;p&gt;Import Bootstrap&#39;s CSS into your src/index.js file:&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import &#39;bootstrap/dist/css/bootstrap.min.css&#39;;
&lt;/pre&gt;


&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Create a Service for API Calls&lt;/h3&gt;&lt;p&gt;It&#39;s good practice to centralize API logic because whenever we move to the product in that time domain of the backend application or url of the backend can be changed, this centralized file helps by only changing url at the one file not everywhere. Create a new file &lt;b&gt;src/services/UserService.js&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;src/services/UserService.js&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;import axios from &#39;axios&#39;;

const API_BASE_URL = &quot;http://localhost:8080/api/v1/users&quot;;

class UserService {

    getUsers() {
        return axios.get(API_BASE_URL);
    }

    createUser(user) {
        return axios.post(API_BASE_URL, user);
    }

    getUserById(userId) {
        return axios.get(API_BASE_URL + &#39;/&#39; + userId);
    }

    updateUser(user, userId) {
        return axios.put(API_BASE_URL + &#39;/&#39; + userId, user);
    }

    deleteUser(userId) {
        return axios.delete(API_BASE_URL + &#39;/&#39; + userId);
    }
}

export default new UserService();
&lt;/pre&gt;


&lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 4: Build the Main User Component&lt;/h3&gt;&lt;p&gt;Let&#39;s create the component that will display the list of users and a form for creating/updating them. Replace the content of src/App.js with the following:&lt;/p&gt;&lt;p&gt;&lt;b&gt;src/App.js&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;JavaScript&lt;/b&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; border-radius: 4px; border: 1px solid rgb(221, 221, 221); color: #444444; font-family: monospace; font-weight: 400; margin: 10px; overflow-wrap: break-word; padding: 10px; white-space: pre-wrap; word-wrap: break-word;&quot;&gt;import React, { useState, useEffect } from &#39;react&#39;;
import UserService from &#39;./services/UserService&#39;;
import &#39;./App.css&#39;;

function App() {
    const [users, setUsers] = useState([]);
    const [currentUser, setCurrentUser] = useState({ id: null, firstName: &#39;&#39;, lastName: &#39;&#39;, email: &#39;&#39; });
    const [isEditing, setIsEditing] = useState(false);

    useEffect(() =&amp;gt; {
        loadUsers();
    }, []);

    const loadUsers = () =&amp;gt; {
        UserService.getUsers().then(response =&amp;gt; {
            setUsers(response.data);
        });
    };

    const handleInputChange = (event) =&amp;gt; {
        const { name, value } = event.target;
        setCurrentUser({ ...currentUser, [name]: value });
    };

    const handleSubmit = (event) =&amp;gt; {
        event.preventDefault();
        if (isEditing) {
            UserService.updateUser(currentUser, currentUser.id).then(() =&amp;gt; {
                loadUsers();
            });
        } else {
            UserService.createUser(currentUser).then(() =&amp;gt; {
                loadUsers();
            });
        }
        resetForm();
    };

    const handleEdit = (user) =&amp;gt; {
        setIsEditing(true);
        setCurrentUser(user);
    };

    const handleDelete = (id) =&amp;gt; {
        UserService.deleteUser(id).then(() =&amp;gt; {
            loadUsers();
        });
    };

    const resetForm = () =&amp;gt; {
        setIsEditing(false);
        setCurrentUser({ id: null, firstName: &#39;&#39;, lastName: &#39;&#39;, email: &#39;&#39; });
    };

    return (
        &amp;lt;div className=&quot;container mt-4&quot;&amp;gt;
            &amp;lt;h1 className=&quot;text-center&quot;&amp;gt;User Management System&amp;lt;/h1&amp;gt;
            
            &amp;lt;div className=&quot;row&quot;&amp;gt;
                &amp;lt;div className=&quot;col-md-8&quot;&amp;gt;
                    &amp;lt;h2&amp;gt;User List&amp;lt;/h2&amp;gt;
                    &amp;lt;table className=&quot;table table-bordered&quot;&amp;gt;
                        &amp;lt;thead className=&quot;thead-dark&quot;&amp;gt;
                            &amp;lt;tr&amp;gt;
                                &amp;lt;th&amp;gt;First Name&amp;lt;/th&amp;gt;
                                &amp;lt;th&amp;gt;Last Name&amp;lt;/th&amp;gt;
                                &amp;lt;th&amp;gt;Email&amp;lt;/th&amp;gt;
                                &amp;lt;th&amp;gt;Actions&amp;lt;/th&amp;gt;
                            &amp;lt;/tr&amp;gt;
                        &amp;lt;/thead&amp;gt;
                        &amp;lt;tbody&amp;gt;
                            {users.map(user =&amp;gt; (
                                &amp;lt;tr key={user.id}&amp;gt;
                                    &amp;lt;td&amp;gt;{user.firstName}&amp;lt;/td&amp;gt;
                                    &amp;lt;td&amp;gt;{user.lastName}&amp;lt;/td&amp;gt;
                                    &amp;lt;td&amp;gt;{user.email}&amp;lt;/td&amp;gt;
                                    &amp;lt;td&amp;gt;
                                        &amp;lt;button className=&quot;btn btn-warning btn-sm me-2&quot; onClick={() =&amp;gt; handleEdit(user)}&amp;gt;Edit&amp;lt;/button&amp;gt;
                                        &amp;lt;button className=&quot;btn btn-danger btn-sm&quot; onClick={() =&amp;gt; handleDelete(user.id)}&amp;gt;Delete&amp;lt;/button&amp;gt;
                                    &amp;lt;/td&amp;gt;
                                &amp;lt;/tr&amp;gt;
                            ))}
                        &amp;lt;/tbody&amp;gt;
                    &amp;lt;/table&amp;gt;
                &amp;lt;/div&amp;gt;

                &amp;lt;div className=&quot;col-md-4&quot;&amp;gt;
                    &amp;lt;h2&amp;gt;{isEditing ? &#39;Edit User&#39; : &#39;Add User&#39;}&amp;lt;/h2&amp;gt;
                    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
                        &amp;lt;div className=&quot;form-group&quot;&amp;gt;
                            &amp;lt;label&amp;gt;First Name&amp;lt;/label&amp;gt;
                            &amp;lt;input type=&quot;text&quot; name=&quot;firstName&quot; className=&quot;form-control&quot; value={currentUser.firstName} onChange={handleInputChange} required /&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;div className=&quot;form-group&quot;&amp;gt;
                            &amp;lt;label&amp;gt;Last Name&amp;lt;/label&amp;gt;
                            &amp;lt;input type=&quot;text&quot; name=&quot;lastName&quot; className=&quot;form-control&quot; value={currentUser.lastName} onChange={handleInputChange} required /&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;div className=&quot;form-group&quot;&amp;gt;
                            &amp;lt;label&amp;gt;Email&amp;lt;/label&amp;gt;
                            &amp;lt;input type=&quot;email&quot; name=&quot;email&quot; className=&quot;form-control&quot; value={currentUser.email} onChange={handleInputChange} required /&amp;gt;
                        &amp;lt;/div&amp;gt;
                        &amp;lt;button type=&quot;submit&quot; className=&quot;btn btn-primary mt-3&quot;&amp;gt;{isEditing ? &#39;Update&#39; : &#39;Create&#39;}&amp;lt;/button&amp;gt;
                        {isEditing &amp;amp;&amp;amp; &amp;lt;button type=&quot;button&quot; className=&quot;btn btn-secondary mt-3 ms-2&quot; onClick={resetForm}&amp;gt;Cancel&amp;lt;/button&amp;gt;}
                    &amp;lt;/form&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}

export default App;
&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;&lt;p&gt;Component Breakdown:&lt;/p&gt;&lt;p&gt;&lt;b&gt;useState:&lt;/b&gt; We use state to manage users (the list from the API), currentUser (the data in the form), and isEditing (to toggle between create and update mode).&lt;/p&gt;&lt;p&gt;&lt;b&gt;useEffect:&lt;/b&gt; The useEffect hook with an empty dependency array [] runs once when the component mounts, calling loadUsers() to fetch the initial data.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Event Handlers:&lt;/b&gt; Functions like handleSubmit, handleEdit, and handleDelete use our UserService to communicate with the backend and then refresh the user list.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Part 3: Running the Full-Stack Application&lt;/h3&gt;&lt;p&gt;Run the Backend: Make sure your Spring Boot application is running.&lt;/p&gt;&lt;p&gt;Run the Frontend: In your crud-frontend directory, run the command:&lt;/p&gt;&lt;p&gt;&lt;b&gt;Bash&lt;/b&gt;&lt;/p&gt;&lt;p&gt;


&lt;/p&gt;&lt;pre style=&quot;background-color: #f3f3f3; color: #444444; font-family: monospace; font-weight: 400; margin: 10px; padding: 10px;&quot;&gt;npm start
&lt;/pre&gt;


&lt;p&gt;&lt;/p&gt;&lt;p&gt;This will open your React application in a browser, usually at http://localhost:3000.&lt;/p&gt;&lt;p&gt;You should now see the User Management System. You can add, view, edit, and delete users, with all changes being persisted in your MySQL database!&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Output:&lt;/h3&gt;&lt;div&gt;You can customize the frontend design by changing the CSS file according to your taste.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;create user&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEhOPDgAGVHnTqBKf_J8OkXywhZprXOkfmYltjpOOfnI5kUZkKHLbX66et5zuZ0eHtByeDla10HocaFGoOmYDbNGgBaI9lVBAQRyU7tR2LePunKzo3Zx66kg6Tt7tt67VDl5rhHFH1ek02M85STZn5rxXvCfRAeiRwBgXPLytO2mhAHMRXgcABkLjWmfPIs&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;629&quot; data-original-width=&quot;889&quot; height=&quot;429&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEhOPDgAGVHnTqBKf_J8OkXywhZprXOkfmYltjpOOfnI5kUZkKHLbX66et5zuZ0eHtByeDla10HocaFGoOmYDbNGgBaI9lVBAQRyU7tR2LePunKzo3Zx66kg6Tt7tt67VDl5rhHFH1ek02M85STZn5rxXvCfRAeiRwBgXPLytO2mhAHMRXgcABkLjWmfPIs=w608-h429&quot; width=&quot;608&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;View Users&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEibwAdnymS7A5IjjzXGF3OLSDjljXkp-4FPvWoTZJdXXF63h4IWyZ9U6wMQpPMSeUNNDVvEMrvX2ZI3DDt5Dj291K47eT3-oNdDttMZdq0ew1iqp6Q7N_aIc3AKh7_gqS7Iu2nKQOfKX36wWHamg-P64Wixrf5PQzr8DAroBdhhmpTBSN_fi4Kqp_WbTGA&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;388&quot; data-original-width=&quot;1289&quot; height=&quot;184&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEibwAdnymS7A5IjjzXGF3OLSDjljXkp-4FPvWoTZJdXXF63h4IWyZ9U6wMQpPMSeUNNDVvEMrvX2ZI3DDt5Dj291K47eT3-oNdDttMZdq0ew1iqp6Q7N_aIc3AKh7_gqS7Iu2nKQOfKX36wWHamg-P64Wixrf5PQzr8DAroBdhhmpTBSN_fi4Kqp_WbTGA=w613-h184&quot; width=&quot;613&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;Edit User&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjJckMSHj1e_zQplkCf2s2vX2AaVeiLydlHT1pRD3CAoTDCReBERjhKyvlR9pOksr_LBlw9EITTtegwg0j8cBVNoWPpjGFCVhWqMIpxTqZ8TotyvnIOppObdpIMx7djAWlx_92Do4h-eUKOA7D5nm6fqZVyT4fZjC2nZ3bMv0GrwlTCvryId2PnNGJBI5s&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;638&quot; data-original-width=&quot;1303&quot; height=&quot;303&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEjJckMSHj1e_zQplkCf2s2vX2AaVeiLydlHT1pRD3CAoTDCReBERjhKyvlR9pOksr_LBlw9EITTtegwg0j8cBVNoWPpjGFCVhWqMIpxTqZ8TotyvnIOppObdpIMx7djAWlx_92Do4h-eUKOA7D5nm6fqZVyT4fZjC2nZ3bMv0GrwlTCvryId2PnNGJBI5s=w618-h303&quot; width=&quot;618&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Finally, you have successfully built a full-stack CRUD application with Spring Boot, React, and MySQL. You&#39;ve learned how to create a robust REST API, connect it to a database using Spring Data JPA, and build a dynamic, interactive frontend with React Hooks&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;This powerful tech stack provides a clear and efficient path for developing modern web applications. Happy coding&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/8219585549130005162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2025/10/crud-spring-boot-react-mysql-guide.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/8219585549130005162'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/8219585549130005162'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2025/10/crud-spring-boot-react-mysql-guide.html' title='Creating a CRUD Application with Spring Boot and React: A Step-by-Step Guide with MySQL'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/a/AVvXsEhOPDgAGVHnTqBKf_J8OkXywhZprXOkfmYltjpOOfnI5kUZkKHLbX66et5zuZ0eHtByeDla10HocaFGoOmYDbNGgBaI9lVBAQRyU7tR2LePunKzo3Zx66kg6Tt7tt67VDl5rhHFH1ek02M85STZn5rxXvCfRAeiRwBgXPLytO2mhAHMRXgcABkLjWmfPIs=s72-w608-h429-c" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-3532312275172049119</id><published>2023-07-21T17:30:00.007+05:30</published><updated>2025-11-19T11:55:01.139+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java Program to Find the Area of the Cricle </title><content type='html'>&lt;p&gt;In this article, we will learn to create a program that calculates the area of a circle using the Java programming language.&lt;span style=&quot;font-size: 11pt; white-space-collapse: preserve;&quot;&gt;            &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/span&gt;Area of Circle&lt;/h2&gt;&lt;p&gt;&lt;b&gt;Area&lt;/b&gt; = pi*r*r&lt;/p&gt;&lt;p&gt;where, pi = 3.14&lt;/p&gt;&lt;p&gt;r is the radius of the circle.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h2&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we will create a function name area().&lt;/li&gt;&lt;li&gt;area() takes the argument as a radius and returns the calculated area of the circle&lt;/li&gt;&lt;li&gt;In the main() function, we take input of radius from the user and call the area() function.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Solution&lt;/h2&gt;&lt;p&gt;&lt;b&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/b&gt;&lt;/p&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;import&lt;/span&gt; java.util.*;

&lt;span style=&quot;color: blue;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;ar_cirlce&lt;/span&gt; {
    &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;double&lt;/span&gt; area(&lt;span style=&quot;color: #2b91af;&quot;&gt;int&lt;/span&gt; r){
        &lt;span style=&quot;color: #2b91af;&quot;&gt;double&lt;/span&gt; area;

        area = 3.14*r*r;

        &lt;span style=&quot;color: blue;&quot;&gt;return&lt;/span&gt; area;
    }

    &lt;span style=&quot;color: blue;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;void&lt;/span&gt; main(String args[]){
        &lt;span style=&quot;color: #2b91af;&quot;&gt;int&lt;/span&gt; radius;

        Scanner sc = &lt;span style=&quot;color: blue;&quot;&gt;new&lt;/span&gt; Scanner(System.in);

        radius = sc.nextInt();
        
        System.out.println(area(radius));
    }
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;b&gt;
&lt;/b&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Output&lt;/h2&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;5
78.5&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/3532312275172049119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/07/java-program-to-find-area-of-cricle.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3532312275172049119'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3532312275172049119'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/07/java-program-to-find-area-of-cricle.html' title='Java Program to Find the Area of the Cricle '/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-32982203616231024</id><published>2023-02-17T16:22:00.005+05:30</published><updated>2024-03-31T10:58:19.874+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>How to Solve Indexerror index 0 is out of bounds for axis 0 with size 0</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;The cause for&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #843fa1;&quot;&gt;Indexerror: index 0 is out of bounds for axis 0 with size 0&lt;/span&gt;&amp;nbsp;is when trying to access the first element stored inside the first dimension array. However, such a numpy array is empty and hence inaccessible. This tutorial shows an example of how the error arise and later, the ways of handling such an error such as first checking if the numpy array is empty or not before access, or using try/except.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;h4 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&amp;nbsp;Table of Contents:&lt;/span&gt;&lt;/h4&gt;&lt;ol style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286785&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Reasons that will cause this error.Example of the Error.&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286786&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution&lt;/span&gt;&lt;/a&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286788&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution One: Checking if the array is empty or not before accessing.&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286789&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution Two: Change the shape of the array.&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286790&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution Three: Use Try/Except.&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286791&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1146/indexerror-index-0-is-out-of-bounds-for-axis-0-with-size-0#_Toc125286792&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Reference&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Example of the Error&lt;/span&gt;&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Let&#39;s take an example of a numpy array that we want to access the first element from.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-javascript&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; np
exArray=np.&lt;span class=&quot;hljs-title function_&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;array&lt;/span&gt;([])
element1=exArray[&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;When we run the above code, we get the following output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;line &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;4&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;in&lt;/span&gt; 
element1=exArray[&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;]
IndexError: index &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;out&lt;/span&gt; of bounds &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;for&lt;/span&gt; axis &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;with&lt;/span&gt; size &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;As we see, the error &quot;IndexError: index 0 is out of bounds for axis 0 with size 0&quot; occurs when trying to access the first element stored inside the first dimension array. However, such a numpy array is empty and hence inaccessible.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-purple&quot; style=&quot;background-color: #efd9fd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Note&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&amp;nbsp;The numpy array &quot;exArray&quot; was created with an empty list, which resulted in the error. It is crucial to check the initialization of the array and ensure that it is not empty before attempting to access its elements.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Solution to the Error&lt;/span&gt;&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;There are multiple ways to solve this error, let&#39;s take a look at some of the most effective solutions.&lt;/span&gt;&lt;/p&gt;&lt;h3 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin: 0px 0px 10px;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Solution One: Checking if the array is empty or not before accessing&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;One of the most straightforward solutions is to check the size of the array before trying to access it. If the size of the array is 0, then the array is empty and we should not try to access it.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-php&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import numpy &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; np
exArray=np.&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;array&lt;/span&gt;([])
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;if&lt;/span&gt; exArray.size&amp;gt;&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;:
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;print&lt;/span&gt;(exArray[&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;])
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;else&lt;/span&gt;:
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;The size of the array is 0&#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The output would be:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The size of the array &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;As we see, we are able to check if the size of the array is 0 or not before trying to access it. In this way, we can avoid the error from occurring.&lt;/span&gt;&lt;/p&gt;&lt;h3 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin: 0px 0px 10px;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Solution Two: Change the shape of the array&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Another solution is to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;change the shape of the array&amp;nbsp;&lt;/a&gt;&lt;/strong&gt;. By giving the array a shape, we are able to access it without any error.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-python&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; np
exArray=np.zeros((&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;4&lt;/span&gt;),dtype=&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt;)
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(exArray.shape)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The output would be:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-scss&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;As we see, by giving the array a shape, we are able to access it without any error.&lt;/span&gt;&lt;/p&gt;&lt;h3 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin: 0px 0px 10px;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Solution Three: Use Try/Except&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://docs.python.org/3/tutorial/errors.html#handling-exceptions&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;try/except block to catch the error&amp;nbsp;&lt;/a&gt;&lt;/strong&gt;is another effective solution. In this way, if the error occurs, we can handle it by providing a message or any other action that we want to take.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-python&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; np
exArray=np.array([])
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;try&lt;/span&gt;:
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(exArray[&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;])
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;except&lt;/span&gt; IndexError:
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;The index 0 is empty&#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The output would be:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The index &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;is&lt;/span&gt; empty
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;As we see, by using the try/except block, we are able to catch the error and handle it.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-green&quot; style=&quot;background-color: #dff6dd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Tip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;A good practice to avoid this error is to always initialize the array with a default size or shape, and check the array&#39;s size before trying to access its elements. This way, you can ensure that the array is not empty and avoid the IndexError from occurring. Additionally, it is always a good idea to use try/except blocks to catch any unexpected errors, and handle them accordingly.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Conclusion&lt;/strong&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Hopefully you have now learned more concerning the I&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; color: #843fa1;&quot;&gt;ndexerror: index 0 is out of bounds for axis 0 with size 0&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;error that occurs when trying to access an element from an empty numpy array. We have also learned about some effective solutions such as checking if the array is empty before accessing it, changing the shape of the array, and using a try/except block to catch the error. Remember, it is important to check if the array is empty before trying to access any element from it to avoid the error.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/32982203616231024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-indexerror-index-0-is-out.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/32982203616231024'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/32982203616231024'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-indexerror-index-0-is-out.html' title='How to Solve Indexerror index 0 is out of bounds for axis 0 with size 0'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-6017525045435767708</id><published>2023-02-17T16:21:00.004+05:30</published><updated>2024-03-31T10:58:23.327+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>How to Solve Error Conflicting types for function in c</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;In this article, we will the error message &quot;&lt;span style=&quot;box-sizing: border-box; color: #ff04fc;&quot;&gt;Conflicting types for function c&lt;/span&gt;&quot;. This message indicates that there is a&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #ff04fc;&quot;&gt;mismatch between the return type of a function&lt;/span&gt;, which in this case is&amp;nbsp; named &quot;c&quot;, and its previous declaration. This can&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #ff04fc;&quot;&gt;happen if a function is declared with one return type in one location (e.g. int), but then defined with a different return type in another location (e.g. void&lt;/span&gt;&amp;nbsp;or float). To fix this error, ensure that the return type of the function is consistent across all of its declarations and definitions.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;div id=&quot;toc-div&quot; style=&quot;background: rgb(249, 249, 249); border-bottom: 1px solid rgb(217, 217, 217); box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; padding: 10px;&quot;&gt;&lt;h4 style=&quot;box-sizing: border-box; margin: 15px 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Table of Contents:&amp;nbsp;&lt;a class=&quot;open&quot; id=&quot;toc-toogle&quot; style=&quot;box-sizing: border-box; color: #039be5; margin-left: 30px; margin-right: 30px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div id=&quot;toc-list&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#what&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Defining the Error &quot;Conflicting types for function c&quot;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#why&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Why does the Error Occur?&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#sol&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;How to Solve the Error&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#con&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#con&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;The References&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Defining the Error &quot;Conflicting types for function c&quot;&lt;/strong&gt;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&amp;nbsp;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#what&quot; id=&quot;TypeError-str-object&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The word &quot;conflicting&quot; is used to describe two or more things that cannot all be right or cannot all happen at the same time.&amp;nbsp;The&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&quot;Conflicting types for function c&quot; error is a common issue&lt;/strong&gt;&amp;nbsp;that can occur when writing C or C++ code.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;When a function is declared, it tells the compiler what the function&#39;s name is, what parameters it takes, and what its return type is. The&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;compiler uses this information to ensure that the function is called correctly in other parts of the code&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The error&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;occurs when the return type of a function named &quot;c&quot; is different between its declaration and definition&lt;/strong&gt;. In other words, the function is declared with one return type in one location, but then defined with a different return type in another location.&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Why does this Error Occur?&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#why&quot; id=&quot;reasons-that-will-cause&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The &quot;Conflicting types for function c&quot; error is caused by a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;difference between the return type of a function in its declaration and definition&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In C and C++, a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;function is typically declared in a header file or above the&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;int main()&lt;/code&gt;&amp;nbsp;function in the same source file&lt;/strong&gt;&amp;nbsp;which provides the compiler with information about the function&#39;s name, parameters, and return type. The&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;function&#39;s definition, which includes the actual code for the function, is typically located in a separate source file or in the same source file below the same&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;int main()&lt;/code&gt;&amp;nbsp;function&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;When the compiler encounters the function&#39;s definition, it&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;compares the return type of the function in the definition to the return type of the function in the declaration&lt;/strong&gt;.&amp;nbsp;If the two do not match, the compiler generates the &quot;Conflicting types for function c&quot; error.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here is an example which will generate same error.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-cpp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-meta&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;#&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;include&lt;/span&gt; &amp;nbsp;&lt;/span&gt;
&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; b)&lt;/span&gt;&lt;/span&gt;;              &lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;// Declaration of function c with return type int&lt;/span&gt;

&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;{
        &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;c&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;12&lt;/span&gt;);
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;;
}&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; b)&lt;/span&gt; &lt;/span&gt;{          &lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;// Definition of function c with return type void          printf(&quot;a:  %d&quot;, a);}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In this example, the function &quot;c&quot; is declared with a return type of &quot;int&quot; in the source file above&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;int main()&lt;/code&gt;&amp;nbsp;function, but defined with a return type of &quot;void&quot; in the same source file below&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;int main()&lt;/code&gt;&amp;nbsp;function. This causes a clash between the declaration and definition of the function, and the compiler will generate the error &quot;Conflicting types for function c&quot;.&lt;/span&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The error will look something like below.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-css&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;D:\c-program\main.c|&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;13&lt;/span&gt;|error: conflicting types for &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;c&#39;&lt;/span&gt;|
D:\c-program\main.c|&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;4&lt;/span&gt;|note: previous declaration of &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;c&#39;&lt;/span&gt; was here|
||=== Build failed: &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;error&lt;/span&gt;(s), &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;warning&lt;/span&gt;(s) (&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;minute&lt;/span&gt;(s), &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;second&lt;/span&gt;(s)) ===|&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;&lt;/span&gt;&lt;div class=&quot;div-pink&quot; style=&quot;background-color: #fef2f2; box-sizing: border-box; color: #b91c1c; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Caution&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Be mindful that this&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;error can occur when a function is defined in a source file but the header file that contains the function&#39;s declaration is not included&lt;/strong&gt;. If the compiler cannot find the function&#39;s declaration, it will not know the correct return type and will generate the &quot;Conflicting types for function c&quot; error.&lt;/span&gt;&lt;/div&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;How to Solve the Error?&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1177/solved-error-conflicting-types-for-function-c-c-language#sol&quot; id=&quot;the-solution&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&quot;Conflicting types for function c&quot; error is typically generated when there is a difference between the decalaration and definition of function. In order&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;to avoid this error make sure that the return type of the function in the declaration and definition match.&lt;/strong&gt;&amp;nbsp;If the error is caused by a distinction between the return type of the function in its declaration and definition, the solution is to ensure that the return types match. In the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;example provided earlier, we change the definition and declaration of the function &quot;c&quot; to have either a return type of&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;int&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;void&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;as done below. This will solve the issue.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-cpp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-meta&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;#&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;include&lt;/span&gt; &amp;nbsp;&lt;/span&gt;
&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; b)&lt;/span&gt;&lt;/span&gt;;              &lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;// Declaration of function c with return type int&lt;/span&gt;

&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;{
        &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;c&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;12&lt;/span&gt;);
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;;
}&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; a, &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; b)&lt;/span&gt; &lt;/span&gt;{          &lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;// Definition of function c with return type void          printf(&quot;a:  %d&quot;, a);}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;&lt;/span&gt;&lt;div class=&quot;div-pink&quot; style=&quot;background-color: #fef2f2; box-sizing: border-box; color: #b91c1c; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Caution&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Ensure that a function is not being defined twice in the same file with different return types. This will also cause the error &quot;Conflicting types for function c&quot;.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;&lt;/span&gt;&lt;div class=&quot;div-green&quot; style=&quot;background-color: #dff6dd; box-sizing: border-box; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Tip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Ensure that there are no spelling mistakes when decalaring and defining a function.&lt;/span&gt;&lt;/div&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;con&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;The Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;In conclusion, the &quot;Conflicting types for function c&quot; error is&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;a common error that can occur in C and C++ programming&lt;/strong&gt;&amp;nbsp;when there is a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;mismatch between the return type of a function in its declaration and definition&lt;/strong&gt;. To solve this error, you need to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;ensure that the return type of the function in the declaration and definition match&lt;/strong&gt;. Understanding the cause of this error and knowing how to fix it can help you write more efficient and error-free code in C and C++ programming.&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/6017525045435767708/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-error-conflicting-types.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/6017525045435767708'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/6017525045435767708'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-error-conflicting-types.html' title='How to Solve Error Conflicting types for function in c'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-7640289615894352922</id><published>2023-02-17T16:18:00.004+05:30</published><updated>2024-03-31T10:58:24.133+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>How to Solve Valueerror: object arrays cannot be loaded when allow_pickle=false</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;This error message is indicating that you are trying to load an object array using the&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;function with the&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;parameter set to&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;False&lt;/code&gt;&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;. The allow_pickle parameter controls whether or not the loading process can unpickle objects in the file. Object arrays contain elements that are Python objects, which need to be unpickled, so you need to set&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;to&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;True&lt;/code&gt;&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;to allow the loading process to unpickle the objects in the array.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div id=&quot;toc-div&quot; style=&quot;background: rgb(249, 249, 249); border-bottom: 1px solid rgb(217, 217, 217); box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; padding: 10px;&quot;&gt;&lt;h4 style=&quot;box-sizing: border-box; margin: 15px 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Table of Contents:&amp;nbsp;&lt;a class=&quot;open&quot; id=&quot;toc-toogle&quot; style=&quot;box-sizing: border-box; color: #039be5; margin-left: 30px; margin-right: 30px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div id=&quot;toc-list&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1064/valueerror-object-arrays-cannot-be-loaded-when-allow_pickle-false#what&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Problem&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1064/valueerror-object-arrays-cannot-be-loaded-when-allow_pickle-false#why&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Reasons of Error&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1064/valueerror-object-arrays-cannot-be-loaded-when-allow_pickle-false#resolve&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution Procedures&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1064/valueerror-object-arrays-cannot-be-loaded-when-allow_pickle-false#sol&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Coding Examples&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1064/valueerror-object-arrays-cannot-be-loaded-when-allow_pickle-false#Con&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;what&quot; name=&quot;what&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;The Problem&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Pickling is a process of converting a Python object, such as a list, dictionary, or custom object, into a byte stream that can be saved to a file or transmitted over a network. The byte stream can then be unpickled to reconstruct the original object.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In the case of numpy, the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.save()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;function can save numpy arrays to a file in a binary format, which can be loaded later using the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;function.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;When loading a numpy array from a file using&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;, if the file contains object arrays, the loading process needs to unpickle the objects to construct the original object arrays. This is where the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter comes in.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter is a boolean value that controls whether or not the loading process can unpickle objects in the file. By default, it is set to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;False&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;to prevent maliciously crafted pickle files from executing arbitrary code when loaded. When set to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;True&lt;/code&gt;&lt;/strong&gt;, it allows the loading process to unpickle the objects and construct the original object arrays.&lt;/span&gt;&lt;/p&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why&quot; name=&quot;why&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Reasons of Error&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;This error occurs because by default the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;function has the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter set to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;False&lt;/code&gt;&lt;/strong&gt;. This is a security measure to prevent maliciously crafted pickle files from executing arbitrary code when loaded.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Object arrays in numpy are a type of array that contains elements that are Python objects, which need to be unpickled in order to be loaded. When the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter is set to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;False&lt;/code&gt;&lt;/strong&gt;, the loading process is not able to unpickle these objects and the error is raised.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;By setting&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle=True&lt;/code&gt;&lt;/strong&gt;, it allows numpy to deserialize any Python objects in the file, enabling the loading of object arrays.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It&#39;s important to note that, loading untrusted data with allow_pickle=True is a security risk, you should make sure you trust the source and the data you are loading.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-purple&quot; style=&quot;background-color: #efd9fd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Note&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The `numpy.save()` function uses pickling internally, to save the object arrays, so it&#39;s not necessary to use the `pickle` library in conjunction with it.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;resolve&quot; name=&quot;resolve&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Solution Proceudres&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;To resolve the &quot;ValueError: object arrays cannot be loaded when allow_pickle=False&quot; error, you need to set the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;True&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;when calling the&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&amp;nbsp;function. This will allow the loading process to unpickle the objects in the file and construct the original object arrays.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here is an example:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import numpy as np&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-ini&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Load the numpy array with allow_pickle=True&lt;/span&gt;
&lt;span class=&quot;hljs-attr&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;data&lt;/span&gt; = np.load(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;file.npy&#39;&lt;/span&gt;, allow_pickle=&lt;span class=&quot;hljs-literal&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Alternatively, you can use&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;pickle.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;instead of&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;to load the object arrays.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-kotlin&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;import&lt;/span&gt; pickle

with &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;file.npy&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;rb&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; f:
    &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;data&lt;/span&gt; = pickle.load(f)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It&#39;s important to note that, loading untrusted data with&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle=True&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;is a security risk, you should make sure you trust the source and the data you are loading.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-pink&quot; style=&quot;background-color: #fef2f2; box-sizing: border-box; color: #b91c1c; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Caution&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;If you&#39;re loading data from a file that you didn&#39;t create, you should check the file extension(.npy) to ensure that it&#39;s a numpy file, maliciously crafted files can be disguised as numpy files.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol&quot; name=&quot;sol&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Coding Examples&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here are some examples of how you can use the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;function with the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter set to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;True&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;to load object arrays:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Example 1: Using&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-python&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; np

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Save an object array to a file&lt;/span&gt;
arr = np.array([&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;string&#39;&lt;/span&gt;, {&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;a&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt;}], dtype=&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;object&lt;/span&gt;)
np.save(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;object_array.npy&#39;&lt;/span&gt;, arr)

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Load the object array from the file&lt;/span&gt;
loaded_arr = np.load(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;object_array.npy&#39;&lt;/span&gt;, allow_pickle=&lt;span class=&quot;hljs-literal&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;True&lt;/span&gt;)
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(loaded_arr)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Output: [1 &#39;string&#39; {&#39;a&#39;: 1}]&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Example 2: Using&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;pickle.load()&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-python&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;import&lt;/span&gt; pickle

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Save an object array to a file&lt;/span&gt;
arr = np.array([&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;string&#39;&lt;/span&gt;, {&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;a&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt;}], dtype=&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;object&lt;/span&gt;)
np.save(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;object_array.npy&#39;&lt;/span&gt;, arr)

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Load the object array from the file&lt;/span&gt;
&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;object_array.npy&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;rb&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;as&lt;/span&gt; f:
    loaded_arr = pickle.load(f)
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(loaded_arr)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# Output: [1 &#39;string&#39; {&#39;a&#39;: 1}]&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In both examples, the object array is first saved using the numpy.save() function. Then, it is loaded using either numpy.load(allow_pickle=True) or pickle.load(). Setting the allow_pickle parameter to True or using pickle.load() allows the loading process to unpickle the objects in the file and construct the original object arrays.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-green&quot; style=&quot;background-color: #dff6dd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Tip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;It&#39;s also a good idea to update your numpy to the latest version, as it may contain security patches and bug fixes.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;Con&quot; name=&quot;Con&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In conclusion, the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&quot;ValueError: object arrays cannot be loaded when allow_pickle=False&quot;&lt;/strong&gt;&amp;nbsp;error occurs when attempting to load object arrays using the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;function with the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter set to&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;False&lt;/code&gt;&lt;/strong&gt;. This is because object arrays contain elements that are Python objects, which need to be unpickled in order to be loaded. To resolve this error, you can set the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;parameter to True when calling the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;numpy.load()&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;function. This will allow the loading process to unpickle the objects and construct the original object arrays. However, it&#39;s important to note that loading untrusted data with&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;allow_pickle=True&lt;/code&gt;&lt;/strong&gt;&amp;nbsp;is a security risk and you should make sure you trust the source and the data you are loading.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/7640289615894352922/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-valueerror-object-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7640289615894352922'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7640289615894352922'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-valueerror-object-arrays.html' title='How to Solve Valueerror: object arrays cannot be loaded when allow_pickle=false'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-3074856571798069762</id><published>2023-02-17T16:17:00.002+05:30</published><updated>2024-03-31T10:58:21.648+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>Error in grid.call(c_textbounds, as.graphicsannot(x$label), x$x, x$y, : polygon edge not found</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;The error message &quot;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;Error in grid.call(c_textbounds, as.graphicsannot(x$label), x$x, x$y, : polygon edge not found&lt;/code&gt;&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&quot; occurs when using the grid graphics package in R, specifically when trying to position text labels on a plot.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div id=&quot;toc-div&quot; style=&quot;background: rgb(249, 249, 249); border-bottom: 1px solid rgb(217, 217, 217); box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; padding: 10px;&quot;&gt;&lt;h4 style=&quot;box-sizing: border-box; margin: 15px 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Table of Contents:&amp;nbsp;&lt;a class=&quot;open&quot; id=&quot;toc-toogle&quot; style=&quot;box-sizing: border-box; color: #039be5; margin-left: 30px; margin-right: 30px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div id=&quot;toc-list&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1153/error-in-grid-call-c_textbounds-as-graphicsannot-x%24label-x%24x-x%24y-polygon-edge-not-found#what&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Problem&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1153/error-in-grid-call-c_textbounds-as-graphicsannot-x%24label-x%24x-x%24y-polygon-edge-not-found#why&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Reasons of Error&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1153/error-in-grid-call-c_textbounds-as-graphicsannot-x%24label-x%24x-x%24y-polygon-edge-not-found#resolve&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution Procedures&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1153/error-in-grid-call-c_textbounds-as-graphicsannot-x%24label-x%24x-x%24y-polygon-edge-not-found#sol&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Coding Examples&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1153/error-in-grid-call-c_textbounds-as-graphicsannot-x%24label-x%24x-x%24y-polygon-edge-not-found#Con&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;what&quot; name=&quot;what&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;The Problem&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;This error message typically occurs when using the grid graphics package in R and is related to the positioning of text labels on a plot. It suggests that the function c_textbounds, which is used to calculate the bounding box for text labels, is unable to locate the edges of the polygon that is being used to define the position of the text.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The c_textbounds function is used to calculate the bounding box for text labels, and it needs the edges of the polygon that is being used to define the position of the text in order to do so. The error message suggests that the function is unable to locate the edges of the polygon.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-green&quot; style=&quot;background-color: #dff6dd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Tip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Be mindful of the data and code used to define the polygon, as an error in this can cause the c_textbounds function to be unable to locate the edges.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why&quot; name=&quot;why&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Reasons of Error&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The reasons for the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;Error in grid.call(c_textbounds, as.graphicsannot(x$label), x$x, x$y, : polygon edge not found&lt;/code&gt;&lt;/strong&gt;&quot; error can include:&lt;/span&gt;&lt;/p&gt;&lt;ol style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Incorrect x and y coordinates: If the x and y coordinates for the text label are incorrect or out of range for the plot, the c_textbounds function will be unable to locate the edges of the polygon and will throw this error.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Error in data or code for defining the polygon: The error can also occur if there is an error in the data or code used to define the polygon. For example, if the polygon is not closed, the function will not be able to locate the edges.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Problem with the grid package or R installation: This error can also occur if there is an issue with the grid package or R installation. For example, if the grid package is not properly loaded or is an outdated version, it may not be able to locate the polygon edges.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Incorrect use of the grid package: If the grid package is used incorrectly, such as using a different coordinate system or not properly defining the polygon, this error can occur.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Other software or hardware issues: The error can also occur due to other software or hardware issues such as, outdated graphic driver or incompatible graphic card.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It&#39;s important to check all of these possible causes in order to identify and fix the underlying issue that is causing the error. Sharing a minimal reproducible example and the version of R and packages you are using can be helpful for others to understand the issue and provide a solution.&lt;/span&gt;&lt;/p&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;resolve&quot; name=&quot;resolve&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Best Possible Solutions&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here are some possible solutions for the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;Error in grid.call(c_textbounds, as.graphicsannot(x$label), x$x, x$y, : polygon edge not found&lt;/code&gt;&lt;/strong&gt;&quot; error:&lt;/span&gt;&lt;/p&gt;&lt;ol style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check the x and y coordinates: Double-check the x and y coordinates for the text label and ensure that they are correct and within the range of the plot.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Inspect the data and code used to define the polygon: Make sure that the data and code used to define the polygon are correct and do not contain any errors or inconsistencies.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Update the R and grid package: Make sure that you are using the latest version of R and the grid package. Updating can fix any bugs or compatibility issues that may be causing the error.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Use a different graphics package: Try using a different graphics package, such as ggplot2, to see if the error persists.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check for hardware and software issues: Make sure that your graphic card is compatible with your system and you have the latest graphic driver installed.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Share a minimal reproducible example: If the problem persists, it might be helpful to share a minimal reproducible example and the version of R and packages you are using, so that others can help you with more specific guidance.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Review the documentation of grid package: Review the documentation of grid package to ensure that you are using the package correctly and familiarize yourself with the proper syntax and usage of the functions.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Get help from the community: If you are still experiencing difficulties, reach out to the R community, such as on forums or mailing lists, for additional help and support.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol&quot; name=&quot;sol&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Coding Examples&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here are some different coding examples that demonstrate how to position text labels on a plot using the grid package in R:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Simple text label on a scatter plot:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-scss&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;library&lt;/span&gt;(grid)

x &amp;lt;- &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;rnorm&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;)
y &amp;lt;- &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;rnorm&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;)

# create scatter plot
&lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hljs-selector-class&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;.newpage&lt;/span&gt;()
&lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hljs-selector-class&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;.points&lt;/span&gt;(x, y)

# add text &lt;span class=&quot;hljs-selector-tag&quot; style=&quot;box-sizing: border-box; color: #7ee787;&quot;&gt;label&lt;/span&gt;
&lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hljs-selector-class&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;.text&lt;/span&gt;(&quot;Label&quot;, x=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0.5&lt;/span&gt;, y=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0.5&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Text label with different font and color:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-scss&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;library&lt;/span&gt;(grid)

x &amp;lt;- &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;rnorm&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;)
y &amp;lt;- &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;rnorm&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;)

# create scatter plot
&lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hljs-selector-class&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;.newpage&lt;/span&gt;()
&lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hljs-selector-class&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;.points&lt;/span&gt;(x, y)

# add text &lt;span class=&quot;hljs-selector-tag&quot; style=&quot;box-sizing: border-box; color: #7ee787;&quot;&gt;label&lt;/span&gt; with different &lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;font&lt;/span&gt; and &lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;color&lt;/span&gt;
&lt;span class=&quot;hljs-attribute&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;hljs-selector-class&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;.text&lt;/span&gt;(&quot;Label&quot;, x=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0.5&lt;/span&gt;, y=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0.5&lt;/span&gt;, gp=gpar(fontface=&quot;italic&quot;, col=&quot;red&quot;))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Text label on a bar plot:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-wasm&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;library&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;grid&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;data&lt;/span&gt; &amp;lt;- data.frame&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;x=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;1&lt;/span&gt;:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;5&lt;/span&gt;, y=c&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;10&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;20&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;30&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;40&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;))&lt;/span&gt;

# create bar plot
grid.newpage&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;()&lt;/span&gt;
grid.barplot&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;$y&lt;/span&gt;, width=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;)&lt;/span&gt;

# add text label
grid.text&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;Label&quot;&lt;/span&gt;, x=mean&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;)&lt;/span&gt;, y=max&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;)&lt;/span&gt;, vjust=&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;-0.2&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot; style=&quot;box-sizing: border-box;&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;div-purple&quot; style=&quot;background-color: #efd9fd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Note&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Remember to use the appropriate coordinate system, since grid package provides different coordinate systems like &#39;native&#39;, &#39;user&#39;, &#39;npc&#39; etc.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;Con&quot; name=&quot;Con&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In conclusion, the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;Error in grid.call(c_textbounds, as.graphicsannot(x$label), x$x, x$y, : polygon edge not found&lt;/code&gt;&lt;/strong&gt;&quot; error occurs when using the grid package in R to position text labels on a plot. This error can occur due to a number of reasons such as incorrect x and y coordinates for the text label, errors in the data or code used to define the polygon, problems with the grid package or R installation, or incorrect use of the grid package.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;To troubleshoot this error, it is important to check the x and y coordinates for the text label, inspect the data and code used to define the polygon, ensure that the latest version of R and the grid package are being used, and to share a minimal reproducible example and the version of R and packages you are using if the problem persists. Additionally, it&#39;s always good to check the documentation and use the appropriate coordinate system, and consider using other graphics packages available in R such as ggplot2 and lattice as an alternative.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/3074856571798069762/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/error-in-gridcallctextbounds.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3074856571798069762'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3074856571798069762'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/error-in-gridcallctextbounds.html' title='Error in grid.call(c_textbounds, as.graphicsannot(x$label), x$x, x$y, : polygon edge not found'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-3718797966730212017</id><published>2023-02-17T16:15:00.003+05:30</published><updated>2024-03-31T10:58:22.005+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>How to Solve IndexError: single positional indexer is out-of-bounds</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;This error message occurs in Pandas when trying to access a single element of a DataFrame using a single&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;indexer&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;and the&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;indexer&lt;/strong&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span face=&quot;Roboto, sans-serif&quot; style=&quot;color: #212121;&quot;&gt;is not within the valid range of the DataFrame&#39;s rows.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;For example, if the DataFrame has 5 rows, the valid indices are 0, 1, 2, 3, and 4. Attempting to access a row at&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;5 or higher would result in this error.&lt;/span&gt;&lt;/p&gt;&lt;div id=&quot;toc-div&quot; style=&quot;background: rgb(249, 249, 249); border-bottom: 1px solid rgb(217, 217, 217); box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; padding: 10px;&quot;&gt;&lt;h4 style=&quot;box-sizing: border-box; margin: 15px 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Table of Contents:&amp;nbsp;&lt;a class=&quot;open&quot; id=&quot;toc-toogle&quot; style=&quot;box-sizing: border-box; color: #039be5; margin-left: 30px; margin-right: 30px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div id=&quot;toc-list&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html#what&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Problem&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html#why&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Reasons of Error&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html#resolve&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution Procedures&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html#sol&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Coding Examples&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html#Con&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;what&quot; name=&quot;what&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;The Problem&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The problem statement for the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&lt;/code&gt;&lt;/strong&gt;&quot; error is that when trying to access a single element of a pandas DataFrame using a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;, the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;is not within the valid range of the DataFrame&#39;s rows. This can happen when trying to access a non-existent&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;, using a non-unique&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;, using an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;that is out of range, using a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;when a slice of indices is expected, or using the wrong accessor. This error can prevent data analysis and data manipulation tasks from being performed correctly and can make it difficult to extract the desired information from the DataFrame.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-green&quot; style=&quot;background-color: #dff6dd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Tip&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Check the index or column name: Make sure that the index or column name you are trying to access is spelled correctly and exists in the DataFrame.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why&quot; name=&quot;why&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Reasons of Error&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&lt;/code&gt;&lt;/strong&gt;&quot; error in pandas is typically caused by one of the following reasons:&lt;/span&gt;&lt;/p&gt;&lt;ol style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Attempting to access a row or column that is not present in the DataFrame: This can happen if you try to access a row or column that has been deleted or if you accidentally use the wrong&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column name.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column name that is not unique: When trying to access a specific row or column, pandas returns a DataFrame or Series with all the rows or columns that match the name or&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;. But if the name or&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;is not unique, the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;is out-of-bounds.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;that is out of range: If you try to access a row or column using an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;that is greater than the number of rows or columns in the DataFrame, you will get an &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&quot; error&lt;/code&gt;&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;when a slice of indices is expected: If you try to use a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;to select a slice of rows or columns, pandas will raise this error. For example,&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;df[1:5]&lt;/code&gt;&amp;nbsp;is correct, but&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;df[1]&lt;/code&gt;&amp;nbsp;is not.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using the wrong accessor: As mentioned in the previous answer, if you are trying to access a specific element of the DataFrame based on its&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;, you must use&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;.loc[]&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;.iloc[]&lt;/code&gt;&amp;nbsp;accessor, if you are using only a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It&#39;s important to make sure that the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column you are trying to access exists in the DataFrame, the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;is within the valid range, and that you are using the appropriate accessor.&lt;/span&gt;&lt;/p&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;resolve&quot; name=&quot;resolve&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Best Possible Solutions&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here are some solutions to fix the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&lt;/code&gt;&lt;/strong&gt;&quot; error in pandas:&lt;/span&gt;&lt;/p&gt;&lt;ol style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column name: Make sure that the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column name you are trying to access is spelled correctly and exists in the DataFrame.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check if the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column is unique: If the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column name is not unique, you can use the&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;.loc[]&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;.iloc[]&lt;/code&gt;&amp;nbsp;accessor to access the specific row or column you want.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;range: Make sure that the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;you are trying to access is within the valid range of indices for the DataFrame. You can use the&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;shape&lt;/code&gt;&amp;nbsp;attribute of the DataFrame to find the number of rows and use that to determine the valid range of indices.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Use the appropriate accessor: As mentioned in the previous answer, if you are trying to access a specific element of the DataFrame based on its&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;, you must use&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;.loc[]&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;.iloc[]&lt;/code&gt;&amp;nbsp;accessor, if you are using only a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Use slicing for multiple rows or columns: When you want to select multiple rows or columns, use slicing,&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;df[1:5]&lt;/code&gt;&amp;nbsp;is correct, but&amp;nbsp;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;df[1]&lt;/code&gt;&amp;nbsp;is not.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check the datatype of the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column: Make sure that the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column is of the correct data type, for example, if you are trying to access a column, make sure that it is a column and not a row.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check if the dataframe is empty: If the DataFrame is empty, it will raise &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&quot; error&lt;/code&gt;&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol&quot; name=&quot;sol&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Coding Examples&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Here are a few examples of how the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&lt;/code&gt;&lt;/strong&gt;&quot; error can occur when working with pandas DataFrames:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using a non-existent&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-bash&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import pandas as pd

&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;A&#39;&lt;/span&gt;: [1, 2, 3], &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;B&#39;&lt;/span&gt;: [4, 5, 6]})
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;#    A  B&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 0  1  4&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 1  2  5&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 2  3  6&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# this will raise an &quot;**`IndexError: single positional indexer is out-of-bounds`**&quot; error&lt;/span&gt;
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(df.loc[3]) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using a non-unique&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-bash&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import pandas as pd

&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;A&#39;&lt;/span&gt;: [1, 2, 3], &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;B&#39;&lt;/span&gt;: [4, 5, 6]}, index=[1,1,2])
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;#    A  B&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 1  1  4&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 1  2  5&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 2  3  6&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# this will raise an &quot;IndexError: single positional indexer is out-of-bounds&quot; error&lt;/span&gt;
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(df.loc[1]) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;that is out of range:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-bash&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import pandas as pd

&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;A&#39;&lt;/span&gt;: [1, 2, 3], &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;B&#39;&lt;/span&gt;: [4, 5, 6]})
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;#    A  B&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 0  1  4&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 1  2  5&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 2  3  6&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# this will raise an &quot;IndexError: single positional indexer is out-of-bounds&quot; error&lt;/span&gt;
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(df.loc[3]) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;when a slice of indices is expected:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-bash&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import pandas as pd

&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;A&#39;&lt;/span&gt;: [1, 2, 3], &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;B&#39;&lt;/span&gt;: [4, 5, 6]})
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;#    A  B&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 0  1  4&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 1  2  5&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# 2  3  6&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# this will raise an &quot;IndexError: single positional indexer is out-of-bounds&quot; error&lt;/span&gt;
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;[1])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Using the wrong accessor:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; color: #212121; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-bash&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;import pandas as pd

&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;A&#39;&lt;/span&gt;: [1, 2, 3], &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;B&#39;&lt;/span&gt;: [4, 5, 6]}, index=[&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;a&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;b&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;c&#39;&lt;/span&gt;])
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;)
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;#    A  B&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# a  1  4&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# b  2  5&lt;/span&gt;
&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# c  3  6&lt;/span&gt;

&lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;# this will raise an &quot;IndexError: single positional indexer is out-of-bounds&quot; error&lt;/span&gt;
&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;df&lt;/span&gt;[&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;a&#39;&lt;/span&gt;]) 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;div-purple&quot; style=&quot;background-color: #efd9fd; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span class=&quot;alert-header&quot; style=&quot;box-sizing: border-box; display: block; margin-bottom: 10px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Note&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;alert-body&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Check the index range: Make sure that the index you are trying to access is within the valid range of indices for the DataFrame.&lt;/span&gt;&lt;/div&gt;&lt;h2 style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;Con&quot; name=&quot;Con&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In conclusion, the &quot;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f3f3f3; border: none; box-sizing: border-box; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif;&quot;&gt;IndexError: single positional indexer is out-of-bounds&lt;/code&gt;&lt;/strong&gt;&quot; error in pandas is caused by attempting to access a single element of a DataFrame using a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;and the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;is not within the valid range of the DataFrame&#39;s rows. There are several common causes of this error, such as using a non-existent&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;, using a non-unique&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;, using an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;that is out of range, using a single&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;indexer&lt;/strong&gt;&amp;nbsp;when a slice of indices is expected, and using the wrong accessor.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;To avoid this error, you should check the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column name, use the appropriate accessor, use slicing for multiple rows or columns, check if the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column is unique, check the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;range, check the datatype of the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;index&lt;/strong&gt;&amp;nbsp;or column, check if the dataframe is empty, be mindful of the order of the operations and keep good track of the dataframe.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/3718797966730212017/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3718797966730212017'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3718797966730212017'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-indexerror-single.html' title='How to Solve IndexError: single positional indexer is out-of-bounds'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-7336397461108987960</id><published>2023-02-17T16:08:00.001+05:30</published><updated>2024-03-31T10:58:23.949+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>C# error: &quot; An object reference is required for the non-static field, method, or property&quot;</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: arial, helvetica, sans-serif;&quot;&gt;Today, we&#39;re going to fix a C# Error:&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #b96ad9;&quot;&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&quot;An object reference is required for the non-static field, method, or property&quot;&lt;/em&gt;&lt;/span&gt;&amp;nbsp;that usually happens when we try to use a&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;non-static&lt;/em&gt;&amp;nbsp;element in a&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static&lt;/em&gt;&amp;nbsp;scope. In the next few sections, we&#39;ll discuss what is &quot;static&quot; modifier keyword in C# is, why this problem happens, and how to resolve it.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;div id=&quot;toc-div&quot; style=&quot;background: rgb(249, 249, 249); border-bottom: 1px solid rgb(217, 217, 217); box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; padding: 10px;&quot;&gt;&lt;h4 style=&quot;box-sizing: border-box; margin: 15px 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&amp;nbsp;Table of Contents:&amp;nbsp;&lt;a class=&quot;open&quot; id=&quot;toc-toogle&quot; style=&quot;box-sizing: border-box; color: #039be5; margin-left: 30px; margin-right: 30px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div id=&quot;toc-list&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774610&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;What is the &quot;static&quot; modifier keyword&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774611&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Why &quot;An object reference is required for the non-static field, method, or property&quot; happens&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774612&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solution&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774613&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774614&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Reference&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;_Toc123774610&quot; name=&quot;_Toc123774610&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;What is &quot;static&quot; modifier keyword&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774610&quot; id=&quot;TypeError-str-object&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;It&#39;s very important to understand what is&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static&lt;/em&gt;&amp;nbsp;modifier keyword in C# before exploring the reason behind&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #b96ad9;&quot;&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&quot;An object reference is required for the non-static field, method, or property&quot;&lt;/em&gt;&lt;/span&gt;. A&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static&lt;/em&gt;&amp;nbsp;modifier keyword can be applied to various elements such as properties, methods, class, and constructor. Here, we&#39;ll cover the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static properties&lt;/em&gt;&amp;nbsp;and&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static methods&lt;/em&gt;&amp;nbsp;since these two are the main reason of&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #b96ad9;&quot;&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&quot;An object reference is required for the non-static field, method, or property&quot;&lt;/em&gt;&lt;/span&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;Static properties&lt;/em&gt;&amp;nbsp;are basically the properties of a class with the &quot;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static&lt;/em&gt;&quot; keyword:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
 &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt; txt = &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;;
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Here, we initialise a static property,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;txt&lt;/em&gt;&amp;nbsp;under&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;Program&lt;/em&gt;&amp;nbsp;class.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;Static methods&lt;/em&gt;&amp;nbsp;are basically the methods with a &quot;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static&lt;/em&gt;&quot; keyword. Here&#39;s the syntax of defining a&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/em&gt;:&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;box-sizing: border-box; margin-bottom: 30px; margin-top: 30px;&quot;&gt;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black; padding: 15px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;lt;Access_Modifier&amp;gt; static &amp;lt;return_type&amp;gt; &amp;lt;method_name&amp;gt;([&amp;lt;params&amp;gt;])&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Let&#39;s look at an example:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-cpp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; x, &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; y)&lt;/span&gt;
    &lt;/span&gt;{
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; x * y;
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;So, what&#39;s the difference between the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;and&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;instance method&lt;/strong&gt;&lt;/em&gt;? A&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/em&gt;&amp;nbsp;can be called without creating an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;of the class containing it. It can be invoked by the class itself or the Objects of the class. Conversely, an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;instance method&lt;/em&gt;&amp;nbsp;is a method without&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static&lt;/em&gt;&amp;nbsp;modifier keyword, which requires creating an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;of the class containing it so that the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;can be used to invoke that function.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Now, let&#39;see how the error,&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #b96ad9;&quot;&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&quot;An object reference is required for the non-static field, method, or property&quot;&lt;/em&gt;&lt;/span&gt;&amp;nbsp;will happen.&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;_Toc123774611&quot; name=&quot;_Toc123774611&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;Why &quot;An object reference is required for the non-static field, method, or property&quot; happens&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774611&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This error always happen when we&#39;re trying to use the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;non-static properties&lt;/em&gt;&amp;nbsp;or&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;non-static methods&lt;/em&gt;&amp;nbsp;in a static scope. Let&#39;s look at them one by one:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
 &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt; txt = &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;;
        
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt;[] args&lt;/span&gt;)&lt;/span&gt; 
        {
            Console.WriteLine(txt);
        }
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is the output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-vbnet&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;span class=&quot;hljs-symbol&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;CS0120:&lt;/span&gt; An &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;object&lt;/span&gt; reference &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;is&lt;/span&gt; required &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;for&lt;/span&gt; the non-&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; field, method, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;property&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;&#39;Program.txt&#39;&lt;/span&gt;
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is because the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;&lt;/em&gt;,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;public static void Main(string[] args)&lt;/em&gt;&amp;nbsp;is associated with the class itself, not the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;of the class containing it. Hence, the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;non-static property&lt;/strong&gt;&lt;/em&gt;,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;txt&lt;/em&gt;&amp;nbsp;which requires an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;of the class to work will not work in a&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;&lt;/em&gt;. Let&#39;s look at another example:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
  &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt;[] args&lt;/span&gt;)&lt;/span&gt; 
        {
            &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; x = &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;3&lt;/span&gt;;
            &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; y = &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;2&lt;/span&gt;;
            Console.WriteLine(multiply(x, y));
        }

    &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;multiply&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; x, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; y&lt;/span&gt;)&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; x * y;
    }
}
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is the output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-vbnet&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;span class=&quot;hljs-symbol&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;CS0120:&lt;/span&gt; An &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;object&lt;/span&gt; reference &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;is&lt;/span&gt; required &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;for&lt;/span&gt; the non-&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; field, method, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;property&lt;/span&gt; &lt;span class=&quot;hljs-comment&quot; style=&quot;box-sizing: border-box; color: #8b949e;&quot;&gt;&#39;Program.multiply(int,int)&#39;&lt;/span&gt;
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Same idea, the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;instance method&lt;/strong&gt;&lt;/em&gt;,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;public int multiply(int x, int y)&lt;/em&gt;&amp;nbsp;needs an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;of class,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;Program&lt;/em&gt;&amp;nbsp;to be created to invoke it in a&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;&lt;/em&gt;, which doesn&#39;t implicitly create an Object of the class,&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Program&lt;/strong&gt;&lt;/em&gt;. Now, we&#39;ve understood the causes of this error, let&#39;see how to resolve it.&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;_Toc123774612&quot; name=&quot;_Toc123774612&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;Solution&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774612&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;We just need to be aware of the following rules to avoid this error:&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black; margin-bottom: 30px; margin-top: 30px; padding: 15px;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Avoid using&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;non-static property&lt;/strong&gt;&amp;nbsp;in a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;.&lt;/span&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Avoid using&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;non-static method&lt;/strong&gt;&amp;nbsp;in a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;.&lt;/span&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Explicitly initialise an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/strong&gt;&amp;nbsp;of the class containing the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;non-static property&lt;/strong&gt;&amp;nbsp;before using it in a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;.&lt;/span&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Explicitly initialise an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/strong&gt;&amp;nbsp;of the class containing the&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;non-static method&lt;/strong&gt;&amp;nbsp;before using it in a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;static method&lt;/strong&gt;.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Let&#39;s see some examples:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
 &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt; txt = &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;;
        
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt;[] args&lt;/span&gt;)&lt;/span&gt; 
        {
            Console.WriteLine(txt);
        }
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is the output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-undefined&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
Hello World
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is because we&#39;re now having the static property,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;txt&lt;/em&gt;, and we can use it directly in the&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static method&lt;/em&gt;. Let&#39;s see another example:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
 &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt; txt = &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;;

        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt;[] args&lt;/span&gt;)&lt;/span&gt;
        {
            Program p = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;new&lt;/span&gt; Program();
            Console.WriteLine(p.txt);
        }
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is the output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-undefined&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
Hello World
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Here, we explicitly create an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;p&lt;/em&gt;&amp;nbsp;of the class,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;Program&lt;/em&gt;. Hence, we can utilise that object to use the non-static variable,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;txt&lt;/em&gt;&amp;nbsp;in the&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static method&lt;/em&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Now, let&#39;s see the examples for &quot;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;methods&lt;/em&gt;&quot;:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
  &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt;[] args&lt;/span&gt;)&lt;/span&gt;
        {
            &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; x = &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;3&lt;/span&gt;;
            &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; y = &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;2&lt;/span&gt;;
            Console.WriteLine(multiply(x, y));
        }

        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;multiply&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; x, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; y&lt;/span&gt;)&lt;/span&gt;
        {
            &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; x * y;
        }
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is the output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-undefined&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
6
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is because we&#39;re now defining the method,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;public static int multiply(int x, int y)&lt;/em&gt;&amp;nbsp;as static method, and we can use it directly in other&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static method&lt;/em&gt;. Let&#39;s see another example:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
  &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Program&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;Main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;string&lt;/span&gt;[] args&lt;/span&gt;)&lt;/span&gt;
        {
            Program p = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;new&lt;/span&gt; Program();
            &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; x = &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;3&lt;/span&gt;;
            &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; y = &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;2&lt;/span&gt;;
            Console.WriteLine(p.multiply(x, y));
        }

        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;public&lt;/span&gt;  &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;multiply&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; x, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; y&lt;/span&gt;)&lt;/span&gt;
        {
            &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; x * y;
        }
    }
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;This is the output:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-undefined&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
6
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Here, we also explicitly create an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;p&lt;/em&gt;&amp;nbsp;of the class,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;Program&lt;/em&gt;. Hence, we can utilise that object to use the instance method,&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;public int multiply(int x, int y)&lt;/em&gt;&amp;nbsp;in the&amp;nbsp;&lt;em style=&quot;background-color: #ced4d9; box-sizing: border-box; color: black;&quot;&gt;static method&lt;/em&gt;.&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;_Toc123774613&quot; name=&quot;_Toc123774613&quot; style=&quot;box-sizing: border-box; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;The Conclusion&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;https://kodlogs.net/1231/c%23-error-an-object-reference-is-required-for-the-non-static-field-method-or-property#_Toc123774613&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;That&#39;s. Clear and simple understanding on this topic! In this study blog, we&#39;ve learn what is a&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static modifier keyword&lt;/em&gt;, why&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #b96ad9;&quot;&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&quot;An object reference is required for the non-static field, method, or property&quot;&lt;/em&gt;&lt;/span&gt;&amp;nbsp;happens and how to resolve it. You should always make sure that you&#39;re not using&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;non-static&lt;/em&gt;&amp;nbsp;item in&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;static&lt;/em&gt;&amp;nbsp;item. If you want to do so, make sure you explicitly create an&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;of the class containing the&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;non-static&lt;/em&gt;&amp;nbsp;items, so that you can utilise that&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Object&lt;/em&gt;&amp;nbsp;to accessthem.&lt;/span&gt;&lt;/p&gt;&lt;div&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 15px;&quot;&gt;&lt;/section&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/7336397461108987960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/c-error-object-reference-is-required.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7336397461108987960'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/7336397461108987960'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/c-error-object-reference-is-required.html' title='C# error: &quot; An object reference is required for the non-static field, method, or property&quot;'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-5525085822792698846</id><published>2023-02-17T16:03:00.002+05:30</published><updated>2024-03-31T10:58:22.539+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><title type='text'>How to solve Expected Primary Expression Before</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;While writing a program, a programmer is sure to face some obscurities. Sometimes it is difficult to find what causes an error in the piece of code that you have written. The error you face can be a logical error, compiling error, or a simple syntax error. In C++, you might have encountered the error&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;/strong&gt;&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: black;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; color: #ff04fc;&quot;&gt;“expected primary expression”&lt;/span&gt;&amp;nbsp;&lt;/span&gt;which usually occurs due to a&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; color: #ff04fc;&quot;&gt;syntax error&lt;/span&gt;&amp;nbsp;In this article, we are going to look over the details of the error with the help of examples.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;div id=&quot;toc-div&quot; style=&quot;background: rgb(249, 249, 249); border-bottom: 1px solid rgb(217, 217, 217); box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; padding: 10px;&quot;&gt;&lt;h4 style=&quot;box-sizing: border-box; margin: 15px 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Table of Contents:&amp;nbsp;&lt;a class=&quot;open&quot; id=&quot;toc-toogle&quot; style=&quot;box-sizing: border-box; color: #039be5; margin-left: 30px; margin-right: 30px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div id=&quot;toc-list&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#what&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;What is the error and why does it occur?&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#why&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Reasons for the Error&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#why1&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Specifying Data Type Along With Function Calling&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#why2&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Wrong Argument Type&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#why3&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Incomplete Curly Braces&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#sol&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Solutions to the error&lt;/span&gt;&lt;/a&gt;&lt;ol style=&quot;box-sizing: border-box;&quot;&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#sol1&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Removing the data type while calling function&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#sol2&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Using Correct Data Type&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box; padding-left: 10px;&quot;&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#sol3&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Even Number of Curly Braces&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#con&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: #039be5; text-decoration-line: none;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;what&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;/strong&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;What is the error and why does it occur?&lt;/strong&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#what&quot; id=&quot;TypeError-str-object&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;‘expected primary expression‘&lt;/strong&gt;&amp;nbsp;is one of the most common syntax errors which occur due to a&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;missing keyword&lt;/strong&gt;&amp;nbsp;or an&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;irrelevant keyword.&lt;/strong&gt;&amp;nbsp;The error prompt displays the location where the keyword is missing.&amp;nbsp;&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;These mistakes are encountered by beginners and people who write their programs in a hurry. The syntax of the error prompt looks something like this:&lt;/span&gt;&lt;/p&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Syntax:&lt;/span&gt;&lt;/strong&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-go&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
/tmp/&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;35&lt;/span&gt;HQ008IAw.cpp:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;11&lt;/span&gt;:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;48&lt;/span&gt;: &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;error&lt;/span&gt;: expected primary-expression before &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;words&#39;&lt;/span&gt;
&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; wordLength  = wordLengthFunction(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;string&lt;/span&gt; words);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;/p&gt;&lt;div class=&quot;div-blue&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;The keyword&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;‘words’&lt;/strong&gt;&amp;nbsp;in the above example denotes where the keyword is missing or added.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Reasons for the Error&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#why&quot; id=&quot;reasons-that-will-cause&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;&lt;/strong&gt;There are specific reasons why the programmer can face this error and some of the reasons can be:&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why1&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h3 style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Specifying Data Type Along With Function Calling&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;One of the most common mistakes that novice coders make is passing the data type along with the function-calling argument.&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;For Example&lt;/strong&gt;, we will be creating a function to add 2 numbers and pass two integers in the function. When calling the function, we will specify the data type again which will trigger the error. The code for the above example will be:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
    &lt;span class=&quot;hljs-meta&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;#include &lt;/span&gt;
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;std&lt;/span&gt;;
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;add&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; num_one, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; num_two&lt;/span&gt;)&lt;/span&gt;
        {
        &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; result;
        result = num_one + num_two;
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; result;
        }
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;main&lt;/span&gt;()&lt;/span&gt;
        {
        &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; answer = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;add&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;230&lt;/span&gt;);
        }
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;;
        }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The above code contains the data type declaration along with the function-calling argument which will generate the following output&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Output:&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
            
            /tmp/CVK1ULcyYx.cpp:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;12&lt;/span&gt;:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;18&lt;/span&gt;: error: expected primary-expression before &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;int&#39;&lt;/span&gt;
            &amp;nbsp;&amp;nbsp;&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;12&lt;/span&gt; | &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; answer = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;add&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;230&lt;/span&gt;);
            &amp;nbsp;&amp;nbsp;|&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;                        ^~~
             /tmp/CVK1ULcyYx.cpp:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;12&lt;/span&gt;:&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;27&lt;/span&gt;: error: expected primary-expression before &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&#39;int&#39;&lt;/span&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;12&lt;/span&gt; | &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; answer = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;add&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;230&lt;/span&gt;);
             &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;| &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ^~~
                }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The output for the code is the specified error and the location where we have declared our data types again.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why2&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h3 style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Wrong Argument Type&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;A lot of times if you pass the wrong argument in a particular function it will produce the same ‘primary expression’ error.&amp;nbsp;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;For Example&lt;/strong&gt;, if you declare a string in a function and then pass an integer in the argument.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
    
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;main&lt;/span&gt;()&lt;/span&gt;
        {
        &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; answer = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;add&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;Hello&quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot; style=&quot;box-sizing: border-box; color: #a5d6ff;&quot;&gt;&quot;How are You&quot;&lt;/span&gt; );
        }
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;;
        }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;When the above code is executed, we will face the same error which will tell us that we passed a string to a function as we cannot pass a string to the function.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;why3&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h3 style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Incomplete Curly Braces&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;If the number of curly braces is not even or are extra than the requirement then the developer is likely to face this error. An example of incomplete curly braces would be:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-cpp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
    
        &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;hljs-params&quot; style=&quot;box-sizing: border-box;&quot;&gt;(&lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; num_1, &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; num_2 )&lt;/span&gt;
        &lt;/span&gt;{
        &lt;span class=&quot;hljs-type&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;int&lt;/span&gt; result;
        result = num_1 + num_2;
        &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; result;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;In the above code, you might notice that there is no closing brace which will result in the stated error.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;div-pink&quot; style=&quot;background-color: #fef2f2; box-sizing: border-box; color: #b91c1c; margin-bottom: 15px; padding: 20px; position: relative; text-align: justify;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Note: The error can also be invoked if the parenthesis in an if statement is empty&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;Solutions to the error&lt;/strong&gt;&lt;/span&gt;&lt;a href=&quot;https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#sol&quot; id=&quot;the-solution&quot; rel=&quot;nofollow&quot; style=&quot;box-sizing: border-box; color: blue; text-decoration-line: none;&quot;&gt;#&lt;/a&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Now that we know about the reasons for the occurrence of the error we can find solutions to fix the error or measures to prevent it.&amp;nbsp; The solutions for correcting the error are given below:&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;section style=&quot;box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol1&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h3 style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Removing the data type while calling function&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;You have to avoid writing the data type in the parenthesis while calling a function. This is a way to solve the error. The proper way to pass arguments to a function is:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-csharp&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;

    &lt;span class=&quot;hljs-function&quot; style=&quot;box-sizing: border-box;&quot;&gt;&lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;hljs-title&quot; style=&quot;box-sizing: border-box; color: #d2a8ff;&quot;&gt;main&lt;/span&gt;()&lt;/span&gt; {
    &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;int&lt;/span&gt; answer = &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;add&lt;/span&gt;( &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;100&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;230&lt;/span&gt;);
    }
    &lt;span class=&quot;hljs-keyword&quot; style=&quot;box-sizing: border-box; color: #ff7b72;&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-number&quot; style=&quot;box-sizing: border-box; color: #79c0ff;&quot;&gt;0&lt;/span&gt;;
    }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;While passing the arguments in a function make sure that you double-check for any missing arguments through the function.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;sol2&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h3 style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Using Correct Data Type&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;You must ensure that the data type of the argument matches the data type which is specified in the given parameter which will ensure that the error does not occur. An example would be passing integers in the function “add” instead of strings.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h3 style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;Even Number of Curly Braces&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The program should be carefully observed and should have even number of opening and closing brackets. If they are not, make sure to look for the brace which is incomplete. The below example will demonstrate this point.&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;background-color: #eeeeee; border-color: rgb(221, 221, 221); border-image: initial; border-radius: 6px; border-style: solid; border-width: 1px 1px 1px 3px; box-sizing: border-box; margin-bottom: 1em; margin-top: 0px; max-height: 400px; overflow: auto; padding: 0px; width: 751px;&quot;&gt;&lt;code class=&quot;hljs language-kotlin&quot; style=&quot;background: rgb(13, 17, 23); border: none; box-sizing: border-box; color: #c9d1d9; display: block; font-family: Consolas, Menlo, Monaco, &amp;quot;Lucida Console&amp;quot;, &amp;quot;Liberation Mono&amp;quot;, &amp;quot;DejaVu Sans Mono&amp;quot;, &amp;quot;Bitstream Vera Sans Mono&amp;quot;, &amp;quot;Courier New&amp;quot;, monospace, sans-serif; overflow-x: auto; padding: 1em;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;

    &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;Int&lt;/span&gt; main(){
    &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;Int&lt;/span&gt; a,b;
    &lt;span class=&quot;hljs-built_in&quot; style=&quot;box-sizing: border-box; color: #ffa657;&quot;&gt;Int&lt;/span&gt; c = a+b;
    } &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;The above output is having an opening curly brace after ‘main’ and a closing curly brace at the end which will reduce the likeliness of occurrence of the stated error.&lt;/span&gt;&lt;/p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;a class=&quot;custom_anchor&quot; id=&quot;con&quot; style=&quot;box-sizing: border-box; color: #039be5; position: relative; top: -55px;&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;h2 style=&quot;box-sizing: border-box; font-weight: 300;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;&lt;strong style=&quot;box-sizing: border-box;&quot;&gt;The Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;box-sizing: border-box; margin-top: 0px;&quot;&gt;&lt;span face=&quot;arial, helvetica, sans-serif&quot; style=&quot;box-sizing: border-box; font-size: medium;&quot;&gt;In this article we dived deep into “expected primary expression before” which is merely a syntax error and can be solved with the help of some observation and attentiveness while writing the program. The programmer should remember the key points like never pass parameters along with arguments when calling a function and ensuring equal number of opening and closing curly braces so as to avoid facing this error.&lt;/span&gt;&lt;/p&gt;&lt;/section&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/5525085822792698846/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/5525085822792698846'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/5525085822792698846'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/how-to-solve-expected-primary.html' title='How to solve Expected Primary Expression Before'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-1410357174189186950</id><published>2023-02-12T16:30:00.001+05:30</published><updated>2024-03-31T10:58:20.492+05:30</updated><title type='text'> Benefits of using Artificial Intelligence in  Marketing</title><content type='html'>&lt;p&gt;In this article, we will see the benifits of the AI in marketing, Artificial intelligence (AI) has&lt;/p&gt;&lt;p&gt;been transforming various industries, and marketing is no exception. From customer&lt;/p&gt;&lt;p&gt;segmentation to personalized messaging and predictive analytics, AI has become a crucial&lt;/p&gt;&lt;p&gt;tool for modern marketers to reach and engage their target audience effectively.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Table of Content&lt;/h3&gt;&lt;p&gt;1. Improved Customer Segmentation&lt;/p&gt;&lt;p&gt;2. Predictive Analytics&lt;/p&gt;&lt;p&gt;3. Personalized Messaging&lt;/p&gt;&lt;p&gt;4. Increased Efficiency&lt;/p&gt;&lt;p&gt;5. mproved ROI&lt;/p&gt;&lt;p&gt;6. Enhanced Customer Experience&lt;/p&gt;&lt;p&gt;7. Conclusion&lt;/p&gt;&lt;p&gt;Here are some of the benefits of using AI in marketing:&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Improved Customer Segmentation&lt;/h3&gt;&lt;p&gt;AI algorithms can analyze vast amounts of data, including customer behavior,&lt;/p&gt;&lt;p&gt;demographics, and purchase history, to create detailed customer segments. This allows&lt;/p&gt;&lt;p&gt;marketers to personalize their campaigns and messages for each segment, which can lead&lt;/p&gt;&lt;p&gt;to higher conversion rates and improved customer satisfaction.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Predictive Analytics&lt;/h3&gt;&lt;p&gt;AI-powered predictive analytics enables marketers to forecast future customer behavior and&lt;/p&gt;&lt;p&gt;market trends based on historical data. This information can help marketers make informed&lt;/p&gt;&lt;p&gt;decisions about their strategies, tactics, and budget allocation, leading to more effective&lt;/p&gt;&lt;p&gt;campaigns and better results.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Personalized Messaging&lt;/h3&gt;&lt;p&gt;With AI, marketers can create highly targeted and personalized messages for each&lt;/p&gt;&lt;p&gt;individual customer. This can include personalized product recommendations, targeted email&lt;/p&gt;&lt;p&gt;campaigns, and customized social media posts. By tailoring messages to specific&lt;/p&gt;&lt;p&gt;customers, marketers can increase the relevance and impact of their campaigns, leading to&lt;/p&gt;&lt;p&gt;higher engagement and conversion rates.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Increased Efficiency&lt;/h3&gt;&lt;p&gt;AI-powered marketing automation can streamline many of the manual and repetitive tasks&lt;/p&gt;&lt;p&gt;involved in marketing, freeing up marketers to focus on more strategic initiatives. Automation&lt;/p&gt;&lt;p&gt;can also increase the speed and accuracy of tasks such as data analysis, lead scoring, and&lt;/p&gt;&lt;p&gt;lead nurturing, leading to a more efficient and effective marketing process.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Improved ROI&lt;/h3&gt;&lt;p&gt;By using AI to analyze data, personalize messages, and automate tasks, marketers can&lt;/p&gt;&lt;p&gt;improve their return on investment (ROI) for their marketing campaigns. AI can help&lt;/p&gt;&lt;p&gt;marketers reach the right audience, with the right message, at the right time, leading to&lt;/p&gt;&lt;p&gt;higher conversion rates and better results.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Enhanced Customer Experience&lt;/h3&gt;&lt;p&gt;AI-powered chatbots and virtual assistants can provide customers with immediate and&lt;/p&gt;&lt;p&gt;personalized support, answering questions, resolving issues, and making recommendations.&lt;/p&gt;&lt;p&gt;This can lead to a better customer experience and increased customer loyalty, which can&lt;/p&gt;&lt;p&gt;drive long-term growth for businesses.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Conclusion&lt;/h3&gt;&lt;p&gt;In conclusion, AI has the potential to transform the way marketers reach and engage their&lt;/p&gt;&lt;p&gt;customers, leading to improved customer segments, predictive analytics, personalized&lt;/p&gt;&lt;p&gt;messaging, increased efficiency, improved ROI, and enhanced customer experiences. While&lt;/p&gt;&lt;p&gt;there is still much work to be done to fully realize the potential of AI in marketing, the benefits&lt;/p&gt;&lt;p&gt;are clear, and marketers who embrace AI are well positioned to succeed in the years ahead.&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/1410357174189186950/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/benefits-of-using-artificial.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1410357174189186950'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1410357174189186950'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/benefits-of-using-artificial.html' title=' Benefits of using Artificial Intelligence in  Marketing'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-4074655703108767024</id><published>2023-02-06T16:00:00.081+05:30</published><updated>2023-02-06T16:00:00.195+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python Program to Copy the list </title><content type='html'>&lt;p&gt;In this article, we will learn to copy lists in a python program. There are so many different ways to copy a list in python.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/span&gt;Method 1: Using the Slicing Technique&lt;/h3&gt;&lt;div&gt;The simplest way to clone or copy a list is to use the slice method. This method creates a new list that contains all the elements of the original list. Here&#39;s how you can use it:&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h4&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we will initialize the list1.&lt;/li&gt;&lt;li&gt;Then we will store all the elements of list1 to list1 using the &lt;b&gt;slicing method&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;Then we will print the original list1 and list2.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Program&lt;/h4&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1
2
3
4
5
6
7
8
9&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# initialize the list1&lt;/span&gt;
list1 = [1, 2, 3, 4, 5, 6]

&lt;span style=&quot;color: green;&quot;&gt;# copying to list2 using sclicing method&lt;/span&gt;
list2 = list1[:]

&lt;span style=&quot;color: green;&quot;&gt;# print the result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Original List:&quot;&lt;/span&gt;, list1)
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;After Copying:&quot;&lt;/span&gt;, list2)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output&lt;/h4&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;Original List: [1, 2, 3, 4, 5, 6]
After Copying: [1, 2, 3, 4, 5, 6]
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Method 2: Using extend() methods&lt;/h3&gt;&lt;div&gt;The list elements copy to the new list using extend() function in python. Here&#39;s how you can use it.&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h4&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we will initialize list1 and list2.&lt;/li&gt;&lt;li&gt;list2 is empty.&lt;/li&gt;&lt;li&gt;Then we will use &lt;b&gt;extend()&lt;/b&gt; function to appends all the elements to list2.&lt;/li&gt;&lt;li&gt;Then, we will print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Program&lt;/h4&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# initialize the list1&lt;/span&gt;
list1 = [1, 2, 3, 4, 5, 6]

&lt;span style=&quot;color: green;&quot;&gt;# initialize the list2&lt;/span&gt;
list2 = []

&lt;span style=&quot;color: green;&quot;&gt;# copying to list2 using extend() function&lt;/span&gt;
list2.extend(list1)

&lt;span style=&quot;color: green;&quot;&gt;# print the result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Original List:&quot;&lt;/span&gt;, list1)
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;After Copying:&quot;&lt;/span&gt;, list2)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output&lt;/h4&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;Original List: [1, 2, 3, 4, 5, 6]
After Copying: [1, 2, 3, 4, 5, 6]
&lt;/pre&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Method 3: Using (=)Assignment Operator&amp;nbsp;&lt;/h3&gt;&lt;div&gt;This is the easiest or simple method to copy the list in python. We will use the assignment operator to copy the list in python.&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h4&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we will initialize the list1.&lt;/li&gt;&lt;li&gt;Copy the list1 to list2 using the assignment operator(=).&lt;/li&gt;&lt;li&gt;Then, print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Program&lt;/h4&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1
2
3
4
5
6
7
8
9&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# initialize the list1&lt;/span&gt;
list1 = [1, 2, 3, 4, 5, 6]

&lt;span style=&quot;color: green;&quot;&gt;# copy the list1 to list2 using assigment operators&lt;/span&gt;
list2 = list1

&lt;span style=&quot;color: green;&quot;&gt;# print the result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Original List:&quot;&lt;/span&gt;, list1)
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;After Copying:&quot;&lt;/span&gt;, list2)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output&lt;/h4&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;Original List: [1, 2, 3, 4, 5, 6]
After Copying: [1, 2, 3, 4, 5, 6]
&lt;/pre&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Conclusion&lt;/h3&gt;&lt;div&gt;In this article, we learned, how to copy or cloning a list in python using different methods and techniques. All the methods is right and we can use according to our function.&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/4074655703108767024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/python-program-to-copy-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/4074655703108767024'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/4074655703108767024'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/python-program-to-copy-list.html' title='Python Program to Copy the list '/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-8212157268608665879</id><published>2023-02-05T16:00:00.005+05:30</published><updated>2023-02-05T16:00:00.198+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Remove multiple elements from a list in Python</title><content type='html'>&lt;p&gt;In this article, we will remove multiple elements from the list in python programming.&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Example&lt;/h4&gt;&lt;div&gt;&lt;b&gt;Input&lt;/b&gt; : [ 1, 2, 3, 4]&lt;/div&gt;&lt;div&gt;&lt;b&gt;Remove&lt;/b&gt; : [2, 4]&lt;/div&gt;&lt;div&gt;&lt;b&gt;Output&lt;/b&gt;: New_list =&amp;nbsp;&amp;nbsp;[1, 3]&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/span&gt;Method 1: Iterating&lt;/h3&gt;&lt;div&gt;In this method, we will remove the elements one by one, which is divisible by 2 using the&amp;nbsp;&lt;b&gt;remove()&lt;/b&gt; function.&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h4&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Initialize the list&lt;/li&gt;&lt;li&gt;Then we will iterate the list.&lt;/li&gt;&lt;li&gt;Then we will apply the condition.&lt;/li&gt;&lt;li&gt;Then we will use the &lt;b&gt;remove()&lt;/b&gt; function to remove the element from the list&lt;/li&gt;&lt;li&gt;After that, we will print the final updated list.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Program&lt;/h4&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# initialize the list&lt;/span&gt;
list = [1, 2, 3, 4, 5, 6, 7, 8]

&lt;span style=&quot;color: green;&quot;&gt;# iterate the list&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list:
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; (ele%2==0):
        list.remove(ele)  &lt;span style=&quot;color: green;&quot;&gt;# use remove() function&lt;/span&gt;

&lt;span style=&quot;color: green;&quot;&gt;# print the list after removing elements&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(list)
   
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output&lt;/h4&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;[1, 3, 5, 7]
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Method 2: List Comprehension&lt;/h3&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;List comprehension is a concise way to create a new list by performing operations on elements of an existing list. We can use list comprehension to create a new list by excluding the elements that meet certain conditions. The new list will have all the elements that don&#39;t match the conditions.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For example, if we have a list of numbers and we want to remove all even numbers from the list, we can use the following code:&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Program&lt;/h4&gt;&lt;/div&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;#initilaize the list &lt;/span&gt;
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

&lt;span style=&quot;color: green;&quot;&gt;# create another list using list comprehension&lt;/span&gt;
odd_numbers = [num &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; numbers &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num % 2 != 0]

&lt;span style=&quot;color: green;&quot;&gt;# print the new list&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(odd_numbers)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output&lt;/h4&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;[1, 3, 5, 7, 9]
&lt;/pre&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Method 2: Using a filter&lt;/h3&gt;&lt;div&gt;The filter function is used to filter elements from a list based on a given condition. We can use the filter function to remove elements that meet certain conditions.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For example, if we have a list of numbers and we want to remove all even numbers from the list, we can use the following code:&lt;/div&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Program&lt;/h4&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1
2
3&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_numbers = list(filter(&lt;span style=&quot;color: blue;&quot;&gt;lambda&lt;/span&gt; x: x % 2 != 0, numbers))
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(odd_numbers)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;Output&lt;/h4&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;[1, 3, 5, 7, 9]
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Conclusion&lt;/h3&gt;&lt;div&gt;In this article, we will learn to remove multiple elements in a list using different methods in python.&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/8212157268608665879/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2023/02/remove-multiple-elements-from-list-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/8212157268608665879'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/8212157268608665879'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2023/02/remove-multiple-elements-from-list-in-python.html' title='Remove multiple elements from a list in Python'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-5085789671846622661</id><published>2022-07-17T17:00:00.017+05:30</published><updated>2022-07-17T17:00:00.195+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print all negative numbers in a range</title><content type='html'>&lt;p&gt;In this article, we will learn to create a program to print all negative numbers in a given range using python programming.&lt;/p&gt;&lt;p&gt;We do this program using 2 different methods:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Using loop&lt;/li&gt;&lt;li&gt;Using list comprehension&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;b&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Input&lt;/b&gt;:&amp;nbsp;&lt;/div&gt;&lt;div&gt;Starting number is: -4&lt;/div&gt;&lt;div&gt;Ending number is: 4&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Output&lt;/b&gt;:&lt;/div&gt;&lt;div&gt;[-4, -3, -2, -1]&lt;/div&gt;&lt;h2&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;For loop&lt;/h2&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;First, we take the input from the user of starting and ending numbers.&lt;/li&gt;&lt;li&gt;Then, we initiate the result list.&lt;/li&gt;&lt;li&gt;After that, iterate all numbers from the given range,&lt;/li&gt;&lt;li&gt;And check if the number is less than 0.&lt;/li&gt;&lt;li&gt;If it satisfied the above condition and appends those elements to result list.&lt;/li&gt;&lt;li&gt;And lastly, print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# take input from the user&lt;/span&gt;
start = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter starting number: &quot;&lt;/span&gt;))
end = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter ending number: &quot;&lt;/span&gt;))

&lt;span style=&quot;color: green;&quot;&gt;# result&lt;/span&gt;
result = []

&lt;span style=&quot;color: green;&quot;&gt;# iterate all elements from the given range&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; range(start, end+1):
    &lt;span style=&quot;color: green;&quot;&gt;# check positive number&lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; ele&amp;lt;0:
        &lt;span style=&quot;color: green;&quot;&gt;# appends elements to result&lt;/span&gt;
        result.append(ele)
        
&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;Enter starting number: -4
Enter ending number: 10
[-4, -3, -2, -1]
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h2&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;List Comprehension&lt;/h2&gt;&lt;div&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;First, we take the input from the user of starting and ending numbers.&lt;/li&gt;&lt;li&gt;Using list comprehension, check the elements is less than 0.&lt;/li&gt;&lt;li&gt;print the result&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;1
2
3
4
5
6
7
8
9&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# take input from the user&lt;/span&gt;
start = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter starting number: &quot;&lt;/span&gt;))
end = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter ending number: &quot;&lt;/span&gt;))

&lt;span style=&quot;color: green;&quot;&gt;# result&lt;/span&gt;
result = [num &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; range(start, end+1) &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num&amp;lt;0]
        
&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;Enter starting number: -4
Enter ending number: 10
&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;[-4, -3, -2, -1]&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/5085789671846622661/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-negative-in-range.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/5085789671846622661'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/5085789671846622661'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-negative-in-range.html' title='Python program to print all negative numbers in a range'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-1431460941981699733</id><published>2022-07-16T17:00:00.065+05:30</published><updated>2022-07-16T17:00:00.194+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print all positive numbers in a range</title><content type='html'>&lt;p&gt;In this article, we will learn to create a program to print all positive numbers in a given range using python programming.&lt;/p&gt;&lt;p&gt;We do this program using 2 different methods:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Using loop&lt;/li&gt;&lt;li&gt;Using list comprehension&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;b&gt;&lt;span&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/span&gt;Input&lt;/b&gt;:&amp;nbsp;&lt;/div&gt;&lt;div&gt;Starting number is: -2&lt;/div&gt;&lt;div&gt;Ending number is: 10&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Output&lt;/b&gt;:&lt;/div&gt;&lt;div&gt;[0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;For loop&lt;/h2&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we take the input from the user of starting and ending numbers.&lt;/li&gt;&lt;li&gt;Then, we initiate the result list.&lt;/li&gt;&lt;li&gt;After that, iterate all numbers from the given range,&lt;/li&gt;&lt;li&gt;And check if the number is greater than or equal to 0.&lt;/li&gt;&lt;li&gt;If it satisfied the above condition and appends those elements to result list.&lt;/li&gt;&lt;li&gt;And lastly, print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Program&lt;/h3&gt;&lt;div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# take input from the user&lt;/span&gt;
start = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter starting number: &quot;&lt;/span&gt;))
end = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter ending number: &quot;&lt;/span&gt;))

&lt;span style=&quot;color: green;&quot;&gt;# result&lt;/span&gt;
result = []

&lt;span style=&quot;color: green;&quot;&gt;# iterate all elements from the given range&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; range(start, end+1):
    &lt;span style=&quot;color: green;&quot;&gt;# check positive number&lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; ele&amp;gt;=0:
        &lt;span style=&quot;color: green;&quot;&gt;# appends elements to result&lt;/span&gt;
        result.append(ele)
        
&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;







&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Output&lt;/h3&gt;&lt;div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;Enter starting number: -4
Enter ending number: 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
&lt;/pre&gt;&lt;/div&gt;




&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h2&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;List Comprehension&lt;/h2&gt;&lt;div&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;First, we take the input from the user of starting and ending numbers.&lt;/li&gt;&lt;li&gt;Using list comprehension, check the elements is greater than or equal to 0.&lt;/li&gt;&lt;li&gt;print the result&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;
  
  
  
  &lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1
2
3
4
5
6
7
8
9&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# take input from the user&lt;/span&gt;
start = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter starting number: &quot;&lt;/span&gt;))
end = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter ending number: &quot;&lt;/span&gt;))

&lt;span style=&quot;color: green;&quot;&gt;# result&lt;/span&gt;
result = [num &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; range(start, end+1) &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num &amp;gt;=0]
        
&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

  
  
  
  &lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;
  
  
  
  
  &lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;Enter starting number: -4
Enter ending number: 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
&lt;/pre&gt;&lt;/div&gt;

  
  &lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/1431460941981699733/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-positive-in-range.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1431460941981699733'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1431460941981699733'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-positive-in-range.html' title='Python program to print all positive numbers in a range'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-3775355392903223200</id><published>2022-07-15T17:00:00.011+05:30</published><updated>2022-07-15T17:00:00.195+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print negative numbers in a list</title><content type='html'>&lt;p&gt;&amp;nbsp;In this article, we will learn to create a python program to print negative numbers in a list.&lt;/p&gt;&lt;p&gt;Print negative numbers in a list using 2 different methods:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Using loop&lt;/li&gt;&lt;li&gt;Using list comprehension&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;&lt;span&gt;&lt;hr /&gt;&lt;/span&gt;Loop&lt;/h2&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;Initialize the list.&lt;/li&gt;&lt;li&gt;Then, iterate all numbers from the list and check if the number is lesser than 0 or not.&lt;/li&gt;&lt;li&gt;If the number is lesser than 0, then append those numbers to a new list named result.&lt;/li&gt;&lt;li&gt;Then, lastly, print results.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# list contains all numbers&lt;/span&gt;
list = [1, 5, 22, -5, 55, -21]

&lt;span style=&quot;color: green;&quot;&gt;# result &lt;/span&gt;
result = []

&lt;span style=&quot;color: green;&quot;&gt;# iterate all numbers &lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list:
    &lt;span style=&quot;color: green;&quot;&gt;# check for a negative number&lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num &amp;lt; 0:
       result.append(num)  &lt;span style=&quot;color: green;&quot;&gt;#append all number to result&lt;/span&gt;

&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;[-5, -21]
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h2&gt;&lt;span&gt;&lt;hr /&gt;&lt;/span&gt;List comprehension&amp;nbsp;&lt;/h2&gt;&lt;div&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;Initialize the list.&lt;/li&gt;&lt;li&gt;Then using list comprehension, store all the negative numbers in a result.&lt;/li&gt;&lt;li&gt;Then print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# list contains all numbers&lt;/span&gt;
list = [1, 5, 22, -5, 55, -21]

&lt;span style=&quot;color: green;&quot;&gt;# list comprehension &lt;/span&gt;
result = [num &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num&amp;lt;0]

&lt;span style=&quot;color: green;&quot;&gt;# print rsult&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;[-5, -21]&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/3775355392903223200/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-negative.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3775355392903223200'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/3775355392903223200'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-negative.html' title='Python program to print negative numbers in a list'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-1587976701196435110</id><published>2022-07-14T17:00:00.000+05:30</published><updated>2022-07-14T17:00:00.214+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print positive numbers in a list</title><content type='html'>&lt;p&gt;In this article, we will learn to create a python program to print positive numbers in a list.&lt;/p&gt;&lt;p&gt;Print positive numbers in a list using 2 different methods:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Using loop&lt;/li&gt;&lt;li&gt;Using list comprehension&lt;/li&gt;&lt;/ol&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Loop&lt;/h2&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Initialize the list.&lt;/li&gt;&lt;li&gt;Then, iterate all numbers from the list and check if the number is greater or equal to 0 or not.&lt;/li&gt;&lt;li&gt;If the number is greater or equal to 0, then append those numbers to a new list named result.&lt;/li&gt;&lt;li&gt;Then, lastly, print results.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Program&lt;/h3&gt;&lt;div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# list contains all numbers&lt;/span&gt;
list = [1, 5, 22, -5, 55, -21]

&lt;span style=&quot;color: green;&quot;&gt;# result &lt;/span&gt;
result = []

&lt;span style=&quot;color: green;&quot;&gt;# iterate all numbers &lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list:
    &lt;span style=&quot;color: green;&quot;&gt;# check for a positive number&lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num &amp;gt;= 0:
       result.append(num)  &lt;span style=&quot;color: green;&quot;&gt;#append all number to result&lt;/span&gt;

&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;




&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Output&lt;/h3&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;[1, 5, 22, 55]
&lt;/pre&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h2&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;List comprehension&amp;nbsp;&lt;/h2&gt;&lt;div&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Initialize the list.&lt;/li&gt;&lt;li&gt;Then using list comprehension, store all the positive numbers in a result.&lt;/li&gt;&lt;li&gt;Then print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;
  &lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# list contains all numbers&lt;/span&gt;
list = [1, 5, 22, -5, 55, -21]

&lt;span style=&quot;color: green;&quot;&gt;# list comprehension &lt;/span&gt;
result = [num &lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; num&amp;gt;=0]

&lt;span style=&quot;color: green;&quot;&gt;# print rsult&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

  
  &lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;
  &lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;[1, 5, 22, 55]&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/1587976701196435110/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-positive-number.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1587976701196435110'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1587976701196435110'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-positive-number.html' title='Python program to print positive numbers in a list'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-5963528076463129273</id><published>2022-07-13T17:00:00.000+05:30</published><updated>2022-07-13T17:00:00.199+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print all odd numbers in a range</title><content type='html'>&lt;p&gt;In this article, we will learn to print all the odd numbers in a range using the python program.&lt;/p&gt;&lt;p&gt;We will use for loop to do that.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Input:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Start = 4&lt;/p&gt;&lt;p&gt;End = 10&lt;/p&gt;&lt;p&gt;&lt;b&gt;Output:&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;[5, 7, 8]&lt;/p&gt;&lt;h2&gt;&lt;span&gt;&lt;hr /&gt;&lt;/span&gt;Explanation&lt;/h2&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;First, we will take input from users for starting and ending numbers.&lt;/li&gt;&lt;li&gt;Then we will iterate for loop from start to end+1.&lt;/li&gt;&lt;li&gt;After that, we will append all the odd numbers to result in a list.&lt;/li&gt;&lt;li&gt;Then we print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2&gt;&lt;span&gt;&lt;hr /&gt;&lt;/span&gt;Program&lt;/h2&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# take input from the user&lt;/span&gt;
Start = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter starting number: &quot;&lt;/span&gt;))
End = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter ending number: &quot;&lt;/span&gt;))

result = []

&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; range(Start,End+1):
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; ele%2!=0:
       result.append(ele)
       
&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
    
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2&gt;&lt;span&gt;&lt;hr /&gt;&lt;/span&gt;Output&lt;/h2&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;Enter starting number: 4
Enter ending number: 10
[5, 7, 9]&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/5963528076463129273/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-odd-numbers-.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/5963528076463129273'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/5963528076463129273'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-odd-numbers-.html' title='Python program to print all odd numbers in a range'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-1674752860338940466</id><published>2022-07-12T17:00:00.009+05:30</published><updated>2022-07-12T17:00:00.205+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print all even numbers in a range</title><content type='html'>&lt;p&gt;In this article, we will learn to print all the even numbers in a range using the python program.&lt;/p&gt;&lt;p&gt;We will use for loop to do that.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Input:&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Start = 4&lt;/p&gt;&lt;p&gt;End = 10&lt;/p&gt;&lt;p&gt;&lt;b&gt;Output:&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;[4, 6, 8, 10]&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Explanation&lt;/h2&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we will take input from users for starting and ending numbers.&lt;/li&gt;&lt;li&gt;Then we will iterate for loop from start to end+1.&lt;/li&gt;&lt;li&gt;After that, we will append all the even numbers to result in a list.&lt;/li&gt;&lt;li&gt;Then we print the result.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Program&lt;/h2&gt;&lt;div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# take input from the user&lt;/span&gt;
Start = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter starting number: &quot;&lt;/span&gt;))
End = int(input(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Enter ending number: &quot;&lt;/span&gt;))

result = []

&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; range(Start,End+1):
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; ele%2==0:
       result.append(ele)
       
&lt;span style=&quot;color: green;&quot;&gt;# print result&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
    
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;




&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Output&lt;/h2&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;Enter starting number: 4
Enter ending number: 10
[4, 6, 8, 10]
&lt;/pre&gt;&lt;/div&gt;


&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/1674752860338940466/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-even.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1674752860338940466'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/1674752860338940466'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-all-even.html' title='Python program to print all even numbers in a range'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-6199606390723917385</id><published>2022-07-11T17:00:00.002+05:30</published><updated>2022-07-11T17:00:00.198+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print odd numbers in a List</title><content type='html'>&lt;p&gt;In this article, we will learn to find the odd number from the given list and print it in the list format.&lt;/p&gt;&lt;h3&gt;How to check whether the given is odd or not?&lt;/h3&gt;&lt;p&gt;If a number is not completely divisible by 2, then the given number is odd.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr&gt;We find the odd number in a list using 1 method:&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Using loop&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;b&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Input&amp;nbsp;&lt;/b&gt;: [1,2,3,4,5,6]&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Output&lt;/b&gt;&amp;nbsp;: [1,3,5]&lt;/div&gt;&lt;h2&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Using loop&lt;/h2&gt;&lt;h3&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;First, we initialize a list.&lt;/li&gt;&lt;li&gt;Then we create a new list to store the odds number (named result).&lt;/li&gt;&lt;li&gt;After, that we will run the loop to get all elements of the list one by one.&lt;/li&gt;&lt;li&gt;Then, we will check each element, whether it&#39;s completely divisible by the 2 or not.&lt;/li&gt;&lt;li&gt;We will check using the modulus(%) operator.&lt;/li&gt;&lt;li&gt;If the element is not completely divisible by 2, then append to the new list result.&lt;/li&gt;&lt;li&gt;And after all, print the result list.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3&gt;Program&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# list&lt;/span&gt;
list = [1,2,3,4,5,6,7,8]

&lt;span style=&quot;color: green;&quot;&gt;# result list&lt;/span&gt;
result = []

&lt;span style=&quot;color: green;&quot;&gt;# iterate all element of list &lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list:
    &lt;span style=&quot;color: green;&quot;&gt;# check for odd &lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; ele%2!=0:
        result.append(ele) &lt;span style=&quot;color: green;&quot;&gt;# append the odd number in result list&lt;/span&gt;

&lt;span style=&quot;color: green;&quot;&gt;# print the result &lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3&gt;Output&lt;/h3&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;[1, 3, 5, 7]
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;hr&gt;&lt;/span&gt;Conclusion&lt;/h2&gt;&lt;div&gt;In this article, we have learned how to create a python program that prints odd numbers in a list using simple methods with the help of a step-by-step explanation.&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/6199606390723917385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-odd-numbers-in-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/6199606390723917385'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/6199606390723917385'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/07/python-program-to-print-odd-numbers-in-list.html' title='Python program to print odd numbers in a List'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8253077814651020859.post-8437345186520445841</id><published>2022-07-10T17:00:00.002+05:30</published><updated>2022-07-11T13:51:02.608+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="list"/><category scheme="http://www.blogger.com/atom/ns#" term="python example"/><title type='text'>Python program to print even numbers in a list</title><content type='html'>&lt;p&gt;In this article, we will learn to find the even number from the given list and print it in the list format.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;How to check whether the given is even or not?&lt;/h3&gt;&lt;p&gt;If a number is completely divisible by 2, then the given number is even.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;We find the even number in a list using 1 method:&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Using loop&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;b&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Input&amp;nbsp;&lt;/b&gt;: [1,2,3,4,5,6]&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Output&lt;/b&gt; : [2,4,6]&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Using loop&lt;/h2&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Explanation&lt;/h3&gt;&lt;div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;First, we initialize a list.&lt;/li&gt;&lt;li&gt;Then we create a new list to store the evens number (named result).&lt;/li&gt;&lt;li&gt;After, that we will run the loop to get all elements of the list one by one.&lt;/li&gt;&lt;li&gt;Then, we will check each element, whether it&#39;s completely divisible by the 2 or not.&lt;/li&gt;&lt;li&gt;We will check using the modulus(%) operator.&lt;/li&gt;&lt;li&gt;If the element is completely divisible by 2, then append to the new list result.&lt;/li&gt;&lt;li&gt;And after all, print the result list.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Program&lt;/h3&gt;&lt;div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: green; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;# list&lt;/span&gt;
list = [1,2,3,4,5,6,7,8]

&lt;span style=&quot;color: green;&quot;&gt;# result list&lt;/span&gt;
result = []

&lt;span style=&quot;color: green;&quot;&gt;# iterate all element of list &lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;for&lt;/span&gt; ele &lt;span style=&quot;color: blue;&quot;&gt;in&lt;/span&gt; list:
    &lt;span style=&quot;color: green;&quot;&gt;# check for even &lt;/span&gt;
    &lt;span style=&quot;color: blue;&quot;&gt;if&lt;/span&gt; ele%2==0:
        result.append(ele) &lt;span style=&quot;color: green;&quot;&gt;# append the even number in result list&lt;/span&gt;

&lt;span style=&quot;color: green;&quot;&gt;# print the result &lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;print&lt;/span&gt;(result)
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;



&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Output&lt;/h3&gt;&lt;div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid green; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;[2, 4, 6, 8]
&lt;/pre&gt;&lt;/div&gt;


&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;!--more--&gt;&lt;/span&gt;Conclusion&lt;/h2&gt;&lt;div&gt;In this article, we have learned how to create a python program that prints even numbers in a list using simple methods with the help of a step-by-step explanation.&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;If you have a better solution or query, please share in comment or Email.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://www.docodehere.com/feeds/8437345186520445841/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.docodehere.com/2022/06/python-program-to-print-even-numbers-in-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/8437345186520445841'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8253077814651020859/posts/default/8437345186520445841'/><link rel='alternate' type='text/html' href='https://www.docodehere.com/2022/06/python-program-to-print-even-numbers-in-list.html' title='Python program to print even numbers in a list'/><author><name>Ankur Ranpariya</name><uri>http://www.blogger.com/profile/16441671548702286256</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH9gyOesbDztLaGJJdj8tdFQIrlXCVv88RBMdJ-QpOcrurVqo9wB45hwj2U-1UX6D5un3jb4GqLtIpbAelyOPgywayARkkuERJ4ZzylYy4VQfDO6ZRVyltTH818AbEThI/s150/IMG-20180503-WA0027-1.jpg'/></author><thr:total>0</thr:total></entry></feed>