<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sakshi J</title>
    <description>The latest articles on Forem by Sakshi J (@jainsakshi).</description>
    <link>https://forem.com/jainsakshi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F703356%2F05d017cb-493b-4294-92e8-ab025bc61d82.jpeg</url>
      <title>Forem: Sakshi J</title>
      <link>https://forem.com/jainsakshi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jainsakshi"/>
    <language>en</language>
    <item>
      <title>LLMs - Embeddings 01</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Mon, 04 Aug 2025 06:18:08 +0000</pubDate>
      <link>https://forem.com/jainsakshi/llms-embeddings-01-17ba</link>
      <guid>https://forem.com/jainsakshi/llms-embeddings-01-17ba</guid>
      <description>&lt;h2&gt;
  
  
  Word Embeddings
&lt;/h2&gt;

&lt;p&gt;An assignment of words to numbers is called a word embedding.&lt;/p&gt;

&lt;p&gt;Consider an example, suppose we have a 2D plane and at multiple coordinates we have some labels like apple, banana, mango are near to each others coordinates, similarly carrot, radish and potato are near each other, and laptop, mobile, TV are far from the vegetables coordinate and fruit coordinate also. Now if we have to place "orange" then think around which coordinates it should be place?&lt;/p&gt;

&lt;p&gt;It should be placed with fruits, near banana, mango and apple.&lt;/p&gt;

&lt;p&gt;Words that are similar should correspond to points that are close by.&lt;/p&gt;

&lt;p&gt;Words that are different should correspond to points that are far away.&lt;/p&gt;

&lt;p&gt;There is something more to these word embeddings, and it is that they don’t only capture word similarity, but they also capture other properties of the language.&lt;/p&gt;

&lt;p&gt;There is something more to these word embeddings, and it is that they don’t only capture word similarity, but they also capture other properties of the language.&lt;/p&gt;

&lt;p&gt;Since each feature is one new axis, or coordinate, then a good embedding must have many more than two coordinates assigned to every word.&lt;/p&gt;

&lt;p&gt;One of the XYZ embeddings, for example, has 5024 coordinates associated with each word. These rows of 5024 (or however many) coordinates are called vectors, so we often talk about the vector corresponding to a word, and to each of the numbers inside a vector as a coordinate. &lt;/p&gt;

&lt;p&gt;understanding vector as an array of coordinates like - [ [3,4] [5,8] [7,9] [12,24] ]&lt;/p&gt;

&lt;p&gt;Some of these coordinates may represent important properties of the word, such as age, gender, size. Some may represent combinations of properties. But some others may represent obscure properties that a human may not be able to understand. But all in all, a word embedding can be seen as a good way to translate human language (words) into computer language (numbers), so that we can start training machine learning models with these numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sentence Embeddings
&lt;/h2&gt;

&lt;p&gt;A sentence embedding is just like a word embedding, except it associates every sentence with a vector full of numbers satisfying similar properties as a word embedding.&lt;/p&gt;

&lt;p&gt;For instance, similar sentences are assigned to similar vectors, different sentences are assigned to different vectors, and most importantly, each of the coordinates of the vector identifies some (whether clear or obscure) property of the sentence.&lt;/p&gt;

&lt;p&gt;Head over to &lt;a href="https://dashboard.cohere.com/playground/embed" rel="noopener noreferrer"&gt;Cohere Playgroud&lt;/a&gt; and try providing inputs, some sentences similar, some different and hit the RUN button.&lt;/p&gt;

&lt;h2&gt;
  
  
  There are problems associated with this solution, let us look what are those
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Similarity Between Sentences
&lt;/h3&gt;

&lt;p&gt;For large language models, it is crucial to know when two words, or two sentences, are similar or different.&lt;/p&gt;

&lt;p&gt;We can measure similarity and dissimilarity between multiple things like movies/car models/ countries anything.&lt;/p&gt;

&lt;p&gt;But for that, you need to brush up your engineering math.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxc8zxlrt5glrfmxrrrc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxc8zxlrt5glrfmxrrrc.gif" alt=" " width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I literally waited for the day when I would finally get rid of math, all my school and college life, but it seems I will never get rid of this subject. So here is a (link)[&lt;a href="https://www.datacamp.com/tutorial/demystifying-mathematics-concepts-deep-learning" rel="noopener noreferrer"&gt;https://www.datacamp.com/tutorial/demystifying-mathematics-concepts-deep-learning&lt;/a&gt;] to study math concepts used in DL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6os5iza9pvssdh12a9pk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6os5iza9pvssdh12a9pk.gif" alt=" " width="498" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PLUS, check this &lt;a href="https://www.datacamp.com/tutorial/cosine-distance" rel="noopener noreferrer"&gt;link&lt;/a&gt; also, for learning cosine similarity and dissimilarity.&lt;/p&gt;

&lt;p&gt;word embedding is an assignment of a list of numbers (vector) to every word, in a way that semantic properties of the word translate into mathematical properties of the numbers. What do we mean by this? For example, two similar words will have similar vectors, and two different words will have different vectors. But most importantly, each entry in the vector corresponding to a word keeps track of some property of the word. Some of these properties can be understandable to humans, such as age, size, gender, etc., but some others could potentially only be understood by the computer. Either way, we can benefit from these embeddings for many useful tasks.&lt;/p&gt;

&lt;p&gt;Sentence embeddings are even more powerful, as they assign a vector of numbers to each sentence, in a way that these numbers also carry important properties of the sentence.&lt;/p&gt;

&lt;p&gt;In this way, the sentence “Hello, how are you?” and its corresponding French translation, “Bonjour, comment ça va?” will be assigned very similar numbers, as they have the same semantic meaning.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 1 - DOT PRODUCT
&lt;/h3&gt;

&lt;p&gt;Heard this before? MMMMM...MATHS High school maths&lt;/p&gt;

&lt;p&gt;THANK ME LATER&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The dot product, also known as the scalar product or inner product, is a mathematical operation that takes two equal-length sequences of numbers (usually vectors) and returns a single number&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://datahacker.rs/dot-product-inner-product/" rel="noopener noreferrer"&gt;FULL TUTORIAL for those who did not study this concept well in school&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to decide how similar or dissimilar is one movie with others??&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwalo5zob3a7m1wc670y5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwalo5zob3a7m1wc670y5.png" alt=" " width="608" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that if two movies are similar, then they must have similar action scores and similar comedy scores. So if we multiply the two action scores, then multiply the two comedy scores, and add them, this number would be high if the scores match. &lt;/p&gt;

&lt;p&gt;**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dot product for the pair [You’ve got mail, Taken] = 0*7 + 5*0 = 0
Dot product for the pair [Rush Hour, Rush Hour 2] = 6*7 + 5*4 = 62
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;h3&gt;
  
  
  ANOTHER MEASURE : COSINE SIMILARITY 😈
&lt;/h3&gt;

&lt;p&gt;Another measure of similarity between sentences (and words) is to look at the angle between them. For example, let’s plot the movie embedding in the plane, where the horizontal axis represents the action score, and the vertical axis represents the comedy score. The embedding looks like this.&lt;/p&gt;

&lt;p&gt;Check above shared links for cosine similarity and dissimilarity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9enh3noxser54pvi5np.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9enh3noxser54pvi5np.png" alt=" " width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we want a measure of similarity that is high for sentences that are close to each other, and low for sentences that are far away from each other. Distance does the exact opposite. &lt;/p&gt;

&lt;p&gt;Let’s look at the angle between the rays from the origin (the point with coordinates [0,0]), and each sentence. Notice that this angle is small if the points are close to each other, and large if the points are far away from each other. Now we need the help of another function, the cosine. The cosine of angles close to zero is close to 1, and as the angle grows, the cosine decreases. This is exactly what we need. Therefore, we define the cosine distance as the cosine of the angle formed by the two rays going from the origin, to the two sentences.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdwkx4wajwn9voakcgwmg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdwkx4wajwn9voakcgwmg.png" alt=" " width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cosine distance measures the dissimilarity between vectors by calculating the cosine of the angle between them, cosine similarity quantifies how similar two vectors are based on the cosine of the same angle. &lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
Thanks for reading till here...posting second blog soon in this series 🙂&lt;/p&gt;

</description>
      <category>llm</category>
      <category>genai</category>
      <category>learning</category>
      <category>ai</category>
    </item>
    <item>
      <title>AI/ML Scholarship Registration OPEN</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Sun, 28 Jul 2024 13:22:15 +0000</pubDate>
      <link>https://forem.com/jainsakshi/aiml-scholarship-registration-open-4igb</link>
      <guid>https://forem.com/jainsakshi/aiml-scholarship-registration-open-4igb</guid>
      <description>&lt;p&gt;Hello college students!&lt;/p&gt;

&lt;p&gt;If you are 16 years or older and enrolled in a college, here is a chance for you to get a seat in the AI/ML scholarship program by AWS.&lt;/p&gt;

&lt;p&gt;Please follow &lt;a href="https://www.udacity.com/scholarships/aws-ai-ml-scholarship-program" rel="noopener noreferrer"&gt;this&lt;/a&gt; link to proceed with registration.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>techtalks</category>
    </item>
    <item>
      <title>Rewards</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Fri, 26 Apr 2024 02:23:34 +0000</pubDate>
      <link>https://forem.com/jainsakshi/job-satisfaction-or-salary-2e51</link>
      <guid>https://forem.com/jainsakshi/job-satisfaction-or-salary-2e51</guid>
      <description>&lt;p&gt;I am curious to know that, what you people like, work satisfaction or salary or anything else.&lt;/p&gt;

&lt;p&gt;What are your end goals and expectations from your job?&lt;/p&gt;

&lt;p&gt;Please let me know in comments...&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>developer</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Dictionary📚 - Python</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Sun, 07 Apr 2024 21:00:33 +0000</pubDate>
      <link>https://forem.com/jainsakshi/dictionary-python-13me</link>
      <guid>https://forem.com/jainsakshi/dictionary-python-13me</guid>
      <description>&lt;h2&gt;
  
  
  Python Unleash'd 12
&lt;/h2&gt;

&lt;p&gt;Hi &lt;/p&gt;

&lt;p&gt;Starting with new data structure today.&lt;br&gt;
We are done with sets.&lt;/p&gt;

&lt;p&gt;Maps in C++ never annoyed me, so helpful data structure, I expect same from dictionary.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is dictionary in python?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Stores values in key value pair.&lt;/li&gt;
&lt;li&gt;curly brackets are used&lt;/li&gt;
&lt;li&gt;Keys are immutable, while values are duplicate and mutable&lt;/li&gt;
&lt;li&gt;Values can be of any data type&lt;/li&gt;
&lt;li&gt;Key names are case sensitive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example from GeeksForGeeks too see how to create dictionary&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Other way to create dictionary is by using
&lt;/h3&gt;

&lt;p&gt;dict function&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb8txibb7njztwvtac4mq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb8txibb7njztwvtac4mq.png" alt=" " width="456" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check length of dictionary using len()&lt;/p&gt;

&lt;p&gt;`len(Dict)1 returns length of dictionary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp7gi0g9afcmewozqx67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp7gi0g9afcmewozqx67.png" alt=" " width="462" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to add elements to dictionary
&lt;/h3&gt;

&lt;p&gt;As we do in C++, we can access dictionary values with keys, see&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb76hfcrpo1k6qn8tmct1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb76hfcrpo1k6qn8tmct1.png" alt=" " width="286" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While adding a value, if the key-value already exists, the value gets updated otherwise a new Key with the value is added to the Dictionary.&lt;/p&gt;

&lt;p&gt;See how dictionary gets updated&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazwfcid7q7w4e493cvw5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazwfcid7q7w4e493cvw5.png" alt=" " width="370" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is another way to access items.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dictionary2.get(3)&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing nested dictionary items
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;dictionary2 = {0 : 'zero', 1:{1:"one", 2:"two"}}&lt;/code&gt;&lt;br&gt;
&lt;code&gt;print(dictionary2[1][2])&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deletion
&lt;/h2&gt;

&lt;p&gt;Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
print("Dictionary =")&lt;br&gt;
print(Dict)&lt;br&gt;
del(Dict[1]) &lt;br&gt;
print("Data after deletion Dictionary=")&lt;br&gt;
print(Dict)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading &lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Settling with Sets 2.0 - Python</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Sun, 07 Apr 2024 11:30:38 +0000</pubDate>
      <link>https://forem.com/jainsakshi/settling-with-sets-20-python-2gd7</link>
      <guid>https://forem.com/jainsakshi/settling-with-sets-20-python-2gd7</guid>
      <description>&lt;h3&gt;
  
  
  Python Unleash'd 11
&lt;/h3&gt;

&lt;p&gt;Consider reading &lt;a&gt;this&lt;/a&gt; first&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Access elements
&lt;/h2&gt;

&lt;p&gt;You can not access elements using index, so the only way is to loop through the elements, takes O(n) time.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Removing Elements
&lt;/h2&gt;

&lt;p&gt;We have a 'remove' method to serve this purpose. It throws error if element is not in the set. To use remove(), first check if element is present. It take O(n) time to check element's presense.&lt;/p&gt;

&lt;p&gt;Better to use 'discard()', as it does not throw any error even if element is not present.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff062xfd3855lw6mff69u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff062xfd3855lw6mff69u.png" alt=" " width="697" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Pop
&lt;/h2&gt;

&lt;p&gt;Pop() function can also be used to remove and return an element from the set, but it removes only the last element of the set. &lt;/p&gt;

&lt;p&gt;As set is unordered there is no way to tell which element is removed. :D&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn534luun8504goh1o0yk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn534luun8504goh1o0yk.gif" alt=" " width="220" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. clear()
&lt;/h2&gt;

&lt;p&gt;To remove all elements from set, use clear() method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyej3ehdo69s5ffjpwlb9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyej3ehdo69s5ffjpwlb9.png" alt=" " width="227" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  DO YOU KNOW
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;There is something 'frozenset' which makes a set immutable and then only those methods are applied which does not affect elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All mathematical operations are valid on these sets : union, intersection, difference, issubset, issuperset  etc&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It does not support append()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consumer more memory than lists&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;THANKS FOR READING GUYS &lt;/p&gt;

&lt;p&gt;keep reading :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frppg9nh9ny4yueb0hsqq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frppg9nh9ny4yueb0hsqq.gif" alt=" " width="165" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>computerscience</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Settling with SETS - Python</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Sat, 06 Apr 2024 22:13:53 +0000</pubDate>
      <link>https://forem.com/jainsakshi/settling-on-sets-python-1b3g</link>
      <guid>https://forem.com/jainsakshi/settling-on-sets-python-1b3g</guid>
      <description>&lt;p&gt;Hello geeks!&lt;/p&gt;

&lt;p&gt;After tuple and lists, lets see sets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python Unleashed 09
&lt;/h2&gt;

&lt;h2&gt;
  
  
  How to create set
&lt;/h2&gt;

&lt;p&gt;There are two ways to define a set in Python. You can use the set() function, which takes in a iterable and converts the iterable to a set.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7la6dbyrjyf2djwcfb6v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7la6dbyrjyf2djwcfb6v.jpg" alt=" " width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Showing you the first way of creating set, when you have elements ready with you&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set = {"this", "is", "a", "set",12,True}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvqq5ddduefwvxn98fw4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkvqq5ddduefwvxn98fw4.png" alt=" " width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See what error we get here. And what's unhashable type.&lt;/p&gt;

&lt;p&gt;To get better understanding of how set is related to hash, lets see important properties of set.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Unordered -&amp;gt; you will not see items in order, unchangeable order (Ascending/descending/sorted)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No duplicate elements -&amp;gt; calculate length of set with two duplicate values, it will return the length as one. Hence we can prove the unique element only rule for set.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set = {1,2,1}
print(len(set))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  FOCUS HERE
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Set is mutable object, which contains only immutable elements &lt;br&gt;
List, Set, Dictionaries are mutable.&lt;br&gt;
Tuple, String, Integer are immutable.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A set cannot have mutable elements like a list or dictionary, as it is immutable&lt;/strong&gt;. (as seen in above snapshot, we get error on adding list to set)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A set itself may be modified, but the elements contained in the set must be of an immutable type - adding more clarity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set does not support indexing and slicing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Hashable
&lt;/h3&gt;

&lt;p&gt;Hashable means hash is associated with each element in set. &lt;br&gt;
For now, consider hash as combination of random alphabets, numbers or hexadecimal number. So any value can be converted to hash by making it to go through a process, that process is called as hash function. When a text/value is passed to hash function, it produces hash of that. If two values have same hash they are considered same. This method used for calculating hash can not be used to get original text again, only hash values are matched. We have so many hashing algorithms also. Hope you got good idea of what hashable means. If not, feel free to google.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Python sets can only include hashable objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means that they can include immutable objects because all immutable objects are hashable and they can include mutable objects that are hashable. So, some examples that you’ve probably seen of immutable objects are tuples, strings, integers, and Booleans.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because these objects cannot be modified after they were created.&lt;/p&gt;

&lt;p&gt;You can also imagine if you can call hash() on your object and it doesn’t error, then it’s hashable. Lists and dictionaries are unhashable because we cannot call the hash() method on them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Immutable vs Hashable
&lt;/h2&gt;

&lt;p&gt;Immutable objects are a type of object that cannot be modified after they were created. Hashable objects, on the other hand, are a type of object that you can call hash() on.&lt;/p&gt;

&lt;p&gt;So if you go into the Python interpreter and type hash, open parenthesis, and then put your object in there, close , and hit Enter and it does not error, then that means that your object is hashable.&lt;/p&gt;

&lt;p&gt;All immutable objects are hashable, but not all hashable objects are immutable. That’s just a rule, so anytime you see some example immutable object, you know for a fact that it’s hashable, but there are some cases where there are hashable objects that you actually can mutate. Python sets can only include hashable objects.&lt;/p&gt;

&lt;p&gt;Let’s look at strings. We can create a string like this, and we can see that strings are indeed immutable. Let’s try to actually mutate it and see that this will error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string = "string type"
print(hash(string))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get hash on running above code.&lt;/p&gt;

&lt;p&gt;Hash remains same for the entire lifetime of object, so this is the reason why immutable objects have hash. Hash is value which is generated once for an object, and then adding more items to it or editing it may affect existing hash value.&lt;/p&gt;

&lt;p&gt;So if we try to add a character to the end or change the first character to another character, both of those will error. That’s because strings are immutable and do not actually have any methods that allow us to mutate them.&lt;/p&gt;

&lt;p&gt;The same goes for integers. We can create an integer, and we can’t add a number to an integer or add an integer to the end of itself and actually change what this 10 is.&lt;/p&gt;

&lt;p&gt;Once the 10 is created, we cannot change it. We can, however, reassign x—but that doesn’t actually change the 10.&lt;/p&gt;

&lt;p&gt;So if we want to add list to set, we would use set() method. &lt;br&gt;
Otherwise, when we know what elements to add we can explicitly create set, as long as elements are immutable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz89qjlrtnketyl9hf46p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz89qjlrtnketyl9hf46p.jpg" alt=" " width="800" height="1574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To iterate or loop over a set, use the same syntax as you would a list or tuple, or any iterable for that matter. But note, you cannot slice or index sets because they’re not ordered, so that doesn’t really make sense to slice some of them or get the zeroth index or something like that. So, here’s an example. We have a set with {'foo', 'bar', 'baz'}.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We try to index. It errors, because it says 'set' is not subscriptable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fog8u2qvfnmsg8gi548ju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fog8u2qvfnmsg8gi548ju.png" alt=" " width="528" height="470"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in set:
    print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set is mutable object, which contains immutable types only.
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create set using curly brackers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;set = { 1,2,3}&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create set using set() method
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;theList = [1, 2, 3, 4, 2, 3, 1]
theSet = set(theList)
print(theSet)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we want to add list (mutable) in existing set?&lt;/p&gt;

&lt;p&gt;there's a way&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;add()&lt;/strong&gt; method&lt;/p&gt;

&lt;p&gt;But one element from the list, at a time&lt;/p&gt;

&lt;h2&gt;
  
  
  add() method
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print('theSet ', theSet)
theList1 = [1,2]
for i in theList1:
    theSet.add(i)
print('theSet now', theSet)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3z4w5mcrmqkxxnhs7qru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3z4w5mcrmqkxxnhs7qru.png" alt=" " width="290" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Update method
&lt;/h2&gt;

&lt;p&gt;For the addition of two or more elements Update() method is used. The update() method accepts lists, strings, tuples as well as other sets as its arguments. &lt;strong&gt;In all of these cases, duplicate elements are avoided.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;See how duplicate elements are avoided&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set1 = set([4, 5, (6, 7)]) 
set1.update([10, 11]) 
set1.update({2,3},"n")
set1.update("hehehe","abc")
print("\nSet after Addition of elements using Update: ") 
print(set1) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fslskwylalm59wk4rqua6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fslskwylalm59wk4rqua6.png" alt=" " width="675" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See how one letter is added to avoid duplicacy.&lt;/p&gt;

&lt;p&gt;We'll see accessing, deletion, popping and all other remaining things about SET in next blog to finallt settle with Sets in python.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F127oc0mh9pr3ge7vmbbm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F127oc0mh9pr3ge7vmbbm.png" alt=" " width="540" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This data structure has really annoyed me in past, this is the reason I wanted to settle set concept this time.&lt;/p&gt;

&lt;p&gt;Hope you also got good understanding of what is mutable and what is not and when it is not.&lt;/p&gt;

&lt;p&gt;Thanks for reading this long blog&lt;/p&gt;

&lt;p&gt;good day :)&lt;/p&gt;

</description>
      <category>python</category>
      <category>datastructures</category>
      <category>tutorial</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>TUPLES in 3 minute - Python</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Tue, 02 Apr 2024 00:23:24 +0000</pubDate>
      <link>https://forem.com/jainsakshi/tuples-in-3-minute-python-56en</link>
      <guid>https://forem.com/jainsakshi/tuples-in-3-minute-python-56en</guid>
      <description>&lt;p&gt;Time to brush up next queued data structure. And its &lt;strong&gt;TUPLE&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  PYTHON UNLEASH'D 08
&lt;/h2&gt;

&lt;p&gt;Good day geeks!🤗&lt;/p&gt;

&lt;h2&gt;
  
  
  How to read this blog?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Be ready with editor opened&lt;/li&gt;
&lt;li&gt;Give one and half min to each section i.e, intro and important&lt;/li&gt;
&lt;li&gt;run programs in between&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  INTRO
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Indexing ( from 0 OBV )&lt;/li&gt;
&lt;li&gt;Values separated by comma&lt;/li&gt;
&lt;li&gt;Parenthesis () encloses values [not mandatory, but convention]&lt;/li&gt;
&lt;li&gt;Remember parenthesis &lt;strong&gt;does not only make it tuple&lt;/strong&gt;, I can make a tuple without using single bracket also (Inviting reader's views here in comments ⬇⬇) So there are other ways also besides parenthesis
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;() -&amp;gt; **parenthesis/round brackets**
[] -&amp;gt; **square brackets / big brackets**
{} -&amp;gt; **curly brackets**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;immutable&lt;/li&gt;
&lt;li&gt;heterogenous data&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  IMPORTANT
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8450ac8iqfrde9u4kka3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8450ac8iqfrde9u4kka3.gif" alt=" " width="200" height="65"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Similarities between list and tuple
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Values stored can be indexed&lt;/li&gt;
&lt;li&gt;Values are separated by comma inside&lt;/li&gt;
&lt;li&gt;contain any data type, any number of elements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Unlike lists, tuples are immutable&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating tuple without parenthesis
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jdfbt3c5lkgx5pqh7go.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jdfbt3c5lkgx5pqh7go.png" alt=" " width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, the brackets alone do not make an object into a tuple, but it’s not usual to define a integer like this: a = (2)&lt;/p&gt;

&lt;p&gt;It’s more common to see: a = 2&lt;/p&gt;

&lt;p&gt;It is better to follow what is easier for everyone to understand as long it follows rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = (2)
print(type(a))

a = (1 , 2)
print(type(a))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvr08kf32fhc9arifhsnx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvr08kf32fhc9arifhsnx.png" alt=" " width="332" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other than indexing, we can access tuple items by "unpacking"&lt;br&gt;
See the demo below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tuple1 = ("Geeks", "For", "Geeks")
# make sure number of var must be equal to total tuple elements
# This line unpack
# values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Concatenation of tuple
&lt;/h3&gt;

&lt;p&gt;Say we have two tuples, tup1 and tup2&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tup1 + tup2&lt;/code&gt; is concatenation of both.&lt;/p&gt;

&lt;h3&gt;
  
  
  Slicing
&lt;/h3&gt;

&lt;p&gt;Slicing rules are same as that for string and list. I request you to see below examples and read slicing on strings once from &lt;a href="https://dev.to/bellatrix/understanding-strings-python-67j"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Negative index can be used in slicing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgil4p85aggvpp7feees.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgil4p85aggvpp7feees.png" alt=" " width="769" height="670"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;As tuples are immutable, we can not delete single element from tuple. However we can delete entire tuple at once, with help of &lt;code&gt;del&lt;/code&gt; keyword. Lets see how&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Deleting a Tuple

Tuple1 = (0, 1, 2, 3, 4)
del Tuple1

print(Tuple1)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for tuples.&lt;/p&gt;

&lt;p&gt;There is a long list of methods which are unseen and unused for many of us, will see those once start practising.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fle4nski79i18j89tjp9e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fle4nski79i18j89tjp9e.gif" alt=" " width="220" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have scrolled till here, do react to this post, and read other blogs in the series.&lt;/p&gt;

&lt;p&gt;I am posting one blog everyday, follow for regular new blogs and leave your feedback in comments.&lt;/p&gt;

&lt;p&gt;Thank you all for reading 🙌&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>List Data Structure in 5 mins - Python</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Mon, 01 Apr 2024 19:00:08 +0000</pubDate>
      <link>https://forem.com/jainsakshi/data-structures-list-12e5</link>
      <guid>https://forem.com/jainsakshi/data-structures-list-12e5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi developers/learners/readers&lt;/strong&gt; 🙋‍♀️✋&lt;/p&gt;

&lt;h2&gt;
  
  
  Python Unleashed 07
&lt;/h2&gt;

&lt;p&gt;Going to do comparison between C++ vectors and Python lists in this blog. As I am moving to Python from C++, so this can help others like me. &lt;/p&gt;

&lt;p&gt;To those who are not familiar with C++, you may find few things confusing. &lt;/p&gt;

&lt;h2&gt;
  
  
  INTRO
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;List items are within square brackets, like C++ arrays/vector&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamically sized, can shrink and grow&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;elements are separated by comma as always&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stores data in sequence, in linear fashion&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It can store data of any type, unlike vectors in C++&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Random Access Possible (Negative and Positive Index), Prone to Index out of bound errors, AND ZERO BASED INDEXING&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mutable&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;We do have a list class for lists&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;List is so so much similar to vectors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F486akstuur4ppu9kw1c9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F486akstuur4ppu9kw1c9.png" alt=" " width="740" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;list[-0]&lt;/code&gt; returns 0th elements only&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods on List
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Append&lt;/strong&gt; -&amp;gt; add element to the end of list, one at a time, tuple and lists can be added&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful3f9341mo4a606qw1kp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful3f9341mo4a606qw1kp.png" alt=" " width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Insert&lt;/strong&gt; -&amp;gt; requires two values, element can be added at desired position. One argument is position, second is value. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1siwbu2ge8vta6bzi4r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1siwbu2ge8vta6bzi4r.png" alt=" " width="417" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Extend&lt;/strong&gt; -&amp;gt; add multiple elements to the end of list, elements must be in square bracket. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg3stm2tdxtv4cd4ya9je.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg3stm2tdxtv4cd4ya9je.png" alt=" " width="421" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reverse&lt;/strong&gt; -&amp;gt; reverses the list&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flih0xbbs6k3la907oweh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flih0xbbs6k3la907oweh.png" alt=" " width="423" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reversed&lt;/strong&gt; -&amp;gt; &lt;a href="https://stackoverflow.com/a/31087151" rel="noopener noreferrer"&gt;A good read&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Remove Elements&lt;/strong&gt; -&amp;gt; Removes one element at a time, and element must be there in the list, otherwise throws error, pass element which you would like to delete&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsd74qs58mn2hq28lozh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhsd74qs58mn2hq28lozh.png" alt=" " width="800" height="656"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pop&lt;/strong&gt; -&amp;gt; Either remove element from end only or pass index&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Slicing&lt;/strong&gt; -&amp;gt; Same as that for strings &lt;a href="https://dev.to/bellatrix/understanding-strings-python-67j"&gt;check here strings&lt;/a&gt;&lt;br&gt;
For more information &lt;a href="https://www.geeksforgeeks.org/python-lists/" rel="noopener noreferrer"&gt;check here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;END&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you liked the content, do react, or for any feedback/correction/suggestion feel free to reach out to me on &lt;a href="https://www.linkedin.com/in/sakshi9ain/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt; or can comment here&lt;/p&gt;

&lt;p&gt;Nice Day&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>INT FLOAT COMPLEX - PYTHON</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Fri, 29 Mar 2024 19:21:26 +0000</pubDate>
      <link>https://forem.com/jainsakshi/int-float-complex-python-a4g</link>
      <guid>https://forem.com/jainsakshi/int-float-complex-python-a4g</guid>
      <description>&lt;p&gt;Good day geeks!&lt;/p&gt;

&lt;h2&gt;
  
  
  PYTHON UNLEASEH'D 06
&lt;/h2&gt;

&lt;p&gt;This mediocre sounding blog is going to be interesting unlike its name I swear. No already known information here, not discussing basics, just (RARELY KNOWN) things for people shifting to python.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffcxatt4yntojorw2d81w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffcxatt4yntojorw2d81w.gif" alt=" " width="220" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have the following data types in python :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Text Type:  str
Numeric Types:  int, float, complex
Sequence Types: list, tuple, range
Mapping Type:   dict
Set Types:  set, frozenset
Boolean Type:   bool
Binary Types:   bytes, bytearray, memoryview
None Type:  NoneType
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Numeric Types&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Integers – This value is represented by &lt;strong&gt;int class&lt;/strong&gt;. It contains positive or negative whole numbers (without fractions or decimals). In Python, there is &lt;strong&gt;no limit to how long an integer value can be.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Float – This value is represented by the &lt;strong&gt;float class&lt;/strong&gt;. It is a real number with a floating-point representation. It is specified by a decimal point. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Complex Numbers – A complex number is represented by a complex class. It is specified as (real part) + (imaginary part)j. For example – 2+3j&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Program for complex numbers
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokm5bj90ladov1qisq2h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokm5bj90ladov1qisq2h.png" alt=" " width="708" height="623"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AS MENTIONED, ONLY 'j/J' is allowed to represent imaginery part of complex number. Please try in your editor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python has class for each data type, as read above. Okay
&lt;/h3&gt;

&lt;p&gt;I guess there must be some methods associated with each class. Lets search about this and explore.&lt;/p&gt;

&lt;p&gt;FROM STACKOVERFLOW -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;int is a class. The type of a class is usually type.&lt;/p&gt;

&lt;p&gt;And yes, almost all classes can be called like functions. You create what's called an instance which is an object that behaves as you defined in the class. They can have their own functions and have special attributes.&lt;/p&gt;

&lt;p&gt;(type is also a class if you're interested but it's a special class. It's a bit complicated but you can read more on it if you'll search for metaclasses)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We get plethora of methods and information🤯 about this 'int' class on writing &lt;code&gt;help(int)&lt;/code&gt; in notebook. That's more than enough, lets wrap up🤐. These numeric types are associated with their respective classes and what all variables we create are instances. These classes have methods. ✔✔&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@artur-khudaverdiev/lesson-9-python-data-types-numeric-data-types-int-float-b5f009e69872" rel="noopener noreferrer"&gt;A good read on numeric data types of Python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use underscore as separators for big integer values like we use comma in maths.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fro319iullkpwbplboi3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fro319iullkpwbplboi3a.png" alt=" " width="356" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading guys&lt;/p&gt;

&lt;p&gt;If you liked the content, do react, or for any feedback/correction/suggestion feel free to reach out to me on &lt;a href="https://www.linkedin.com/in/sakshi9ain/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt; or can comment here...&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>LOOPS - FOR LOOP</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Fri, 29 Mar 2024 15:56:52 +0000</pubDate>
      <link>https://forem.com/jainsakshi/loops-for-loop-5e2g</link>
      <guid>https://forem.com/jainsakshi/loops-for-loop-5e2g</guid>
      <description>&lt;h2&gt;
  
  
  HI AGAIN
&lt;/h2&gt;

&lt;h3&gt;
  
  
  PYTHON UNLEASH'D 04
&lt;/h3&gt;

&lt;p&gt;Back with basics, grinding on loops in python. Life becomes really hard when you have to manage so many things&lt;/p&gt;

&lt;p&gt;Anyways, focus on agenda!&lt;/p&gt;

&lt;p&gt;We know about for loop, lets look at its basic syntax first.&lt;/p&gt;

&lt;h2&gt;
  
  
  for loop with list
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for name in list1:
    print(name)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  for loop within range
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;list2 = [1,2,3,4,5,6]
for i in range(0,6,2):
    print(list2[i])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  for loop with dictionaries
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;d = dict()
d['a'] = 123
d['b'] = "hehe"

for i in d:
    print(i,d[i])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  zip() used to traverse two list together
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits = ["apple", "banana", "cherry"]
colors = ["red", "yellow", "green"]
for fruit, color in zip(fruits, colors):
    print(fruit, "is", color)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tuple
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tup = (1,2,3,4,"holy")
for a in tup:
    print(a)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t = ((1, 2), (3, 4), (5, 6))
for a, b in t:
    print(a, b)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good examples&lt;br&gt;
See you in next blog guys!&lt;br&gt;
Happy reading...&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>Mastering Strings - Python</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Fri, 29 Mar 2024 15:52:36 +0000</pubDate>
      <link>https://forem.com/jainsakshi/understanding-strings-python-67j</link>
      <guid>https://forem.com/jainsakshi/understanding-strings-python-67j</guid>
      <description>&lt;p&gt;Good day guys,&lt;/p&gt;

&lt;h2&gt;
  
  
  Python Unleash'd 05
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8sxm0c557ojj1lyo9fdw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8sxm0c557ojj1lyo9fdw.gif" alt=" " width="189" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I always wished someone taught me strings along with all its methods and behaviors, there are some exceptions where things become complex. Lets see all applicable methods on strings one by one. &lt;/p&gt;

&lt;p&gt;Here we see empty string with nothing, empty string with space&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("first")
empty_string = ""
print(empty_string, len(empty_string))
print("second")
string_with_space = " "
print(string_with_space, len(string_with_space))
print("third")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output is : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fza50d5mcsks720vmnbm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fza50d5mcsks720vmnbm6.png" alt=" " width="407" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;String with one space has length 1 while without space has 0 length, i.e, an empty string.&lt;/p&gt;

&lt;p&gt;Lets move forward to see different ways of creating string&lt;br&gt;
&lt;code&gt;str = 'string'&lt;/code&gt;&lt;br&gt;
&lt;code&gt;str = "string"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;''' str
    =
    multiline
    string 
'''
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of multiline string with triple quotes : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk6ad3kj28z8lbac3k81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk6ad3kj28z8lbac3k81.png" alt=" " width="569" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehe2rus17gkvic4wpzw3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fehe2rus17gkvic4wpzw3.png" alt=" " width="244" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Random Access
&lt;/h2&gt;

&lt;p&gt;We can access string character using negative and positive indexing. Positive indexing means from left to right, while negative starts from back to front. -1 index means last element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slicing (was once a toughest concept for me)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Colon is used for slicing.&lt;/li&gt;
&lt;li&gt;Slicing, as the name suggests, means cutting string in slices&lt;/li&gt;
&lt;li&gt;Slicing does not change string content unless we assign it to sliced string&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsabv4qxgae3j0styy1em.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsabv4qxgae3j0styy1em.png" alt=" " width="402" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  TO GET SUBSTRING YOU SLICE THE STRING
&lt;/h3&gt;

&lt;p&gt;Now let us see how index is given within these square brackets, by breaking it in pieces.&lt;/p&gt;

&lt;p&gt;So last index is always excluded in the range.&lt;br&gt;
When I write &lt;code&gt;string[0:3]&lt;/code&gt;&lt;br&gt;
0 is the starting index, while 2 is the end index. It excludes last index.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;string[1:-1]&lt;/code&gt;, it includes char from 1 to -2 &lt;strong&gt;and not from 0 to -1.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note that indexing starts from 0 and not 1&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  So is not there any way to get entire string as substring
&lt;/h3&gt;

&lt;p&gt;I call slice as substring. Yes, there is a way.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;string[0:]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we'll get entire string.&lt;/p&gt;
&lt;h3&gt;
  
  
  Reverse string with help of colons
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;string[::-1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here we are introduced to a new param, third digit after second colon. It is used for step. &lt;/p&gt;

&lt;p&gt;We are taking some easy examples here, to understand this STEP.&lt;/p&gt;

&lt;p&gt;Look at the following examples, consider there is a string 'temp = maple_string' as its value.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;temp[:]&lt;/code&gt; gives entire string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tem[0:]&lt;/code&gt; gives entire string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;temp[:1]&lt;/code&gt; gives only first character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;temp[::]&lt;/code&gt; gives empty string&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;first param before first column is for start index&lt;br&gt;
second param after first colon is for end index&lt;br&gt;
leaving blank means, start or end depending on colon&lt;/p&gt;

&lt;p&gt;If there is mistake, or you write end index before start index, then you get empty string returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5mnem2jr8rxyipqsyo0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5mnem2jr8rxyipqsyo0h.png" alt=" " width="800" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;string[1:0]&lt;/code&gt; returns empty string&lt;/p&gt;

&lt;p&gt;&lt;code&gt;temp[-1:]&lt;/code&gt; is same as temp[-1], both return last char&lt;/p&gt;

&lt;p&gt;&lt;code&gt;temp[:1]&lt;/code&gt; is same as temp[0], in the former, 1 is not included as it is end index so only 0th char is returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpuckse9skgp7xt173xk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpuckse9skgp7xt173xk.png" alt=" " width="351" height="758"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider, &lt;code&gt;temp = "maple_string"&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;temp[1:2:1]&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  last part of slice can not be 0, in three step, either remove third step or it should not be zero. IT CAN BE ANY POSITIVE OR NEGATIVE NUMBER BUT NOT ZERO.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugv4a81uq5jqquapdts5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugv4a81uq5jqquapdts5.png" alt=" " width="657" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In above code snapshot, we see there -55 as the STEP, which does not even exist, so when it takes -55th step between start and end index, it does not get anything so it returns empty string, not even space.&lt;/p&gt;

&lt;p&gt;Below we have another code snapshot from notebook, here I have provided valid STEP size, which will not return empty string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcisax41lm5qdylw36w9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcisax41lm5qdylw36w9k.png" alt=" " width="642" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It obtains the substring from index one to one only, and step size is one so it returns just that character which is at temp[1]&lt;/p&gt;

&lt;p&gt;Step size of one does not mean anything is skipped, it is equivalent to leaving third param empty.&lt;/p&gt;

&lt;p&gt;Look the next example, still we have&lt;/p&gt;

&lt;p&gt;&lt;code&gt;temp = "maple_string"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;See how we get every second character from maple_string&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1s24lumtf8fi4znaxqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1s24lumtf8fi4znaxqg.png" alt=" " width="666" height="121"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Few more examples where we can get empty strings and full string by writing less
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc9nb3h027hiy670i1fm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc9nb3h027hiy670i1fm.png" alt=" " width="322" height="421"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Some more code you should look at -
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9lfmbjtxln8e4x171qv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9lfmbjtxln8e4x171qv.png" alt=" " width="451" height="720"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Program to reverse a string
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftv82frl2s7bz3sjwd95g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftv82frl2s7bz3sjwd95g.png" alt=" " width="704" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;reversed returns hexadecimal value, so we use join with empty string to make it string again.&lt;/p&gt;

&lt;p&gt;More example of using JOIN with STRING&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexh7tbo1n9ctq23qgcb1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexh7tbo1n9ctq23qgcb1.png" alt=" " width="515" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the output, its 5 after every character of string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#temp[5] = 'y'
#string does not support updation like this, either convert to list or slice then join
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to update/delete from a string
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Python Program to Update 
# character of a String 

String1 = "Hello, I'm a Geek"
print("Initial String: ") 
print(String1) 

# Updating a character of the String 
## As python strings are immutable, they don't support item updation directly 
### there are following two ways 
#1 
list1 = list(String1) 
list1[2] = 'p'
String2 = ''.join(list1) 
print("\nUpdating character at 2nd Index: ") 
print(String2) 

#2 
String3 = String1[0:2] + 'p' + String1[3:] 
print(String3) 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some more methods on string&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54338omlz1ui4n5wdnfs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54338omlz1ui4n5wdnfs.png" alt=" " width="551" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Update entire string is comparatively easier
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Python Program to Update 
# entire String 

String1 = "Hello, I'm a Geek"
print("Initial String: ") 
print(String1) 

# Updating a String 
String1 = "Welcome to the Geek World"
print("\nUpdated String: ") 
print(String1) 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deleting a character
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Deleting a character 
# of the String 
String2 = String1[0:2] + String1[3:] 
print("\nDeleting character at 2nd Index: ") 
print(String2) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete entire string, easier
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;del String1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  String REPLACE
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string = "Hello World"
new_string = string.replace("Hello", "Good Bye")

print(new_string)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output we'll get here is - Good Bye World&lt;/p&gt;

&lt;p&gt;It does not change the original string but returns a new one. It is mostly used in string substitution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Prints the string by replacing only 3 occurrence of 'ek' by 'a'

print(string.replace("ek", "a", 3))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output -&lt;/p&gt;

&lt;p&gt;geas for geas geas geeks geeks&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9wo057qfprgncv0lg4c3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9wo057qfprgncv0lg4c3.gif" alt=" " width="220" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;END.&lt;/p&gt;

&lt;p&gt;Thanks for reading&lt;br&gt;
Will learn more about input and input formatting in upcoming blogs&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>learning</category>
      <category>programming</category>
    </item>
    <item>
      <title>What You Need To Know About Conditional Statements In Python ?</title>
      <dc:creator>Sakshi J</dc:creator>
      <pubDate>Fri, 29 Mar 2024 09:47:09 +0000</pubDate>
      <link>https://forem.com/jainsakshi/what-you-need-to-know-about-conditional-statements-in-python--25ni</link>
      <guid>https://forem.com/jainsakshi/what-you-need-to-know-about-conditional-statements-in-python--25ni</guid>
      <description>&lt;h2&gt;
  
  
  PYTHON UNLEASH'D 04
&lt;/h2&gt;

&lt;p&gt;HELLO LETS SEE CONDITIONAL STATEMENT IN PYTHON&lt;/p&gt;

&lt;p&gt;As indentation plays major role in python. Indentation is used in place of brackets. &lt;br&gt;
In conditional statements, we have colon which we have to use after is and else, at the end.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;if statement&lt;/li&gt;
&lt;li&gt;if-else statement&lt;/li&gt;
&lt;li&gt;nested if
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Unlike C++, we do not write else-if we write elif in python in if-else chain&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case of nested if the following if statement execute only if the s=above statements are true, else if comes to 'else block'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WE HAVE SHORTHANDS ALSO FOR IF ELSE&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var = 6
if(var &amp;gt; 5): print("greater")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("hello") if var &amp;gt; 5 else print("no")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ternary Operator
&lt;/h3&gt;

&lt;p&gt;In C++, we have below ternary operator&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(5 &amp;lt; 6)?b = "true": b = "false";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here in python, we have if-else in ternary operator&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each operand of the Python ternary operator is an expression, not a statement, that means we can’t use assignment statements inside any of them. Otherwise, the program will throw an error.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a, b = 10, 20

print ("Both a and b are equal" if a == b else "a is greater than b"
        if a &amp;gt; b else "b is greater than a")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/ternary-operator-in-python/" rel="noopener noreferrer"&gt;Shows good example of how we can use ternary operator with tuple, dictionaries and lambda function&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are pretty familiar with if-else so lets move to next new type of conditional statement, not definitely the new but here we have different name and keyword for that.&lt;/p&gt;

&lt;p&gt;Also are you aware about 'pass' and when to use this?&lt;br&gt;
Lets read about pass in next blog of this series.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
