<?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>DEV Community: Siddharth Chandra</title>
    <description>The latest articles on DEV Community by Siddharth Chandra (@siddharth2016).</description>
    <link>https://dev.to/siddharth2016</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%2F505934%2Fc2642f29-2738-406e-aed1-eb7ca89612a9.jpeg</url>
      <title>DEV Community: Siddharth Chandra</title>
      <link>https://dev.to/siddharth2016</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siddharth2016"/>
    <language>en</language>
    <item>
      <title>Blockchain Fundamentals, Cryptocurrencies and Open Source</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 06:09:57 +0000</pubDate>
      <link>https://dev.to/siddharth2016/blockchain-fundamentals-cryptocurrencies-and-open-source-2235</link>
      <guid>https://dev.to/siddharth2016/blockchain-fundamentals-cryptocurrencies-and-open-source-2235</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;This article is divided into 4 sections, 1&lt;sup&gt;st&lt;/sup&gt; section is about blockchain fundamentals, 2&lt;sup&gt;nd&lt;/sup&gt; section deals with the key features of building a blockchain mobile app, 3&lt;sup&gt;rd&lt;/sup&gt; section dives into cryptocurrency wallets available for us to use and 4&lt;sup&gt;th&lt;/sup&gt; section outlines how the opensource is driving the blockchain revolution.&lt;/p&gt;

&lt;p&gt;You can jump onto any section you like to read, the order of reading these sections are not related.&lt;/p&gt;




&lt;h2&gt;
  
  
  Blockchain Fundamentals
&lt;/h2&gt;

&lt;p&gt;Blockchain technology is a constantly evolving, secure and shared record-keeping system in which each user has a copy of the data, which can only be modified if all parties involved in a transaction agree. This section examines the fundamentals of blockchain technology.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Blockchain Transaction
&lt;/h3&gt;

&lt;p&gt;To understand the fundamentals of the blockchain, we need to understand how a transaction can be added to the existing blockchain. This can be done by going through a few important steps, as illustrated in the below figure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7KaSzQNT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427151158/95A8tJwIo.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7KaSzQNT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427151158/95A8tJwIo.jpeg" alt="fig1.jpeg" width="666" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Assume that user 1 wants to send X amount of digital bitcoins to user 2. User 1 then initiates the transaction.&lt;/li&gt;
&lt;li&gt;A transaction is initiated by user 1's node by first making it and then digitally signing it with its private key. A transaction in a blockchain can reflect a range of actions.&lt;/li&gt;
&lt;li&gt;A peer-to-peer (P2P) network broadcasts the desired transaction to each individual computer (or node).&lt;/li&gt;
&lt;li&gt;Individual nodes receive the request and attempt to validate the transaction using cryptographic techniques. The miner node is the final node in the validation chain. These miner nodes are compensated in Bitcoin.&lt;/li&gt;
&lt;li&gt;The verified blocks are now added to the blockchain network. Hashing is used to connect these nodes.&lt;/li&gt;
&lt;li&gt;The approved transactions are then recorded in a public ledger. Transactions are completed and the ledger is updated after the block is added to an existing chain.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Cryptography Techniques Used in Blockchain
&lt;/h3&gt;

&lt;p&gt;Hashing, public-private key mechanisms, and digital signatures are the key cryptographic techniques used by the blockchain community. In this case, a hash feature is implemented to offer each user the opportunity to view the blockchain in its entirety. Blockchain characteristically uses the SHA-256 hashing algorithm. Public-private key pairs are used to gain access to the information and allow transactions. Digital signatures are also used for multi-signature contracts and digital wallets on the blockchain, as well as to ratify transactions by signing them securely (offline).&lt;/p&gt;

&lt;h3&gt;
  
  
  Working of A Blockchain Network
&lt;/h3&gt;

&lt;p&gt;In a blockchain network, each node holds a complete copy of the distributed ledger. The four fields of a block in this network are the block number, the data field, the hash value, and the nonce. Miners adjust the value of nonce to make it a suitable number for hashing the value of a block. A nonce is a random whole number that is a 32-bit (4-byte) field that is changed by miners to make it an acceptable number for hashing the value of a block. In addition, the chain of blocks is generated by applying the hash of the previous block to the current block. The previous block's hash is stored in the field prior.&lt;/p&gt;

&lt;p&gt;Four zeros are used to begin the hash value. This determines whether or not a block is valid. Each new hash value is created by combining the old hash value, the new transaction block, and a nonce. The hash cash difficulty factor is determined by the number of leading zeroes required in the hash output. The difficulty is tweaked so that block production stays consistent at about one block per ten minutes. The more difficult it is to alter the blockchain, the tougher it is to overwrite the blockchain and double-spend coins.&lt;/p&gt;

&lt;h3&gt;
  
  
  Winding Up
&lt;/h3&gt;

&lt;p&gt;Blockchain technology is widely accepted by a variety of businesses. It has opened up a plethora of opportunities in the areas of healthcare, finance, accountancy and the online marketplace. The adoption of blockchain technology is expected to grow manifold in the coming years, offering quite a wide range of jobs for young aspirants.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basics of Building a Blockchain App
&lt;/h2&gt;

&lt;p&gt;A blockchain stores data in such a way that it is almost impossible to alter or hack it. In this section, we will go through the key features of the blockchain, look at blockchain development platforms, and view the steps in the blockchain mobile app development process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Blockchain Apps
&lt;/h3&gt;

&lt;p&gt;While blockchain development began with cryptocurrencies, it is no longer limited to them. Blockchain applications have now gone beyond cryptocurrency and into a variety of other sectors. As we all know, the main benefit of blockchain technology is that it ensures maximum data transparency and security, helping businesses and organisations save a significant amount of money on standard security methods.&lt;/p&gt;

&lt;p&gt;The following are some useful blockchain apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Filament:&lt;/strong&gt; This is an excellent example of a blockchain-based IoT app that uses blockchain data ledger systems to manage data processed by software and microchip hardware. Several linked devices can communicate information over the blockchain and keep in sync with the help of the app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. KYC-chain:&lt;/strong&gt; For many firms, completing Know Your Customer (KYC) documentation is a must. This simple and effective blockchain-based program streamlines the entire process of handling client documentation across administrative and criminal databases in many nations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. BurstIQ:&lt;/strong&gt; BurstIQ is healthcare software that uses Big Data and blockchain technology to allow patients and doctors to share and collaborate on vital medical information.&lt;/p&gt;

&lt;h3&gt;
  
  
  Development Platforms
&lt;/h3&gt;

&lt;p&gt;There are more than two dozen blockchain development platforms. A few of them are described below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Multichain:&lt;/strong&gt; With this powerful platform, developers can create and deploy private blockchains that can be used by a variety of businesses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Hyperledger:&lt;/strong&gt; This distributed ledger technology is used to govern the connected ecosystem of things and supply chains. This advanced blockchain platform has received a lot of acclaims.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Ethereum:&lt;/strong&gt; A versatile opensource blockchain technology, Ethereum is commonly used for decentralised apps, smart contracts and initial coin offerings (ICOs).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. EOS:&lt;/strong&gt; The EOS platform’s goal is to create a reliable smart contract function. It’s also appropriate for modern businesses’ need for highly scalable and decentralised storage and hosting.&lt;/p&gt;

&lt;p&gt;Blockchain systems are available in a wide range of sizes and shapes. Determining which platform is better and more suitable for one's application needs might be confusing. The solution is contingent on the desired capabilities and use-cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choosing the most appropriate platform:&lt;/strong&gt; The key to your project’s success lies in determining the platform’s character. Platforms based on cryptocurrencies as well as those based on smart contracts are available.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decide whether you need smart contracts or not:&lt;/strong&gt; You need to determine if your app needs smart contracts or not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Selecting the consensus protocol:&lt;/strong&gt; The consensus protocol ensures that every new block added to the blockchain is the only version of the truth that all nodes in the blockchain agree on, such as proof of stake, proof of work, proof of elapsed time, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choosing the cryptocurrency:&lt;/strong&gt; Another challenge is determining whether or not a cryptocurrency is required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Selecting between a public and private network:&lt;/strong&gt; Based on the requirement, you need to decide whether a public or a private network is needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Process of Blockchain Mobile App Development
&lt;/h3&gt;

&lt;p&gt;There are mainly five stages in the blockchain app development process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Identify the problem that the blockchain is required to solve:&lt;/strong&gt; To begin, thoroughly examine the problem description and create a full report on it. Consider whether you need to create a new app from scratch or if you can just integrate the blockchain into an existing one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Select the best platform for the app:&lt;/strong&gt; Select the most appropriate platform for your project. Determine if you need a smart contract app or a crypto-transaction app, as well as the type of network and consensus protocol you'll need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Requirement analysis:&lt;/strong&gt; Begin by doing the requirement analysis and evaluating the app idea’s requirements. Do more brainstorming around the idea and, if possible, fine-tune it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Create a blueprint of the idea:&lt;/strong&gt; Try to develop the proof of concept that will put the entire app concept into a visual structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Development of the application:&lt;/strong&gt; Develop the application employing best practices. Ensure to build a robust blockchain app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Winding Up
&lt;/h3&gt;

&lt;p&gt;We’ve covered the basics of blockchain in this essay — its main characteristics, as well as several development platforms and stages for developing blockchain applications. If you find the subject interesting, you can delve deeper into it. There are a variety of resources available on the Internet.&lt;/p&gt;




&lt;h2&gt;
  
  
  Cryptocurrency Wallets
&lt;/h2&gt;

&lt;p&gt;Cryptocurrencies are getting entrenched in the global financial system slowly and steadily, with certain companies using these to pay their employees too. Hesitancy with respect to their use is slowly ebbing, and you may run the risk of being behind the times if you don’t stay updated on the latest trends in this domain. This section lists the exchanges and wallets you should be aware of in case you want to buy or trade-in cryptocurrencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Trends in Cryptocurrencies
&lt;/h3&gt;

&lt;p&gt;Due to the high potential for profit, cryptocurrency trading is considered extremely lucrative, particularly among the younger population. Traditional stock market trading has a lot of restrictions and rules. There are no such limitations in the case of cryptocurrencies, and big profits can be produced immediately. The 'crypto market,' in which 'crypto users' trade various types of cryptocurrencies, is also known as the cryptocurrency industry.&lt;/p&gt;

&lt;p&gt;The price of a BitCoin (BTC) was less than one US dollar around 10 years back when it was launched. Today, its price is more than US$ 30,000. A few months ago, its price touched around US$ 60,000, proving it is a very attractive form of investment.&lt;/p&gt;

&lt;p&gt;The market capitalisation of cryptocurrencies is around 338 million US dollars, with 13,951 BitCoin ATMs located in different parts of the world, as per a report from Statista.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wallets and Exchanges
&lt;/h3&gt;

&lt;p&gt;Currently, there are a number of exchanges in the world that offer platforms and wallets for buying and trading cryptocurrencies. These include Web-based environments as well as apps for Android, iPhone and other mobile phone platforms. Crypto users can buy and sell different currencies that are legal tender around the world with ease on these sites.&lt;/p&gt;

&lt;p&gt;The major worldwide crypto exchanges in the world that provide wallets and Web platforms for cryptocurrency trading are listed in the below figure. In crypto exchanges, pairings refer to the pairs of cryptocurrencies that are used to buy and sell such coins.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_KPdLCAL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427259763/bIN_-rYI3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_KPdLCAL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427259763/bIN_-rYI3.png" alt="fig2.png" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the crypto world, there are two types of exchanges and wallets — centralised and decentralised.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aXYHjFS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427269884/Ao3ACLUK-.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aXYHjFS2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427269884/Ao3ACLUK-.jpeg" alt="fig3.jpeg" width="600" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Centralized Exchanges
&lt;/h4&gt;

&lt;p&gt;The centralised exchanges and wallets are owned by companies or corporate giants, which record the complete Know Your Customer (KYC) details either through video KYC or paper-based KYC. Users can open accounts in these centralised exchanges and then trade in any cryptocurrency.&lt;/p&gt;

&lt;p&gt;The below figure lists some centralized exchanges.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LgcZ7rvz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427285675/PKkGxWZiT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LgcZ7rvz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427285675/PKkGxWZiT.png" alt="fig4.png" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Decentralized Exchanges
&lt;/h4&gt;

&lt;p&gt;Decentralised exchanges are platforms on which the account is opened directly without any intermediate company, and trading is done directly by the crypto users.&lt;/p&gt;

&lt;p&gt;The below figure lists some decentralized exchanges.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EDePjtBN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427313694/LmhcxQcF-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EDePjtBN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1640427313694/LmhcxQcF-.png" alt="fig5.png" width="800" height="678"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Winding Up
&lt;/h3&gt;

&lt;p&gt;The jury is out on the use of cryptocurrencies because these are sometimes used to circumvent the national money transfer system. Some governments prohibit their use in order to prevent money laundering.&lt;/p&gt;

&lt;p&gt;In order to incorporate the maximum benefits and features into a real-world scenario, the use of cryptocurrencies in any country demands legislation and procedures. The benefits to society and the corporate sector might be substantial if cryptocurrencies are used for real-time applications and e-governance.&lt;/p&gt;




&lt;h2&gt;
  
  
  Open Source Blockchain Revolution
&lt;/h2&gt;

&lt;p&gt;Blockchain is a disruptive technology that is changing the world in a variety of ways. Cryptocurrencies are the most obvious example. This section compares and contrasts this technique with opensource, another revolutionary technology that has transformed software development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Blockchain Characteristics
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Immutability:&lt;/strong&gt; A hallmark feature of blockchain is that it is incorruptible. Data once saved in a block cannot be changed without breaking the chain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decentralisation:&lt;/strong&gt; Blockchain does not require a central authority to maintain the ledger. All nodes in the network have a copy of the ledger that gets updated with every transaction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enhanced security:&lt;/strong&gt; Decentralisation and consensus mechanisms built into blockchain make it highly secure, as every transaction has to be verified with ‘proof of work’.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Distributed:&lt;/strong&gt; The blockchain ledger is not a singular entity in the hands of a few; instead it is a shared entity that is accessible and maintained by everyone in the network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transparency:&lt;/strong&gt; Every addition to the blockchain requires consensus from all nodes in the peer-to-peer network. This makes every transaction transparent and public.&lt;/p&gt;

&lt;h3&gt;
  
  
  Open Source Characteristics
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Transparency:&lt;/strong&gt; The founding principle of open source is to make the source code of the project public. This enhances the transparency of the project, as every detail that goes into its development can be freely accessed by anyone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enhanced security:&lt;/strong&gt; Finding security loopholes in the software is way easier when the code is non-proprietary and is publicly available. This naturally enhances the security of the software or, at the very least, can help raise public awareness about the potential security-related pitfalls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Collaboration:&lt;/strong&gt; The primary purpose of open source software is to encourage other developers to collaborate and contribute to the development of the software. This makes open source development, by nature, a community experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modifiability:&lt;/strong&gt; Open source software can be modified and re-distributed to solve different problems. This is the case with Linux, as its modified ‘distributions’ are used in various different gadgets and computers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decentralisation:&lt;/strong&gt; One can argue that open source software is not inherently decentralised as making changes to the main project still requires the approval of the project moderator(s) and the owner(s). But if perceived from a different angle, given that the source code can be modified and re-distributed under proper licensing, this gives the developer the autonomy to use the software as and how desired thus making the process decentralised.&lt;/p&gt;

&lt;h3&gt;
  
  
  Convergent Evolution
&lt;/h3&gt;

&lt;p&gt;Opensource software and blockchain are two independent technologies with different themes and strategies for addressing different problems. When we compare the attributes of blockchain to open source, however, we discover certain similarities.&lt;/p&gt;

&lt;p&gt;In evolutionary biology, the term 'convergent evolution' refers to the occurrence in which animals from distinct time periods or epochs acquire similar characteristics. This can be seen in molecular biology, animal morphology, and even plant morphology. This holds true for technical breakthroughs as well. We produce new technologies every day to address a wide range of problems; nonetheless, there are underlying patterns in the motivations that drive the creation of these technologies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Winding Up
&lt;/h3&gt;

&lt;p&gt;Blockchain is new, but open source technology has been around for a long time. In my perspective, the rise of open source technology has had a direct impact on the birth of blockchain. Both technologies are focused on transferring power from a small number of people to a big number of people. Both strive to increase product security and transparency while also promoting fairness and collaboration. While the applications of open source and blockchain are diverse, the motives for their creation are similar. It is safe to assume that open source and blockchain share many characteristics in common and are hence the product of technological convergence.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>cryptocurrency</category>
      <category>opensource</category>
      <category>web3</category>
    </item>
    <item>
      <title>Big Data Open Source Frameworks</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 06:08:46 +0000</pubDate>
      <link>https://dev.to/siddharth2016/big-data-open-source-frameworks-3app</link>
      <guid>https://dev.to/siddharth2016/big-data-open-source-frameworks-3app</guid>
      <description>&lt;p&gt;Big Data is a term used to define large scale data sets that are too complex to be manipulated with basic DBMS. Handling Big Data requires sophisticated hardware and software technologies.&lt;/p&gt;

&lt;p&gt;Just as open-source has been the primary reason for the Big Data revolution, so has it been at the forefront of developing tools to counter the challenges that we face with the data. And while there are hundreds of popular open-source projects that are focused on Big Data management, in this article, we shall focus on the top three that caught my attention.&lt;/p&gt;

&lt;p&gt;We shall discuss the Hadoop framework, the Spark analytics engine and the Kafka framework.&lt;/p&gt;




&lt;h3&gt;
  
  
  Is Big Data Hard To Handle?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Almost 90% of the Big Data we produce today is unstructured and there are no predefined models that it adheres to. And there is no quick and easy way to access and analyse unstructured data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not only is Big Data unstructured, but it is also extremely complex. This means traversing through it requires advanced algorithms that are being run on top of grade-A machinery and not everyone can afford that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have multiple sources from which we accrue data and there are no clean ways to integrate disparate data from so many sources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With Big Data comes big mistakes. Storing massive and complex data in a secure manner is a really daunting challenge. It is also the need of the hour. Since the beginning of the pandemic, more people are stuck at home with their computers than ever before and cybercrime and data theft have gone up by 400%.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Open Source Softwares
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Hadoop
&lt;/h4&gt;

&lt;p&gt;Developed by the Apache Foundation, Hadoop is a framework for Big Data management. It uses distributed systems architecture, which has the ability to store and easily access vast amounts of data.&lt;/p&gt;

&lt;p&gt;While there are many different types of Big Data management solution software, what makes Hadoop stands out are 3 components, HDFS, MapReduce and YARN.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HDFS (Hadoop Distributed File System)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Hadoop Distributed File System (HDFS) is a powerful and dynamic tool that is efficient at handling Big Data. HDFS breaks down Big Data into blocks and the default size of each block is 128MB. This makes accessing data simple, as the HDFS only has to access the specific block of data that the user is searching for. HDFS is a resilient framework, thanks to its replication methodology. To maintain the accessibility of the data even when one of the distributed system blocks is down, HDFS replicates that block data in a three-fold fashion making it robust to avoid failures or inconsistency.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MapReduce&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Storing and accessing Big Data, with all its complications and difficulties, is still the easy part. The real fun begins when we try to process Big Data. MapReduce is exactly what it sounds like, it maps the data followed by reducing it. MapReduce takes the data as input and splits it into different parts. It then maps each individual component within each part. Then the data is shuffled and sorted into clusters of homogenous data. The data is then reduced by integrating homogenous data and assigning IDs, which give the smaller final output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;YARN (Yet Another Resource Negotiator)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Hadoop environment is constrained to the number of resources it has, namely, CPUs, memory units, storage space, etc. But since Hadoop is designed to be able to run multiple processes at the same time, a mechanism for allocating resources is necessary to make sure there are no deadlocks and every task is accomplished in its desired time period. Now, YARN comes into play, when a job requires the resources to run, the application manager sends a request to the node manager to allocate the physical resources. The node manager then sends the resources to the resource manager, which assigns the physical resource to the client process.&lt;/p&gt;

&lt;h4&gt;
  
  
  Spark
&lt;/h4&gt;

&lt;p&gt;While HDFS, MapReduce and YARN are the three major defining components of Hadoop, the Hadoop ecosystem has more tools to offer when it comes to Big Data processing, one such tool is Apache Spark. Spark is one of the few tools in the industry that combines Big Data analytics tools with Machine Learning and AI. Spark is 100 times faster than MapReduce in processing data. Spark computations are done in memory itself, it has the ability to perform both batch processing real-time processing of data. Spark is also comparatively lightweight as most of it is written in &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Following are the Spark components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spark Core&lt;/li&gt;
&lt;li&gt;Spark SQL&lt;/li&gt;
&lt;li&gt;Spark Streaming&lt;/li&gt;
&lt;li&gt;Spark MLib&lt;/li&gt;
&lt;li&gt;Spark GraphX&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Kafka
&lt;/h4&gt;

&lt;p&gt;Apache Kafka is a framework implementation of a software bus using stream processing. If you break down an information transaction on the Internet, it's essentially nothing but an exchange of information between 2 systems which we sometimes call producers and consumers. Each transaction can be thought of as integration between producer and consumer. In order to simplify complex integrations, Kafka acts as a broker between producer and consumer, which can be well integrated with Hadoop and Spark.&lt;/p&gt;

&lt;p&gt;Kafka consists of Brokers and a Zookeeper.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Broker&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A broker is a Kafka server that runs in a Kafka cluster. Each broker divides a unique transaction between the producer and consumer into a topic. Now, these topics within the broker are used to consume and broadcast the messages in an asynchronous fashion.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Zookeeper&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to define topics and keep a metadata record of all the topics defined in the cluster, Kafka uses Zookeeper which is a replicated distributed log with a file system API built on top. It keeps a record of all the topics within all the nodes and also keeps track of which nodes are down and which are up and running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where To Go From Here?
&lt;/h3&gt;

&lt;p&gt;The tools and frameworks discussed in this article are a high-level overview of the innovation and efforts taking place in the Big Data management industry. There are many more open source tools and frameworks that can be learned and used for Big Data management.&lt;/p&gt;

&lt;p&gt;Google all the available resources and check which fits your Big Data management need to the point and become a Big Data Engineer one step at a time.&lt;/p&gt;

&lt;p&gt;Books to check out more on Big Data Engineering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3DbHhYN"&gt;Big Data Simplified&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3phG2CF"&gt;Big Data Analytics, Introduction to Hadoop, Spark, and Machine-Learning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3rvFcoE"&gt;Big Data and Analytics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>python</category>
      <category>bigdata</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Scala Vs Python Syntax Cheat Sheet</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 06:06:48 +0000</pubDate>
      <link>https://dev.to/siddharth2016/scala-vs-python-syntax-cheat-sheet-3d26</link>
      <guid>https://dev.to/siddharth2016/scala-vs-python-syntax-cheat-sheet-3d26</guid>
      <description>&lt;p&gt;There are about 8 million Python developers around the world and it's also the top programming language people want to learn, but why is that? Why not any other language but Python? Let me tell you, Python offers a lean learning curve, with just 2-3 days of hands-on Python anyone can start developing applications (they might not be technically sound software initially), it's syntax is one of the easiest to learn due to its plain english like code, it offers flexibility from both domains either object-oriented or functional programming (not so much FP, but it has its benefits) and it is used in almost every field of software engineering.&lt;/p&gt;

&lt;p&gt;The only drawback people face when working with Python is related to its speed of executing programs. But is there any other programming language as easy to understand and to get started to work with? Yep, there is, Scala comes to the rescue!&lt;/p&gt;

&lt;p&gt;Python faces speed issues mainly due to its interpreted nature, whereas Scala is a compiled language that offers dynamic typing as well (close to 90%), many new features in Python like pattern matching are already present in Scala since the beginning, there are awesome syntactic sugars in Scala that allows us to write even less code when compared to Python and also Scala being a functional programming language does not shy away from object-oriented programming as it is run over JVM.&lt;/p&gt;

&lt;p&gt;When it comes to the syntax of these programming languages, there are a lot of similar patterns and some differences as well, but overall they both offer a lean learning curve with endless opportunities in the software engineering world.&lt;/p&gt;

&lt;p&gt;So, let's discuss those similarities and differences in the form of a cheat sheet, which not only helps in refreshing some concepts from Python/Scala but also acts as a guide to learn Python or Scala for someone coming from the Scala or Python background respectively.&lt;/p&gt;

&lt;p&gt;This will be a to-the-point kind of article, with little to no explanation but with necessary links wherever needed.&lt;/p&gt;

&lt;p&gt;So, let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  How To Import?
&lt;/h3&gt;

&lt;p&gt;In Python importing a library can look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;math.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="c1"&gt;# OR
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;math.pow&lt;/span&gt;

&lt;span class="c1"&gt;# OR
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nb"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;

&lt;span class="c1"&gt;# OR
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;squareRoot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Scala importing a library can look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.math._&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.math.BigInt&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.math.&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.math.&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;BigInt&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BInt&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How To Declare Variables?
&lt;/h3&gt;

&lt;p&gt;Python offers dynamic typing so you don't need to declare variables beforehand like in C/C++, but you can just type out the variables and use them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;some_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scala also offers dynamic typing to some extent and infer that type during compile time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;some_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;  &lt;span class="c1"&gt;// infers it as Int type&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How To Define Conditionals?
&lt;/h3&gt;

&lt;p&gt;In Python and Scala defining conditionals are more or less similar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Odd"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Zero"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Even"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Scala:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Odd"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Zero"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Even"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important thing to note in conditionals is that in Python conditionals are referred to as statements whereas in Scala these are expressions that evaluates to something or nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How To Define Loops?
&lt;/h3&gt;

&lt;p&gt;In Python a simple for-loop would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# do something here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same for-loop in Scala would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;   &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference in their respective working behind the scenes, in Scala, is the use of conjunction of &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt; methods whereas in Python it is simple iteration.&lt;/p&gt;

&lt;p&gt;There is a &lt;code&gt;while&lt;/code&gt; loop in Scala, but let's not discuss that, while coding in Scala it is not encouraged to use &lt;code&gt;while&lt;/code&gt; loops.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/49552915/why-are-while-loops-not-recommended-in-scala#:~:text=The%20scala%20style%20checker%20says,%2Ddev.html%23org_scalastyle_scalariform_WhileChecker."&gt;Why are while loops not recommended in Scala?&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How To Write Methods?
&lt;/h3&gt;

&lt;p&gt;In Python, we can simply use the &lt;code&gt;def&lt;/code&gt; keyword to define functions, which are similar to methods in Scala.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_method&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# do something here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Scala we can define a method like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To define functions in Scala, we can use &lt;code&gt;Function&lt;/code&gt; objects or their respective syntactic sugars. To know more about functions and functional programming in Scala, do check &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-3-hep"&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How To Define Classes and Objects?
&lt;/h3&gt;

&lt;p&gt;In Python, we can define a class using the &lt;code&gt;class&lt;/code&gt; keyword, the same keyword can be used to define a class in Scala.&lt;/p&gt;

&lt;p&gt;Whereas objects in Scala are singleton instances and similar to what we call static class in other programming languages.&lt;/p&gt;

&lt;p&gt;A class however in Scala can be instantiated using the &lt;code&gt;new&lt;/code&gt; keyword, whereas in Python calling the class directly creates an instance for that.&lt;/p&gt;

&lt;p&gt;In Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# do something here
&lt;/span&gt;
&lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;     &lt;span class="c1"&gt;# creates an instance of class Student
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Scala:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;

&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;     &lt;span class="c1"&gt;// creates an instance of class Student&lt;/span&gt;

&lt;span class="c1"&gt;// use Object keyword for singleton objects&lt;/span&gt;

&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;

&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;aParticularStudent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To know more about classes and objects in Scala, I recommend checking out &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-2-39bj"&gt;this&lt;/a&gt; article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Miscellaneous
&lt;/h3&gt;

&lt;p&gt;Scala offers 2 more advantages over Python, namely &lt;strong&gt;traits&lt;/strong&gt; and &lt;strong&gt;tail recursion&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Python does not offer tail recursive capabilities to its functions and suffers from stack overflow errors whenever functional programming goes wrong. To know more about why python does not offer tail recursion, you can read &lt;a href="https://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html"&gt;this&lt;/a&gt; article. However, there is a work-around as described in &lt;a href="https://chrispenner.ca/posts/python-tail-recursion"&gt;this&lt;/a&gt; article using decorators.&lt;/p&gt;

&lt;p&gt;Similarly, Python does not have concepts like traits. A trait is used to describe the quality of a class, whereas in Python you would have to do multiple inheritances to get certain qualities or have to define certain methods within the class definition. Trait allows loose coupling.&lt;/p&gt;

&lt;p&gt;How to define a trait in Scala?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;SomeQuality&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;SomeQuality&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Well, that wraps up this simple Scala Vs Python syntax cheat sheet.&lt;/p&gt;

&lt;p&gt;What else would you add to this cheat sheet? Please let me know in the comments!&lt;/p&gt;

&lt;p&gt;If you are looking to sharpen your coding skills in Python or Scala, then do consider buying these awesome books that go in-depth to let you understand the semantics of the language.&lt;/p&gt;

&lt;p&gt;For Python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3k4sVmN"&gt;Python - The Bible&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3959lR9"&gt;Python - The Complete Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3hxhjqJ"&gt;Automate The Boring Stuff With Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3AcJgvc"&gt;Python Basics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Scala:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3C4rZ8a"&gt;Scala For The Impatient&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3tAluXH"&gt;Scala Programming Projects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3hnZYAw"&gt;Functional Programming in Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3k66Lk1"&gt;Programming Scala&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>bigdata</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scala For Beginners - Crash Course - Part 5</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 06:04:32 +0000</pubDate>
      <link>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-5-4j4o</link>
      <guid>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-5-4j4o</guid>
      <description>&lt;p&gt;Welcome to the 5&lt;sup&gt;th&lt;/sup&gt; and last part of the Scala beginners crash course, here we will go through the concepts like collections, sequences and map, flatmap, filter in the Scala programming language. This will be a short article, so if you need much more in-depth information you can check out the recommended books at the end of this article.&lt;/p&gt;

&lt;p&gt;It will be a no-nonsense, to the point kind of article (like &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt;, &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-2-39bj"&gt;part 2&lt;/a&gt;, &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-3-hep"&gt;part 3&lt;/a&gt; and &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-4-1a9b"&gt;part 4&lt;/a&gt; with all the necessary links wherever needed.&lt;/p&gt;

&lt;p&gt;Without any further ado, let's get started.&lt;/p&gt;




&lt;h3&gt;
  
  
  Before We Start
&lt;/h3&gt;

&lt;p&gt;Before we start coding out, first create a new object in the &lt;code&gt;crashcourse&lt;/code&gt; package (please go through &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt; for more information on creating packages and objects).&lt;/p&gt;

&lt;p&gt;Let's name it &lt;code&gt;Collections&lt;/code&gt; like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sYqSaTLh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531346691/e6CNxW3S3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sYqSaTLh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531346691/e6CNxW3S3.png" alt="collections.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can start coding out the examples on this Scala application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collections
&lt;/h3&gt;

&lt;p&gt;Scala offers 2 sets of collections, one is mutable and the other one is immutable. Scala by default works with immutable collection objects.&lt;/p&gt;

&lt;p&gt;But, we can import mutable collection objects whenever needed like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.collection.mutable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, if we want we can see all immutable collection objects in package &lt;code&gt;scala.collections.immutable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Following are some of the immutable collection objects provided in scala:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/HashSet.html"&gt;HashSet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/SortedSet.html"&gt;SortedSet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/Vector.html"&gt;Vector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/StringLike.html"&gt;StringLike&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/Range.html"&gt;Range&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/List.html"&gt;List&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/Stream.html"&gt;Stream&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/Stack.html"&gt;Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/Queue.html"&gt;Queue&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/HashMap.html"&gt;HasMap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/immutable/SortedMap.html"&gt;SortedMap&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similar collection objects are available in the mutable set. This &lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/mutable/index.html"&gt;link&lt;/a&gt; has all the mutable collection objects set.&lt;/p&gt;

&lt;p&gt;The commonality between mutable and immutable collection objects is that both extends trait &lt;a href="https://www.scala-lang.org/api/2.12.2/scala/collection/Traversable.html"&gt;Traversable&lt;/a&gt;. It is a base trait for all collections and offers a great variety of methods.&lt;/p&gt;

&lt;p&gt;Some of these collection objects are often sequenced in nature. Let's discuss sequences now.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sequences
&lt;/h3&gt;

&lt;p&gt;Sequences in scala are a general interface for data structures that have a well-defined order and can be indexed.&lt;/p&gt;

&lt;p&gt;These support various operations like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;iterator&lt;/code&gt;, &lt;code&gt;length&lt;/code&gt;, &lt;code&gt;reverse&lt;/code&gt; for indexing and iterating.&lt;/li&gt;
&lt;li&gt;Concatenation, appending, prepending.&lt;/li&gt;
&lt;li&gt;A lot of others - grouping, sorting, zipping, searching and slicing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see how to define a sequence:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w5CceOvr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531369496/8rbrbjIYW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w5CceOvr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531369496/8rbrbjIYW.png" alt="sequence.png" width="800" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bSZ0Td3O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531388160/8067-fcPJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bSZ0Td3O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531388160/8067-fcPJ.png" alt="sequence2.png" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Range&lt;/code&gt; is one of the sequences available, we can define a &lt;code&gt;Range&lt;/code&gt; like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5YwkYihS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531407959/iaYGwXKJ4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5YwkYihS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531407959/iaYGwXKJ4.png" alt="range.png" width="800" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wk0GlYze--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531425930/J_O_pCi5I.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wk0GlYze--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531425930/J_O_pCi5I.png" alt="range2.png" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is another type of sequence, &lt;code&gt;LinearSeq&lt;/code&gt; which is an immutable linked list with the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accessing head, tail and using methods like &lt;code&gt;isEmpty&lt;/code&gt; are fast and takes &lt;code&gt;O(1)&lt;/code&gt; time.&lt;/li&gt;
&lt;li&gt;Most other operations like &lt;code&gt;length&lt;/code&gt; or &lt;code&gt;reverse&lt;/code&gt; takes &lt;code&gt;O(n)&lt;/code&gt; time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Arrays in scala are equivalent to java arrays, these can be manually constructed with predefined lengths. These can be mutated, have fast indexing and are interoperable with java's arrays.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2MgTKf85--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531440888/_h4W3tUUF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2MgTKf85--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531440888/_h4W3tUUF.png" alt="array.png" width="800" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Vectors are also present in scala, these are the default implementation for immutable sequences taking constant time for indexed based read/write, with fast append/prepend, shows good performance for large size vectors and are implemented using a fixed branched trie data structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XOBT7srv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531455900/knizLYOg-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XOBT7srv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531455900/knizLYOg-.png" alt="vector.png" width="800" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tuples are finite ordered kinds of lists that can contain a maximum of 22 elements. A tuple value data type is defined similarly to a function like &lt;code&gt;TupleN[...]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--faxlEPf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531475585/fR2ZmAxRN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--faxlEPf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531475585/fR2ZmAxRN.png" alt="tuple.png" width="800" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is one more collection object, &lt;code&gt;Map&lt;/code&gt;, which is a key-&amp;gt;value pair data structure present in scala. Following are some ways to manipulate a map:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0UC68dRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531488699/EIud6p632.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0UC68dRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531488699/EIud6p632.png" alt="map.png" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YIJlECaB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531499216/dHUHeRySj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YIJlECaB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531499216/dHUHeRySj.png" alt="map2.png" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More on Scala collections can be found &lt;a href="https://docs.scala-lang.org/overviews/collections/overview.html"&gt;here&lt;/a&gt; or you can check the books provided in the last section for the more detailed in-depth working of collections.&lt;/p&gt;

&lt;h3&gt;
  
  
  map, flatMap, filter and for
&lt;/h3&gt;

&lt;p&gt;Finally, on the most important part of functional programming, that would become the daily need of a Scala programmer. I wanted to include these in the previous &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-4-1a9b"&gt;part&lt;/a&gt; but to show how these work I needed to show collections in scala first, now that we know enough about collections, let's move on to these functions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: For the sake of examples, I will be showing these operations on the &lt;code&gt;List&lt;/code&gt; sequence, you can try these on other collections as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, define our list collection like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xpaDMCRI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531520453/GABMEV5EE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xpaDMCRI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531520453/GABMEV5EE.png" alt="list.png" width="800" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the &lt;code&gt;map&lt;/code&gt; method? It allows a certain function to be applied to all the elements present in the given sequence and evaluates to a sequence of updated values.&lt;/p&gt;

&lt;p&gt;What is the &lt;code&gt;filter&lt;/code&gt; method? It filters out the elements which do not meet a certain criterion defined as another function and evaluates to a sequence of filtered values.&lt;/p&gt;

&lt;p&gt;What is the &lt;code&gt;flatMap&lt;/code&gt; method? This is identical to the &lt;code&gt;map&lt;/code&gt; method, but the only difference is that in &lt;code&gt;flatMap&lt;/code&gt; the inner grouping of an item is removed and a sequence is generated. So, if &lt;code&gt;flatMap&lt;/code&gt; is applied to a list then the generated output will be a list of lists, changing the inner grouping of integer elements to a list and later flattening it.&lt;/p&gt;

&lt;p&gt;What is the &lt;code&gt;for&lt;/code&gt; loop in scala? Yeah, you read it right 'What is' and not 'How to', in scala, writing a &lt;code&gt;for&lt;/code&gt; loop is just a fancy way of writing a coupled &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt; methods. A &lt;code&gt;for&lt;/code&gt; loop in scala can be written as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o4Ue7ZYF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531539746/H2GaxC-s3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o4Ue7ZYF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531539746/H2GaxC-s3.png" alt="mapfilter.png" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZD9Aooj6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531550914/FvTwmpzad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZD9Aooj6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1631531550914/FvTwmpzad.png" alt="mapfilter2.png" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are some important methods to remember while coding out in scala.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Well, that wraps up part 5 and the end of this crash course.&lt;/p&gt;

&lt;p&gt;I hope it was something worth it for you as a reader and made you a scala enthusiast at the end of this course.&lt;/p&gt;

&lt;p&gt;You can pick up any of the below books to master the art of programming in scala:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3C4rZ8a"&gt;Scala For The Impatient&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3tAluXH"&gt;Scala Programming Projects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3hnZYAw"&gt;Functional Programming in Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3k66Lk1"&gt;Programming Scala&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To keep in touch, you can follow me on here or subscribe to get the updates of the new blogs that I will write in the future or follow me on &lt;a href="https://twitter.com/chandrajidev"&gt;Twitter&lt;/a&gt; where I share my thoughts not only on programming but also on personal finance.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>bigdata</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scala For Beginners - Crash Course - Part 4</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 06:01:37 +0000</pubDate>
      <link>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-4-1a9b</link>
      <guid>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-4-1a9b</guid>
      <description>&lt;p&gt;Welcome to the 4&lt;sup&gt;th&lt;/sup&gt; part of the scala beginners crash course, here we will go through the concepts like options, exception handling and pattern matching in the scala programming language. This will be a short article, so if you need much more in-depth information you can check out the recommended books at the end of this article.&lt;/p&gt;

&lt;p&gt;It will be a no-nonsense, to the point kind of article (like &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt;, &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-2-39bj"&gt;part 2&lt;/a&gt; and &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-3-hep"&gt;part 3&lt;/a&gt;) with all the necessary links wherever needed.&lt;/p&gt;

&lt;p&gt;Without further ado, let's get started.&lt;/p&gt;




&lt;h3&gt;
  
  
  Before We Start
&lt;/h3&gt;

&lt;p&gt;Before we start coding out, first create a new object in the &lt;code&gt;crashcourse&lt;/code&gt; package (please go through &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt; for more information on creating packages and objects).&lt;/p&gt;

&lt;p&gt;Let's name it &lt;code&gt;OptionErrorPattern&lt;/code&gt; like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fx6-n19A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925030021/nVd9E9gnC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fx6-n19A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925030021/nVd9E9gnC.png" alt="optionerrorpattern.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can start coding out examples on this scala application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Options
&lt;/h3&gt;

&lt;p&gt;An option is a wrapper for a value that might be present or not.&lt;/p&gt;

&lt;p&gt;As the name suggests it evaluates to an option of 2 values, either &lt;code&gt;Some&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;, when given an expression. In this way, when we are dealing with some unsafe APIs, we can always wrap that API calls within an &lt;code&gt;Option&lt;/code&gt; object to keep our program safe and error-free that might otherwise result in a &lt;code&gt;Null&lt;/code&gt; pointer kind of exception.&lt;/p&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GaLUVr87--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925018987/sB1uRqqay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GaLUVr87--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925018987/sB1uRqqay.png" alt="option1.png" width="800" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mpDdLSKz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925042862/-nK_ZfcMc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mpDdLSKz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925042862/-nK_ZfcMc.png" alt="option2.png" width="800" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Some&lt;/code&gt; wraps a concrete value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;None&lt;/code&gt; is a singleton for absent values.&lt;/li&gt;
&lt;li&gt;It is better to use &lt;code&gt;Option&lt;/code&gt; in APIs that we are creating.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Handling Exceptions
&lt;/h3&gt;

&lt;p&gt;In scala, exceptions are handled inside try-catch blocks, like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QduSR6TA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925058053/hTqvEBrrBP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QduSR6TA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925058053/hTqvEBrrBP.png" alt="trycatch1.png" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gVvww5k5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925069861/Esd5kD21N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gVvww5k5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925069861/Esd5kD21N.png" alt="trycatch2.png" width="800" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, it comes with its drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple/Nested try-catch blocks make the code hard to read/follow.&lt;/li&gt;
&lt;li&gt;We can not chain multiple operations prone to failure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To overcome these limitations, scala introduced a &lt;code&gt;Try&lt;/code&gt; object that can act as a wrapper to wrap an error-prone expression and evaluates to either a &lt;code&gt;Success&lt;/code&gt; or a &lt;code&gt;Failure&lt;/code&gt; as a result.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Try&lt;/code&gt; is a wrapper for a computation that might fail or not. It wraps failed computations as &lt;code&gt;Failure&lt;/code&gt; and successful computations as &lt;code&gt;Success&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;Try&lt;/code&gt;, &lt;code&gt;Success&lt;/code&gt; and &lt;code&gt;Failure&lt;/code&gt; objects, we have to import these first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;scala.util.&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;Try&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Failure&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the above method &lt;code&gt;someErrorProneFunc&lt;/code&gt; can be used with &lt;code&gt;Try&lt;/code&gt; as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZZylanl3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925084602/6lmVG7jIU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZZylanl3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925084602/6lmVG7jIU.png" alt="try1.png" width="800" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GhKBjY_h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925093846/jtYL6zcZR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GhKBjY_h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925093846/jtYL6zcZR.png" alt="try2.png" width="800" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can check if a &lt;code&gt;Try&lt;/code&gt; is a &lt;code&gt;Success&lt;/code&gt; or &lt;code&gt;Failure&lt;/code&gt; using &lt;code&gt;isSuccess&lt;/code&gt; or &lt;code&gt;isFailure&lt;/code&gt; methods respectively.&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;Try&lt;/code&gt; to handle exceptions for avoiding runtime crashes due to uncaught exceptions or just to avoid an endless amount of try-catch blocks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pattern Matching
&lt;/h3&gt;

&lt;p&gt;Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the &lt;code&gt;switch&lt;/code&gt; statement in Java and it can likewise be used in place of a series of if/else statements.&lt;/p&gt;

&lt;p&gt;A match expression has a value, the &lt;code&gt;match&lt;/code&gt; keyword, and at least one &lt;code&gt;case&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3bitHkOI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925111421/1pCDtmdXV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3bitHkOI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925111421/1pCDtmdXV.png" alt="pattern1.png" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4nfro5iQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925120830/nW6vRfMZ8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4nfro5iQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630925120830/nW6vRfMZ8.png" alt="pattern2.png" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some important points to note on pattern matching:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is used to decompose values.&lt;/li&gt;
&lt;li&gt;Cases are matched in the order given.&lt;/li&gt;
&lt;li&gt;What if there was no case that matched the pattern, then it would result in &lt;code&gt;MatchError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Pattern matching works really well with case classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Well, that wraps up part 4 of this crash course.&lt;/p&gt;

&lt;p&gt;I know, it was a very short read, my motivation was to keep it short and simple and be able to provide ample impetus to you to explore it further. &lt;/p&gt;

&lt;p&gt;Take it as a challenge or homework, explore advanced pattern matching, how you can use &lt;code&gt;Option&lt;/code&gt; in conjunction with &lt;code&gt;Try&lt;/code&gt; (it can do wonders to the API you are working on) and in the comments, mention what you know about on these concepts.&lt;/p&gt;

&lt;p&gt;You can check these books to get more insights on these topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3C4rZ8a"&gt;Scala For The Impatient&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3tAluXH"&gt;Scala Programming Projects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3hnZYAw"&gt;Functional Programming in Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://amzn.to/3k66Lk1"&gt;Programming Scala&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next and last part of this crash course, we will discuss the collections available in scala and most important of all, the map, flatmap and filter methods in scala.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>bigdata</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scala For Beginners - Crash Course - Part 3</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 05:56:43 +0000</pubDate>
      <link>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-3-hep</link>
      <guid>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-3-hep</guid>
      <description>&lt;p&gt;Welcome to the 3&lt;sup&gt;rd&lt;/sup&gt; part of the scala beginners crash course, here we will go through the functional way of scala programming language and concepts like higher-order functions, curries and anonymous functions!&lt;/p&gt;

&lt;p&gt;It will be a no-nonsense, to the point kind of article (like &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt; and &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-2-39bj"&gt;part 2&lt;/a&gt;) with all necessary links wherever needed.&lt;/p&gt;

&lt;p&gt;Without further ado, let's get started.&lt;/p&gt;




&lt;h3&gt;
  
  
  What Is Functional Programming?
&lt;/h3&gt;

&lt;p&gt;The below is an excerpt from the &lt;strong&gt;Functional Programming in Scala&lt;/strong&gt; book.&lt;/p&gt;

&lt;p&gt;Functional programming (FP) is based on a simple premise with far-reaching implications: we construct our programs using only pure functions, in other words, functions that have no side effects. What are the side effects? A function has a side effect if it does something other than simply return a result, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modifying a variable.&lt;/li&gt;
&lt;li&gt;Modifying a data structure in place.&lt;/li&gt;
&lt;li&gt;Setting a field on an object.&lt;/li&gt;
&lt;li&gt;Throwing an exception or halting with an error.&lt;/li&gt;
&lt;li&gt;Printing to the console or reading user input.&lt;/li&gt;
&lt;li&gt;Reading from or writing to a file.&lt;/li&gt;
&lt;li&gt;Drawing on the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We discussed side-effects also in &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Functional programming is a restriction on how we write programs, but not on what programs we can express.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Before We Start
&lt;/h3&gt;

&lt;p&gt;Before we start coding out, first create a new object in the &lt;code&gt;crashcourse&lt;/code&gt; package (please go through &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt; for more information on creating packages and objects).&lt;/p&gt;

&lt;p&gt;Let's name it &lt;code&gt;FunctionalProgramming&lt;/code&gt; like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bloYOzIc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575945646/OC1gaLVh2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bloYOzIc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575945646/OC1gaLVh2.png" alt="functionalprog.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can start coding out examples on this scala application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Functions In Scala
&lt;/h3&gt;

&lt;p&gt;Before moving on to create functions in scala, please keep in mind that scala works on JVM and for JVM it needs classes/objects to work with, hence everything in scala will be an object, even the functions are objects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Due to scala's syntactic sugar, we can still write functions in a more functional fashion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's create a function in scala:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DwT0qQ4---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575959941/pcZ5h_BWX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DwT0qQ4---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575959941/pcZ5h_BWX.png" alt="function1.png" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Scala has a collection of functions namely &lt;code&gt;Function1&lt;/code&gt; to &lt;code&gt;Function22&lt;/code&gt;, which can be used out-of-the-box without any issues.&lt;/p&gt;

&lt;p&gt;A function like &lt;code&gt;Function1[String, Int]&lt;/code&gt; represents that it will take &lt;code&gt;String&lt;/code&gt; as an input and evaluates to &lt;code&gt;Int&lt;/code&gt; as an output. This function definition will be written in its &lt;code&gt;apply&lt;/code&gt; method, as &lt;code&gt;Function1&lt;/code&gt; is an object and we need to override its &lt;code&gt;apply&lt;/code&gt; method to make it callable.&lt;/p&gt;

&lt;p&gt;A simple syntactic sugar for a function like &lt;code&gt;Function2[Int, Int, Int]&lt;/code&gt; would be &lt;code&gt;(Int, Int) =&amp;gt; Int&lt;/code&gt;, which is more functional while reading the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Stms0-CS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575972248/l-jeLp22Hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Stms0-CS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575972248/l-jeLp22Hj.png" alt="function2.png" width="800" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We should also note that while working in IntelliJ, it tells us that the given &lt;code&gt;Function1[String, Int]&lt;/code&gt; can be converted to its respective syntactic sugar, we can do so by hovering over the yellow line and simply clicking &lt;code&gt;Replace FunctionN[A1, A1, ..., AN, R] with (A1, A1, ..., AN) =&amp;gt; R&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v1hQAZbm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575985895/CLRREEfBc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v1hQAZbm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575985895/CLRREEfBc.png" alt="function1yellow.png" width="800" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Converted to syntactic sugar:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PqIui7R8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575997793/1s4T2rLe-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PqIui7R8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630575997793/1s4T2rLe-.png" alt="function1syntax.png" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Important points to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All scala functions are objects, that is, instances of &lt;code&gt;Function1&lt;/code&gt;, &lt;code&gt;Function2&lt;/code&gt; and so on.&lt;/li&gt;
&lt;li&gt;JVM was designed for object-oriented programming, but using it for scala function we need something like objects (like &lt;code&gt;Function1&lt;/code&gt;) to instantiate them.&lt;/li&gt;
&lt;li&gt;Functions are traits with maximum parameters to be 22.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Anonymous Functions
&lt;/h3&gt;

&lt;p&gt;The anonymous function is something we saw in the previous example of &lt;code&gt;Function2[Int, Int, Int]&lt;/code&gt; syntactic sugar.&lt;/p&gt;

&lt;p&gt;So, the below expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oZ4hREUj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576036088/edN80uOpx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oZ4hREUj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576036088/edN80uOpx.png" alt="anonfunc.png" width="800" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;can be written as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1rr8zGR5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576046885/Pqc562saY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1rr8zGR5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576046885/Pqc562saY.png" alt="anonfunc2.png" width="800" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;or as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0tIS7JLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576059186/SLHmwCK82r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0tIS7JLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576059186/SLHmwCK82r.png" alt="anonfunc3.png" width="800" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anonymous functions are just another way of writing functions as supported by scala's compiler, behind the scenes everything is the same.&lt;/p&gt;

&lt;p&gt;More ways in which we can write anonymous functions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rfwmybBG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576079593/k_WNsN_7p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rfwmybBG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576079593/k_WNsN_7p.png" alt="anonfunc4.png" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: An anonymous function is also known as a function literal. A function that does not contain a name is known as an anonymous function in general.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Higher-Order Functions and Curries
&lt;/h3&gt;

&lt;p&gt;Higher-order functions have at least one of the following properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Takes one or more functions as parameters.&lt;/li&gt;
&lt;li&gt;Returns a function as a result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In scala, a general higher-order function declaration can look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2EW9y8_R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576093034/_ocWhLD0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2EW9y8_R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576093034/_ocWhLD0h.png" alt="hof.png" width="800" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are called Higher-Order Functions, which essentially takes one or more functions as input and/or returns a function as output.&lt;/p&gt;

&lt;p&gt;Moving on to curries, currying means transforming a function that takes multiple arguments into a chain of calls to functions, each of which takes one argument. Each function returns another function that takes the subsequent argument.&lt;/p&gt;

&lt;p&gt;A simple curry can be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wv_id7RZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576108908/ZoNJdX16A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wv_id7RZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576108908/ZoNJdX16A.png" alt="curry.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Functions with multiple parameter list also act as curries:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x-YypUlS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576119542/o5ECg8pcP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x-YypUlS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630576119542/o5ECg8pcP.png" alt="curry2.png" width="800" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Well, that wraps up part 3 of this crash course.&lt;/p&gt;

&lt;p&gt;I highly recommend you to go through the books below to get a better and deeper understanding of functional programming:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.in/Functional-Thinking-Paradigm-Over-Syntax-ebook/dp/B00LEX6SP8?crid=2I35UGEUXLTHJ&amp;amp;dchild=1&amp;amp;keywords=functional+thinking&amp;amp;qid=1630574749&amp;amp;sprefix=functional+think%2Caps%2C317&amp;amp;sr=8-1&amp;amp;linkCode=li2&amp;amp;tag=chandrajidev-21&amp;amp;linkId=576c0c2c9e91856ea455b7baa167cbe1&amp;amp;language=en_IN&amp;amp;ref_=as_li_ss_il"&gt;&lt;img src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;amp;ASIN=B00LEX6SP8&amp;amp;Format=_SL160_&amp;amp;ID=AsinImage&amp;amp;MarketPlace=IN&amp;amp;ServiceVersion=20070822&amp;amp;WS=1&amp;amp;tag=chandrajidev-21&amp;amp;language=en_IN"&gt;&lt;/a&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XdU3ZF5N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://ir-in.amazon-adsystem.com/e/ir%3Ft%3Dchandrajidev-21%26language%3Den_IN%26l%3Dli2%26o%3D31%26a%3DB00LEX6SP8" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XdU3ZF5N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://ir-in.amazon-adsystem.com/e/ir%3Ft%3Dchandrajidev-21%26language%3Den_IN%26l%3Dli2%26o%3D31%26a%3DB00LEX6SP8" alt="" width="1" height="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.in/Grokking-Simplicity-software-functional-thinking-ebook/dp/B09781TWFL?dchild=1&amp;amp;keywords=functional+thinking&amp;amp;qid=1630574798&amp;amp;sr=8-2&amp;amp;linkCode=li2&amp;amp;tag=chandrajidev-21&amp;amp;linkId=54fce22f0619670582a2391a0daf4e0b&amp;amp;language=en_IN&amp;amp;ref_=as_li_ss_il"&gt;&lt;img src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;amp;ASIN=B09781TWFL&amp;amp;Format=_SL160_&amp;amp;ID=AsinImage&amp;amp;MarketPlace=IN&amp;amp;ServiceVersion=20070822&amp;amp;WS=1&amp;amp;tag=chandrajidev-21&amp;amp;language=en_IN"&gt;&lt;/a&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BGL9_nU3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://ir-in.amazon-adsystem.com/e/ir%3Ft%3Dchandrajidev-21%26language%3Den_IN%26l%3Dli2%26o%3D31%26a%3DB09781TWFL" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BGL9_nU3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://ir-in.amazon-adsystem.com/e/ir%3Ft%3Dchandrajidev-21%26language%3Den_IN%26l%3Dli2%26o%3D31%26a%3DB09781TWFL" alt="" width="1" height="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.in/Functional-Programming-Scala-Paul-Chiusano/dp/1617290653?crid=24RA56XW8MZX1&amp;amp;dchild=1&amp;amp;keywords=functional+programming+in+scala&amp;amp;qid=1630574828&amp;amp;sprefix=functional+prog%2Caps%2C322&amp;amp;sr=8-1&amp;amp;linkCode=li2&amp;amp;tag=chandrajidev-21&amp;amp;linkId=8928ebdaa8126f751f76a0612beeac4f&amp;amp;language=en_IN&amp;amp;ref_=as_li_ss_il"&gt;&lt;img src="//ws-in.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;amp;ASIN=1617290653&amp;amp;Format=_SL160_&amp;amp;ID=AsinImage&amp;amp;MarketPlace=IN&amp;amp;ServiceVersion=20070822&amp;amp;WS=1&amp;amp;tag=chandrajidev-21&amp;amp;language=en_IN"&gt;&lt;/a&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KDm1R0KY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://ir-in.amazon-adsystem.com/e/ir%3Ft%3Dchandrajidev-21%26language%3Den_IN%26l%3Dli2%26o%3D31%26a%3D1617290653" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KDm1R0KY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://ir-in.amazon-adsystem.com/e/ir%3Ft%3Dchandrajidev-21%26language%3Den_IN%26l%3Dli2%26o%3D31%26a%3D1617290653" alt="" width="1" height="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next part, we will discuss some topics specific to scala, like options, handling errors in a unique style and pattern matching (the most awesome thing in scala).&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>bigdata</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scala For Beginners - Crash Course - Part 2</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 29 Dec 2021 05:52:25 +0000</pubDate>
      <link>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-2-39bj</link>
      <guid>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-2-39bj</guid>
      <description>&lt;p&gt;Welcome to the 2&lt;sup&gt;nd&lt;/sup&gt; part of scala beginners crash course, here we will go through the object-oriented way of scala programming language and concepts like anonymous class, case classes and traits!&lt;/p&gt;

&lt;p&gt;It will be a no-nonsense, to the point kind of article (like &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt;) with all necessary links wherever needed.&lt;/p&gt;

&lt;p&gt;Without further ado, let's get started.&lt;/p&gt;




&lt;h3&gt;
  
  
  Before We Start
&lt;/h3&gt;

&lt;p&gt;Before we start coding out, first create a new object in &lt;code&gt;crashcourse&lt;/code&gt; package (please go through &lt;a href="https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj"&gt;part 1&lt;/a&gt; for more information on creating packages and objects).&lt;/p&gt;

&lt;p&gt;Let's name it &lt;code&gt;ClassAndObject&lt;/code&gt; like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0HMTsTW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428192859/oxQvO6f-0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0HMTsTW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428192859/oxQvO6f-0.png" alt="classandobject.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we can start coding out examples on this scala application.&lt;/p&gt;




&lt;h3&gt;
  
  
  Classes And Objects
&lt;/h3&gt;

&lt;p&gt;In scala, to define a class we use the &lt;code&gt;class&lt;/code&gt; keyword:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Plp0UHcD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428208513/-YBKewovA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Plp0UHcD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428208513/-YBKewovA.png" alt="classkeyword.png" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can directly use the &lt;code&gt;Student&lt;/code&gt; class later in the program. That's it! that's how we can create a class in scala.&lt;/p&gt;

&lt;p&gt;It is different from writing &lt;code&gt;object Student&lt;/code&gt; as it is not instantiated right away, we have just defined it and are not using it anywhere. To instantiate a class we can use the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6kU6vgnk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428230819/rKcOjejYv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6kU6vgnk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428230819/rKcOjejYv.png" alt="newkeyword.png" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A class can have parameters, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rOGIITfK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428242656/aO-thvn6J.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rOGIITfK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428242656/aO-thvn6J.png" alt="classargs.png" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Class parameters are not class fields, which means we can not use these parameters with instantiated objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To make class parameters as class fields, we have to use the &lt;code&gt;val&lt;/code&gt; keyword along with the name of the parameter, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8K8brxK8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428319579/tsxrdzViI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8K8brxK8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428319579/tsxrdzViI.png" alt="valclassargs.png" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use class parameters within the class code block by using the &lt;code&gt;this&lt;/code&gt; keyword if value name conflict occurs, like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0n9LXAfd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428339539/wdMAz_16a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0n9LXAfd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428339539/wdMAz_16a.png" alt="usingthis.png" width="800" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If value name conflict does not occur, then the class method by default (implicitly) uses the &lt;code&gt;this&lt;/code&gt; operator on class parameters values.&lt;/p&gt;

&lt;p&gt;Some other important concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Overloading occurs with only different signature methods. More on overloading in scala &lt;a href="https://www.geeksforgeeks.org/method-overloading-in-scala/"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Auxiliary constructor are generally used for default parameters. More on auxiliary constructor &lt;a href="https://www.geeksforgeeks.org/scala-auxiliary-constructor/"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Instances are fixed, whenever we want to do something with the given class, say creating/updating something, then we would need to instantiate a new class, it is the clause of immutability, useful in functional programming.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Conceptually, scala does not provide class level functionality, like a static class. However, scala provides something called an &lt;code&gt;object&lt;/code&gt;, using which we can obtain class level functionalities.&lt;/p&gt;

&lt;p&gt;Scala objects are singleton instances by definition. We can create an object like the below code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XwJ7ap1---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428399034/tswQeZPm90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XwJ7ap1---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428399034/tswQeZPm90.png" alt="object.png" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can create an object with the same name as that of a class in the same scope, creating such an object/class is called companion object/class, known as companions.&lt;/p&gt;

&lt;p&gt;Companions can be used to make a factory method in objects, that can help instantiate a class in a certain way. Using companions does not require using the &lt;code&gt;new&lt;/code&gt; keyword to instantiate a scala class, we can define a method named &lt;code&gt;apply&lt;/code&gt; within the object to handle instantiation.&lt;/p&gt;

&lt;p&gt;We can create a companion like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MQtGKdCi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428637173/s90e0_FRe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MQtGKdCi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428637173/s90e0_FRe.png" alt="companion.png" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, that's about it on classes and objects in scala. You can check out official docs on scala classes &lt;a href="https://docs.scala-lang.org/tour/classes.html"&gt;here&lt;/a&gt; and on objects &lt;a href="https://docs.scala-lang.org/tour/singleton-objects.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Anonymous And Case Classes
&lt;/h3&gt;

&lt;p&gt;To create an anonymous class we can directly define the class like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VTLbDpon--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428653602/AuZMDeqQt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VTLbDpon--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428653602/AuZMDeqQt.png" alt="anonclass.png" width="800" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Things to remember when creating an anonymous class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pass proper parameters whenever required.&lt;/li&gt;
&lt;li&gt;Anonymous class declaration works for abstract as well as for non-abstract classes. It works for traits as well.
&amp;gt; Note: We will discuss abstract classes and traits in the next section.&lt;/li&gt;
&lt;li&gt;Useful for on-spot declaration in some scenarios.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the &lt;code&gt;case&lt;/code&gt; keyword in front of the &lt;code&gt;class&lt;/code&gt; keyword will make it a case class, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LN0C0KE_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428673599/WBzmt4Qjo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LN0C0KE_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428673599/WBzmt4Qjo.png" alt="caseclass.png" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Case classes promote class parameters to fields, it has a predefined &lt;code&gt;toString&lt;/code&gt; method that evaluates to a representation that is understandable.&lt;/p&gt;

&lt;p&gt;Equality and hashcodes are implemented out-of-the-box with a handy &lt;code&gt;copy&lt;/code&gt; method to copy instances. Case classes come with their companion objects predefined.&lt;/p&gt;

&lt;p&gt;Things to remember when creating a case class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Case class is a quick lightweight data structure with little boilerplate.&lt;/li&gt;
&lt;li&gt;Case classes are serializable and used with the Akka framework.&lt;/li&gt;
&lt;li&gt;Case classes have extractor patterns that can be used in pattern matching.&lt;/li&gt;
&lt;li&gt;Case objects are the same as case classes but don't get companion objects as they themselves are objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, that's about it on anonymous class and case class in scala.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inheritance, Abstract Class And Traits
&lt;/h3&gt;

&lt;p&gt;Scala offers single level inheritance. Inheriting a class inherits public methods and protected methods can be used within the child class.&lt;/p&gt;

&lt;p&gt;Inheritance example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uAA1peae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428695980/LGFCROTuq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uAA1peae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428695980/LGFCROTuq.png" alt="inheritance.png" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Important notes on inheritance overriding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To override methods, use the &lt;code&gt;override&lt;/code&gt; keyword. Override works for &lt;code&gt;val&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;def&lt;/code&gt; or anything within in the class.&lt;/li&gt;
&lt;li&gt;To prevent override, we have to use the &lt;code&gt;final&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;Put a &lt;code&gt;final&lt;/code&gt; keyword before &lt;code&gt;class&lt;/code&gt; to altogether prevent the inheritance of a class.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;sealed&lt;/code&gt; keyword to restrict inheritance in different scopes of the program.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moving on to abstract classes in scala, we have to use the &lt;code&gt;abstract&lt;/code&gt; keyword to make an abstract class and an abstract class cannot be instantiated, however, we can create an anonymous class using the &lt;code&gt;new&lt;/code&gt; keyword and defining all the methods within that anonymous code block.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0Y2YUua8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428722924/REIHImy5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0Y2YUua8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428722924/REIHImy5p.png" alt="abstractclass.png" width="800" height="96"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Traits are similar to interfaces in java. They are also used to declare undefined or unimplemented methods. We have to use the &lt;code&gt;trait&lt;/code&gt; keyword to define traits in scala.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w0ieGzLv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428738822/-Gt2CDPkh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w0ieGzLv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1630428738822/-Gt2CDPkh.png" alt="trait.png" width="800" height="77"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Important points to note for abstract classes and traits in scala:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Abstract class can have both abstract and non-abstract methods. It is the same for traits.&lt;/li&gt;
&lt;li&gt;Traits does not have constructor parameters.&lt;/li&gt;
&lt;li&gt;Classes can inherit multiple traits, but not multiple classes.&lt;/li&gt;
&lt;li&gt;Traits are used to define the behaviour of a class.&lt;/li&gt;
&lt;li&gt;Abstract class is the type of thing itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you see &lt;code&gt;???&lt;/code&gt; in abstract/trait methods, it means that method is not implemented and evaluates to &lt;code&gt;Nothing&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, that's about it on inheritance, abstract class and traits in scala.&lt;/p&gt;

&lt;h3&gt;
  
  
  External Links
&lt;/h3&gt;

&lt;p&gt;Below are some of the external links you can go through to get a more in-depth understanding of object-oriented programming in scala.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.baeldung.com/scala/oop-intro#:~:text=Scala%20is%20a%20hybrid%20between,encapsulation%2C%20inheritance%2C%20and%20polymorphism"&gt;Object Oriented Programming in Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.scala-exercises.org/scala_tutorial/object_oriented_programming"&gt;Scala Exercises&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Well, that wraps up part 2 of this crash course.&lt;/p&gt;

&lt;p&gt;In the next part, we will discuss functions, HOFs, curries and much more related to functional programming in scala.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from &lt;a href="https://dev.toseries/python"&gt;Python&lt;/a&gt; to &lt;a href="https://dev.toseries/computer-vision"&gt;Computer Vision&lt;/a&gt; to &lt;a href="https://dev.toseries/scala"&gt;Scala&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>opensource</category>
      <category>bigdata</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scala For Beginners - Crash Course - Part 1</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Mon, 24 May 2021 05:53:25 +0000</pubDate>
      <link>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj</link>
      <guid>https://dev.to/siddharth2016/scala-for-beginners-crash-course-part-1-cgj</guid>
      <description>&lt;p&gt;Welcome to this scala beginners crash course, here we will go through the basic syntax of scala programming language and some of the nuances available just for scala developers!&lt;/p&gt;

&lt;p&gt;It will be a no-nonsense, to the point kind of article with all necessary links wherever needed.&lt;/p&gt;

&lt;p&gt;Without further ado, let's get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is Scala?
&lt;/h3&gt;

&lt;p&gt;Scala is a programming language just like any other that is available out there but with a lot of sweetness and awesome features that opens our world to functional programming with easy to understand syntax, running on JVM under the hood, having all functionality that Java language offers with popularity ranging from being used in major FinTech companies to FAANG companies and as one of the major language for data engineering applications.&lt;/p&gt;

&lt;p&gt;Need to know more about what is Scala, visit &lt;a href="https://www.scala-lang.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How To Install Scala?
&lt;/h3&gt;

&lt;p&gt;Well, there are a couple of ways, in which the shortest and easy to follow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install IntelliJ IDE from JetBrains. Check &lt;a href="https://www.jetbrains.com/idea/download/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;IntelliJ IDEA | Preferences&lt;/code&gt; for macOS ( Ctrl+Alt+S ) or &lt;code&gt;File | Settings&lt;/code&gt; for Windows and Linux.&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;Plugins&lt;/code&gt; on the left pane.&lt;/li&gt;
&lt;li&gt;Search for &lt;code&gt;Scala&lt;/code&gt;, install the plugin and restart IDE.&lt;/li&gt;
&lt;li&gt;Now, after it restarts, select &lt;code&gt;New Project&lt;/code&gt; and then &lt;code&gt;Scala&lt;/code&gt; -&amp;gt; &lt;code&gt;IDEA&lt;/code&gt; project, click &lt;code&gt;Next&lt;/code&gt;, set project name and your project folder are ready with necessary scala features.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697251647%2FlECjG6zCA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697251647%2FlECjG6zCA.png" alt="install_scala.png"&gt;&lt;/a&gt;&lt;br&gt;
Installing Scala&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697268678%2FdtXJwQfg8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697268678%2FdtXJwQfg8.png" alt="setup_project.png"&gt;&lt;/a&gt;&lt;br&gt;
Creating a New Project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697291997%2FGWDVd0_-X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697291997%2FGWDVd0_-X.png" alt="project_name.png"&gt;&lt;/a&gt;&lt;br&gt;
Set Suitable Project Name&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How To Start Working With Scala?
&lt;/h3&gt;

&lt;p&gt;After successful installation and creating a scala project, you would be seeing a nice project structure with &lt;code&gt;src&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;We will create a package, inside that package we will continue our work. &lt;a href="https://docs.oracle.com/javase/tutorial/java/concepts/package.html" rel="noopener noreferrer"&gt;What is a package?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To create a package, right-click on &lt;code&gt;src&lt;/code&gt; folder and select &lt;code&gt;New | Package&lt;/code&gt; type, then fill in a name, whatever you like, for our case, let's name it &lt;code&gt;crashcourse&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697312670%2FNxpfJgmX3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697312670%2FNxpfJgmX3.png" alt="create_package.png"&gt;&lt;/a&gt;&lt;br&gt;
Create New Package&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, right-click the &lt;code&gt;crashcourse&lt;/code&gt; then &lt;code&gt;New | Scala&lt;/code&gt;, here we are creating a scala object (we will discuss what scala object is in a minute). Let's name it &lt;code&gt;BeginnersCrashCourse&lt;/code&gt;. It will create a standard template of scala object like below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697340573%2F3icKtWx0Z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697340573%2F3icKtWx0Z.png" alt="object_creation.png"&gt;&lt;/a&gt;&lt;br&gt;
Select Scala Class&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697358744%2FE9Gci-BRM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621697358744%2FE9Gci-BRM.png" alt="select_object.png"&gt;&lt;/a&gt;&lt;br&gt;
Select Scala Object&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, now we have all we need to get started with some code!&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is An Object?
&lt;/h3&gt;

&lt;p&gt;Before moving to object, if you notice the created template have &lt;code&gt;package crashcourse&lt;/code&gt; at the top, it means that whatever code we are going to write below this, will belong to this package and can be accessed by any code file within the same package or can be imported using &lt;code&gt;import crashcourse._&lt;/code&gt; in some other package. &lt;a href="https://docs.scala-lang.org/tour/packages-and-imports.html" rel="noopener noreferrer"&gt;How to do imports in scala?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, what is object in scala?&lt;/p&gt;

&lt;p&gt;Object is a class that has exactly one instance, it means it already is an instance with a defined class structure. What is the defined class structure for an object type? It is everything we write inside scala object.&lt;/p&gt;

&lt;p&gt;Scala objects are singleton, which means if we say we have an object named &lt;code&gt;BeginnersCrashCourse&lt;/code&gt; then it is an instance that is a singleton in nature and whenever I am accessing it in the same executing program then its behaviour is going to be the same at every place, containing same values, methods or functions.&lt;/p&gt;

&lt;p&gt;Treat an object as something that has static class level functionality.&lt;/p&gt;

&lt;p&gt;How to write an object? Simple, just use &lt;code&gt;object&lt;/code&gt; keyword before writing an object, like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;crashcourse&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;BeginnersCrashCourse&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is &lt;code&gt;extends App&lt;/code&gt;? We extend &lt;code&gt;App&lt;/code&gt; level functionality to a scala object whenever we want it to be able to run something inside it. It is like saying, we have a prebuilt &lt;code&gt;public static void main(String[] args)&lt;/code&gt; whenever we extend it.&lt;/p&gt;

&lt;p&gt;The above code is equivalent to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;crashcourse&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;BeginnersCrashCourse&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just to avoid writing the &lt;code&gt;main&lt;/code&gt; method we can simply extend &lt;code&gt;App&lt;/code&gt; to an object.&lt;/p&gt;

&lt;p&gt;It is called a Scala Application if the main object (here &lt;code&gt;BeginnersCrashCourse&lt;/code&gt;) extends &lt;code&gt;App&lt;/code&gt; and is executable in nature. The main code goes inside an object that has &lt;code&gt;App&lt;/code&gt; level functionality. Make sure you have a single main application while creating a service. Each service will have its main application to execute scala code.&lt;/p&gt;

&lt;p&gt;Need more on scala objects, visit &lt;a href="https://docs.scala-lang.org/tour/singleton-objects.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Expressions, Types, Vars, Vals, Def, Print and Codeblocks
&lt;/h3&gt;

&lt;p&gt;How to create a variable in scala?&lt;/p&gt;

&lt;p&gt;Well, there are 2 ways in which we can create a variable in scala:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;crashcourse&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;BeginnersCrashCourse&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;var1&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;//Mutable&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;var2&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;  &lt;span class="c1"&gt;//Immutable&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Writing comments in Scala is the same as Java.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The key difference between the 2 variables is that &lt;code&gt;var1&lt;/code&gt; is mutable and &lt;code&gt;var2&lt;/code&gt; is immutable and in functional programming, we will prefer to use immutable values.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;val&lt;/code&gt; - values and &lt;code&gt;var&lt;/code&gt; - variables, the name suggest itself that &lt;code&gt;val&lt;/code&gt; will be a definite value whereas &lt;code&gt;var&lt;/code&gt; are variables.&lt;/p&gt;

&lt;p&gt;We can also use the semicolon &lt;code&gt;;&lt;/code&gt; at end of each line, but they are not needed. We can also define types to the variables, but for the above case, compiler does this work for us, although it is best to explicitly define the type of each value we use in our code. So below code is similar to the above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;crashcourse&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;BeginnersCrashCourse&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;var1&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//Mutable&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;var2&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//Immutable&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are several data types available in scala, some of them are as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;crashcourse&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;BeginnersCrashCourse&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;var1&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//Mutable&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;var2&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//Immutable&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;bool&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;  &lt;span class="c1"&gt;//or false&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;chr&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;   &lt;span class="c1"&gt;//Make sure you use single quotes for character type value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;str&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"String"&lt;/span&gt;    &lt;span class="c1"&gt;//And double quotes for a string type&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;int&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;    &lt;span class="c1"&gt;//An int value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;shrt&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Short&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;   &lt;span class="c1"&gt;//A short value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;lng&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;     &lt;span class="c1"&gt;//A long value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;lng2&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123L&lt;/span&gt;   &lt;span class="c1"&gt;//Make sure to put L to let compiler know that it is of type Long&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;flt&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;123.0f&lt;/span&gt;   &lt;span class="c1"&gt;//Make sure to put f at the end of float values&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;dbl&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;123.0&lt;/span&gt;   &lt;span class="c1"&gt;//Otherwise compiler will infer it as a type of double&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More on scala types, &lt;a href="https://docs.scala-lang.org/tour/unified-types.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's move on to defining methods in scala. It can be done so using &lt;code&gt;def&lt;/code&gt; keyword, methods can take arguments passed to them as parameters while calling them. We can define empty methods as well, those that take nothing.&lt;/p&gt;

&lt;p&gt;Let's define some methods and call them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;crashcourse&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;BeginnersCrashCourse&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;var1&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//Mutable&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;var2&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//Immutable&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;bool&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;  &lt;span class="c1"&gt;//or false&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;chr&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;   &lt;span class="c1"&gt;//Make sure you use single quotes for character type value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;str&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"String"&lt;/span&gt;    &lt;span class="c1"&gt;//And double quotes for a string type&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;int&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;    &lt;span class="c1"&gt;//A int value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;shrt&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Short&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;   &lt;span class="c1"&gt;//A short value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;lng&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;     &lt;span class="c1"&gt;//A long value&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;lng2&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123L&lt;/span&gt;   &lt;span class="c1"&gt;//Make sure to put L to let compiler know that it is of type Long&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;flt&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;123.0f&lt;/span&gt;   &lt;span class="c1"&gt;//Make sure to put f at the end of float values&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;dbl&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;123.0&lt;/span&gt;   &lt;span class="c1"&gt;//Otherwise compiler will infer it as a type of double&lt;/span&gt;

  &lt;span class="c1"&gt;// Methods&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greetings&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greetings&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Siddharth"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greetingsModified&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;greetingsModified&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sid"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greetingsFromSystem&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, you are learning Scala!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;greetingsFromSystem&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are lot of things happening here, let's break it down for each method:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; We define a method using &lt;code&gt;def&lt;/code&gt; keyword, our first method named &lt;code&gt;greetings&lt;/code&gt; takes a single parameter named &lt;code&gt;name&lt;/code&gt; which of type &lt;code&gt;String&lt;/code&gt; and the method returns a value of type &lt;code&gt;String&lt;/code&gt; denoted by &lt;code&gt;: String&lt;/code&gt; after &lt;code&gt;greetings(name: String)&lt;/code&gt;, and this method evaluates to &lt;code&gt;"Hello " + name&lt;/code&gt; with replacing with whatever name value we pass. We can print it to the console using &lt;code&gt;println&lt;/code&gt; method that conveniently puts a new line after printing the passed value to the console.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A little detour before we move on to the next methods.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In scala everything we write gets evaluated to some value, those statements like &lt;code&gt;"Hello " + name&lt;/code&gt; from above are called expressions. Everything in scala can be referred to as an expression, even assigning of values is called an expression evaluating in such a way that assign values to a given name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any expression that does not evaluate anything but does something, like printing to console or assigning a value or storing data, then data type of that whole expression is called &lt;code&gt;Unit&lt;/code&gt;. So, whenever we assign a value within a method, and that's the last thing that method evaluated to then it will be of type &lt;code&gt;Unit&lt;/code&gt;. Anything that evaluates to &lt;code&gt;Unit&lt;/code&gt; type is called side-effect in scala.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can use code blocks &lt;code&gt;{...}&lt;/code&gt; instead of directly writing an expression for our methods. Methods are evaluated to the value returned by the last expression if it has a number of expressions. You can consider it to be the return value of the method but without needing to write &lt;code&gt;return&lt;/code&gt; explicitly. &lt;a href="https://tpolecat.github.io/2014/05/09/return.html" rel="noopener noreferrer"&gt;Why we should not use return in scala?&lt;/a&gt;. A method code block would look something like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;methodWithCodeBlock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;    &lt;span class="c1"&gt;//Evaluates to String type&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Below method evaluates to same value as above method while doing something else, like printing something&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;methodWithSomethingElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Inside methodWithSomethingElse"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;//Evaluates to Unit type&lt;/span&gt;
  &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;    &lt;span class="c1"&gt;//Evaluates to String type, hence the type of method as it is the last expression evaluating&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Now, the remaining methods can be understood well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; We define the second method &lt;code&gt;greetingsModified&lt;/code&gt; that takes a parameter named &lt;code&gt;name&lt;/code&gt; of type &lt;code&gt;String&lt;/code&gt; but evaluates to &lt;code&gt;Unit&lt;/code&gt; as all this method does is print the greeting to the console as a side-effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; The last method we wrote &lt;code&gt;greetingsFromSystem&lt;/code&gt; does not take any parameter and evaluates to &lt;code&gt;Unit&lt;/code&gt; with the side-effect of printing to console. This is called a parameterless method, scala allows us to define such a method, awesome, right?&lt;/p&gt;

&lt;p&gt;For each method call, the following is the output to the console (to run a scala application, right-click and run in IntelliJ):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Hello Siddharth
Hello Sid
Hello, you are learning Scala!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;More on &lt;code&gt;println&lt;/code&gt;, visit &lt;a href="https://www.geeksforgeeks.org/scala-console-println-printf-and-readline/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More on &lt;code&gt;Unit&lt;/code&gt; type and other such types, visit &lt;a href="https://www.geeksforgeeks.org/scala-null-null-nil-nothing-none-and-unit/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More on codeblocks in scala, visit &lt;a href="https://www.includehelp.com/scala/code-blocks-in-scala.aspx" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More on methods in scala, visit &lt;a href="https://docs.scala-lang.org/overviews/scala-book/methods-first-look.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Well, that warps up part 1 of this crash course.&lt;/p&gt;

&lt;p&gt;In the next part we will be discussing classes, more on objects, companions, case classes and much more related to object-oriented programming in Scala.&lt;/p&gt;

&lt;p&gt;Make sure to follow me to get the update for the next part or subscribe so that you never miss my upcoming blog articles ranging from Python Programming to Computer Vision to Scala Programming.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source" rel="noopener noreferrer"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos" rel="noopener noreferrer"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong" rel="noopener noreferrer"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme" rel="noopener noreferrer"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>scala</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How To Convert Markdown To HTML Using Python</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Mon, 10 May 2021 03:29:18 +0000</pubDate>
      <link>https://dev.to/siddharth2016/how-to-convert-markdown-to-html-using-python-4p1b</link>
      <guid>https://dev.to/siddharth2016/how-to-convert-markdown-to-html-using-python-4p1b</guid>
      <description>&lt;p&gt;In this article, we are going to see how we can use python to convert GitHub flavoured markdown text to HTML. We will go through a python library, what are its benefits and how we can leverage it for automated conversion.&lt;/p&gt;

&lt;p&gt;So, without any further ado, let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  What Are We Going To Use?
&lt;/h3&gt;

&lt;p&gt;We are going to use Python and one of its awesome 3rd party library, &lt;a href="https://github.com/phseiff/github-flavored-markdown-to-html" rel="noopener noreferrer"&gt;gh-md-to-html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To install &lt;code&gt;gh-md-to-html&lt;/code&gt; run the following command on terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;gh-md-to-html
Collecting gh-md-to-html
  Downloading gh_md_to_html-1.11.4-py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;59 kB&lt;span class="o"&gt;)&lt;/span&gt;
     |████████████████████████████████| 59 kB 5.9 MB/s 
Collecting shellescape
  Downloading shellescape-3.8.1-py2.py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;3.1 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting Pillow&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;8.0.1
  Downloading Pillow-8.2.0-cp38-cp38-macosx_10_10_x86_64.whl &lt;span class="o"&gt;(&lt;/span&gt;2.8 MB&lt;span class="o"&gt;)&lt;/span&gt;
     |████████████████████████████████| 2.8 MB 4.8 MB/s 
Collecting webcolors
  Downloading webcolors-1.11.1-py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;9.9 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting requests
  Using cached requests-2.25.1-py2.py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;61 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting beautifulsoup4
  Using cached beautifulsoup4-4.9.3-py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;115 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting soupsieve&amp;gt;1.2
  Downloading soupsieve-2.2.1-py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;33 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting idna&amp;lt;3,&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;2.5
  Using cached idna-2.10-py2.py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;58 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting certifi&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;2017.4.17
  Using cached certifi-2020.12.5-py2.py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;147 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting chardet&amp;lt;5,&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;3.0.2
  Using cached chardet-4.0.0-py2.py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;178 kB&lt;span class="o"&gt;)&lt;/span&gt;
Collecting urllib3&amp;lt;1.27,&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;1.21.1
  Using cached urllib3-1.26.4-py2.py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;153 kB&lt;span class="o"&gt;)&lt;/span&gt;
Installing collected packages: urllib3, soupsieve, idna, chardet, certifi, webcolors, shellescape, requests, Pillow, beautifulsoup4, gh-md-to-html
Successfully installed Pillow-8.2.0 beautifulsoup4-4.9.3 certifi-2020.12.5 chardet-4.0.0 gh-md-to-html-1.11.4 idna-2.10 requests-2.25.1 shellescape-3.8.1 soupsieve-2.2.1 urllib3-1.26.4 webcolors-1.11.1
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, this library depends on a lot of other libraries and is not a pure library like &lt;code&gt;pytube&lt;/code&gt; discussed &lt;a href="https://blog.codekaro.info/download-youtube-videos-using-python-your-own-youtube-downloader" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;p&gt;Advantages include (from its official documentation):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's you specify the markdown to convert as a string, as a repository path, as a local file name or as a hyperlink.&lt;/li&gt;
&lt;li&gt;Pulls any images referenced in the markdown files from the web/ your local storage and places them in a directory relative to your specified website root, so the resulting file structure is host-ready for static sites. Multiple arguments allow the customization of the saving locations, but the images will always be referenced correctly in the resulting HTML files. This is especially useful since it reflects GitHub's behaviour to serve cached copies of README images instead of linking to them directly, reducing tracking and possibly downscaling overlarge images in the process.&lt;/li&gt;
&lt;li&gt;Creates all links as root-relative hyperlinks and lets you specify the root directory as well as the locations for CSS and images, but uses smart standard values for everything.&lt;/li&gt;
&lt;li&gt;Supports inline LaTeX-formulas (use &lt;code&gt;$&lt;/code&gt;-formula-&lt;code&gt;$&lt;/code&gt; to use them), which GitHub usually doesn't. gh-md-to-html uses &lt;a href="https://www.tug.org/texlive/" rel="noopener noreferrer"&gt;LaTeX&lt;/a&gt; and &lt;a href="https://dvisvgm.de/" rel="noopener noreferrer"&gt;dvisvgm&lt;/a&gt; if they are both installed (advantage: fast, requires no internet), and otherwise the &lt;a href="https://latex.codecogs.com/" rel="noopener noreferrer"&gt;Codecogs EqnEditor&lt;/a&gt; (advantage: doesn't require you to install 3 GB of LaTeX libraries) to achieve this.&lt;/li&gt;
&lt;li&gt;Supports exporting to pdf with or without Github styling, using the &lt;a href="https://pypi.org/project/pdfkit/" rel="noopener noreferrer"&gt;pdfkit&lt;/a&gt; python module (if it is installed).&lt;/li&gt;
&lt;li&gt;Tested and optimized to look good when using &lt;a href="https://github.com/darkreader/darkreader" rel="noopener noreferrer"&gt;DarkReader&lt;/a&gt; (the .js-module as well as the browser extension). This is especially relevant considering that DarkReader doesn't usually shift the colours of SVG images, and the formulas added by gh-md-to-html's formula support are embedded as inline svg. gh-md-to-html ensured that the formulas are the same colour as the text, shifted in accordance with DarkReader's current/enabled colorscheme.&lt;/li&gt;
&lt;li&gt;Supports umlauts and other non-ascii-characters in plain text as well as in multiline code blocks, which the github REST api usually doesn't.&lt;/li&gt;
&lt;li&gt;Allows you to choose which tool or module to use at its core for the basic markdown to html conversion.&lt;/li&gt;
&lt;li&gt;Styles its output with github's README-css (can be turned off).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whilst using pandoc to convert from markdown to pdf usually yields more beautiful results (pandoc uses LaTeX, after all), gh-md-to-html has its own set of advantages when it comes to quickly converting complex files for a homework assignment or other purposes where reliability weights more than beauty. pandoc's md-to-pdf-conversion acts quite unusual when it comes to images, nested lists, multiline bullet point entries, or formulas, and gh-md-to-html does not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Converting Markdown To HTML
&lt;/h3&gt;

&lt;p&gt;Now, let's create a simple script that converts markdown text to HTML using &lt;code&gt;gh-md-to-html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following code to a file, name it whatever you like, say &lt;code&gt;mdtohtmldemo.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#./ mdtohtmldemo.py
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gh_md_to_html&lt;/span&gt;

&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gh_md_to_html&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;md-to-html/sample.txt&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where the markdown contents are as follows (notice that we are using it as a text file, but it is not necessary, we can use &lt;code&gt;.md&lt;/code&gt; files directly):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# This is a sample markdown

Hello World!

&amp;gt; Feel free to add your own markdown here.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the above script for given markdown content, we get the below output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version='1.0' encoding='UTF-8'?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/github-markdown-css/github-css.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"text/html"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"gist"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"formula-style"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;svg&lt;/span&gt;&lt;span class="nc"&gt;.gh-md-to-html-formula&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="py"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"gist-file"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c"&gt;&amp;lt;!-- This is the class that is responsible for the boxing! --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"gist-data"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"js-gist-file-update-container js-task-list-container file-box"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"file"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"article-sample"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"Box-body readme blob js-code-block-container p-5 p-xl-6"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"file-docker-image-pull-md-readme"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"margin-left: 40px; margin-right: 40px; margin-top: 20px; margin-bottom: 20px"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;article&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"markdown-body entry-content container-lg"&lt;/span&gt; &lt;span class="na"&gt;itemprop=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user-content-this-is-a-sample-markdown"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;aria-hidden=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"anchor"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#user-content-this-is-a-sample-markdown"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user-content-this-is-a-sample-markdown"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;aria-hidden=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"octicon octicon-link"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt;This is a sample markdown&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;blockquote&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Feel free to add your own markdown here.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/blockquote&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/article&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we notice, it created a CSS &lt;code&gt;/github-markdown-css/github-css.css&lt;/code&gt; in our local directory where we ran the script similar to what github uses.&lt;/p&gt;

&lt;p&gt;There is an output HTML file also created with the same name as &lt;code&gt;sample.html&lt;/code&gt;, if we open it in a browser, we get the following output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1619777354750%2FrohnVAw8s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1619777354750%2FrohnVAw8s.png" alt="samplehtml.png"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Well, that's it from me. If you followed this, you would be seeing an autogenerated HTML file for your markdown file.&lt;/p&gt;

&lt;p&gt;Try different functions, methods and utilities it provides and use them with your projects as needed.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source" rel="noopener noreferrer"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos" rel="noopener noreferrer"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong" rel="noopener noreferrer"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme" rel="noopener noreferrer"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>python</category>
      <category>markdown</category>
      <category>html</category>
      <category>automation</category>
    </item>
    <item>
      <title>Download YouTube Videos Using Python - Your Own YouTube Downloader</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Fri, 07 May 2021 05:40:06 +0000</pubDate>
      <link>https://dev.to/siddharth2016/download-youtube-videos-using-python-your-own-youtube-downloader-3ohf</link>
      <guid>https://dev.to/siddharth2016/download-youtube-videos-using-python-your-own-youtube-downloader-3ohf</guid>
      <description>&lt;p&gt;Do you find it troublesome to always look for a YouTube video downloader with your privacy at risk? Downloading a YouTube video using an external extension might be maddening because of the mouse click ads and other issues while using that extension.&lt;/p&gt;

&lt;p&gt;So, why not we create our own bot that can download any YouTube video with just simply running a Python script? Everything would be in our control then, awesome, right?&lt;/p&gt;

&lt;p&gt;In this article, we are going to see how we can make a Python script to automate the YouTube video downloading activity.&lt;/p&gt;

&lt;p&gt;After going through this article, you can say &lt;code&gt;NO&lt;/code&gt; to all those annoying ads, privacy issues, authorisation to different extensions and can finally have total control.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  What Are We Going To Use?
&lt;/h3&gt;

&lt;p&gt;We are going to use Python and one of its amazing 3rd party library, &lt;a href="https://pypi.org/project/pytube/"&gt;pytube&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To install pytube:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;pytube
Collecting pytube
  Downloading pytube-10.7.2-py3-none-any.whl &lt;span class="o"&gt;(&lt;/span&gt;42 kB&lt;span class="o"&gt;)&lt;/span&gt;
     |████████████████████████████████| 42 kB 3.4 MB/s 
Installing collected packages: pytube
Successfully installed pytube-10.7.2
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More on pytube:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.&lt;/li&gt;
&lt;li&gt;pytube is a lightweight library written in Python. It has no third-party dependencies and aims to be highly reliable.&lt;/li&gt;
&lt;li&gt;pytube also makes pipelining easy, allowing you to specify callback functions for different download events, such as &lt;code&gt;on progress&lt;/code&gt; or &lt;code&gt;on complete&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Finally, pytube also includes a command-line utility, allowing you to quickly download videos right from the terminal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support for both progressive &amp;amp; DASH streams.&lt;/li&gt;
&lt;li&gt;Support for downloading complete playlist.&lt;/li&gt;
&lt;li&gt;Easily register &lt;code&gt;on_download_progress&lt;/code&gt; &amp;amp; &lt;code&gt;on_download_complete&lt;/code&gt; callbacks.&lt;/li&gt;
&lt;li&gt;Command-line interfaced included.&lt;/li&gt;
&lt;li&gt;Caption track support.&lt;/li&gt;
&lt;li&gt;Outputs caption tracks to &lt;code&gt;.srt&lt;/code&gt; format (SubRip Subtitle).&lt;/li&gt;
&lt;li&gt;Ability to capture thumbnail URL.&lt;/li&gt;
&lt;li&gt;Extensively documented source code.&lt;/li&gt;
&lt;li&gt;No third-party dependencies (written in pure Python).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On a high level, to download a video using the library in a script, we'll need to first import the YouTube class from the library, and pass it an argument of the video URL. Then we can download different streams present on that YouTube object.&lt;/p&gt;

&lt;p&gt;Now, let's move on to use this information to build our first YouTube video downloader.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get The Streams
&lt;/h3&gt;

&lt;p&gt;First, we will make a simple script to get all the available streams for the given YouTube video URL. Open a file name it whatever you like with the &lt;code&gt;.py&lt;/code&gt; extension, let's say for our case we name it &lt;code&gt;pytube_demo.py&lt;/code&gt;, add the following code in it and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./pytube_demo.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pytube&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YouTube&lt;/span&gt;

&lt;span class="n"&gt;video_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter YouTube Video URL: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;youtube_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YouTube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;video_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;youtube_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you hit run, when the terminal prompts you to enter a video URL, enter this &lt;code&gt;https://www.youtube.com/watch?v=fibIyvTleqc&lt;/code&gt;, it is one of my favourite songs &lt;code&gt;Daydreamer&lt;/code&gt; by &lt;code&gt;AURORA&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: You can enter any YouTube video URL here, what we are going to explore further doesn't depend on which URL you enter.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Enter YouTube Video URL: https://www.youtube.com/watch?v&lt;span class="o"&gt;=&lt;/span&gt;fibIyvTleqc
&lt;span class="o"&gt;[&lt;/span&gt;&amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"18"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"360p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.42001E"&lt;/span&gt; &lt;span class="nv"&gt;acodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"mp4a.40.2"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"True"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"137"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"1080p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.640028"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"248"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"1080p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"136"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"720p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.4d401f"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"247"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"720p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"135"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"480p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.4d401e"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"244"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"480p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"134"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"360p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.4d401e"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"243"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"360p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"133"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"240p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.4d4015"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"242"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"240p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"160"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"144p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.4d400c"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"278"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"144p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"140"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio/mp4"&lt;/span&gt; &lt;span class="nv"&gt;abr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"128kbps"&lt;/span&gt; &lt;span class="nv"&gt;acodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"mp4a.40.2"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"249"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio/webm"&lt;/span&gt; &lt;span class="nv"&gt;abr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"50kbps"&lt;/span&gt; &lt;span class="nv"&gt;acodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"opus"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"250"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio/webm"&lt;/span&gt; &lt;span class="nv"&gt;abr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"70kbps"&lt;/span&gt; &lt;span class="nv"&gt;acodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"opus"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"251"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio/webm"&lt;/span&gt; &lt;span class="nv"&gt;abr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"160kbps"&lt;/span&gt; &lt;span class="nv"&gt;acodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"opus"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"audio"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we got a list of &lt;code&gt;Stream&lt;/code&gt; objects, each stream object giving us information about different aspects of a video.&lt;/p&gt;

&lt;p&gt;If we see &lt;code&gt;res&lt;/code&gt; attribute of stream object, we see that we have &lt;code&gt;360p&lt;/code&gt;, &lt;code&gt;1080p&lt;/code&gt;, &lt;code&gt;720p&lt;/code&gt;, &lt;code&gt;480p&lt;/code&gt;, &lt;code&gt;240p&lt;/code&gt; and &lt;code&gt;144p&lt;/code&gt; with different &lt;code&gt;vcodec&lt;/code&gt; attribute. This &lt;code&gt;res&lt;/code&gt; represents the resolution of a video, we can select any stream of desired resolution to download.&lt;/p&gt;

&lt;p&gt;Also, notice that we have only audio option streams available as well to download.&lt;/p&gt;

&lt;p&gt;Next, we will create a filter for video resolution and to keep it simple we will not dive into audio downloadable streams.&lt;/p&gt;

&lt;h3&gt;
  
  
  Filter Stream Based On Resolution
&lt;/h3&gt;

&lt;p&gt;Update the code in the &lt;code&gt;pytube_demo.py&lt;/code&gt; file to the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./pytube_demo.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pytube&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YouTube&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_stream_for_res&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""
    Filter on the basis of a given resolution. Return a list of filtered streams.
    """&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resolution&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;

&lt;span class="n"&gt;video_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter YouTube Video URL: "&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;youtube_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YouTube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;video_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;video_res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Enter YouTube Video Resolution for &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;youtube_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: "&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;req_stream_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_stream_for_res&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;youtube_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;video_res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req_stream_obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hit run and we can see following output in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Enter YouTube Video URL: https://www.youtube.com/watch?v&lt;span class="o"&gt;=&lt;/span&gt;fibIyvTleqc
Enter YouTube Video Resolution &lt;span class="k"&gt;for &lt;/span&gt;AURORA - Daydreamer &lt;span class="o"&gt;(&lt;/span&gt;Audio&lt;span class="o"&gt;)&lt;/span&gt;: 720p
&lt;span class="o"&gt;[&lt;/span&gt;&amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"136"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"720p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.4d401f"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;, &amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"247"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/webm"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"720p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"vp9"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"False"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we take the video URL and creates the &lt;code&gt;YouTube&lt;/code&gt; object, then we take custom input from the user while displaying the title of the video using &lt;code&gt;youtube_obj.title&lt;/code&gt;. After receiving the resolution, say &lt;code&gt;720p&lt;/code&gt;, we call the function &lt;code&gt;get_stream_for_res&lt;/code&gt; that filters the given list of streams on the given resolution. At last, we can see the output, it contains 2 stream objects in a list, both objects have different values for some attributes but have the same resolution.&lt;/p&gt;

&lt;p&gt;Without diving deep on what all other attribute means, let's just simply take the first element of the list and download the video.&lt;/p&gt;

&lt;h3&gt;
  
  
  Download Stream Based On Resolution
&lt;/h3&gt;

&lt;p&gt;Now update the code to the following to download the YouTube video (complete script):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./pytube_demo.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pytube&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YouTube&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_stream_for_res&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resolution&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;

&lt;span class="n"&gt;video_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter YouTube Video URL: "&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;youtube_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YouTube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;video_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;video_res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Enter YouTube Video Resolution for &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;youtube_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: "&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;req_stream_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_stream_for_res&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;youtube_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;streams&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;video_res&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;req_stream_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"YouTube Video &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;youtube_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; Downloaded With Resolution &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;video_res&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run the above script, we get the following output and a downloaded video:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Enter YouTube Video URL: https://www.youtube.com/watch?v&lt;span class="o"&gt;=&lt;/span&gt;fibIyvTleqc
Enter YouTube Video Resolution &lt;span class="k"&gt;for &lt;/span&gt;AURORA - Daydreamer &lt;span class="o"&gt;(&lt;/span&gt;Audio&lt;span class="o"&gt;)&lt;/span&gt;: 720p
YouTube Video AURORA - Daydreamer &lt;span class="o"&gt;(&lt;/span&gt;Audio&lt;span class="o"&gt;)&lt;/span&gt; Downloaded With Resolution 720p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we check our directory where we ran the script, we can find the downloaded video.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You may notice that some streams listed have both a video codec and audio codec, while others have just video or just audio, this is a result of YouTube supporting a streaming technique called Dynamic Adaptive Streaming over HTTP (DASH).&lt;/p&gt;

&lt;p&gt;In the context of pytube, the implications are for the highest quality streams; you now need to download both the audio and video tracks and then post-process them with software like FFmpeg to merge them.&lt;/p&gt;

&lt;p&gt;The legacy streams that contain the audio and video in a single file (referred to as “progressive download”) are still available, but only for resolutions 720p and below.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The video downloaded from the given code has a Stream object whose attribute &lt;code&gt;progressive&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;. The given video URL does not have legacy streams available for &lt;code&gt;720p&lt;/code&gt; but if you notice there is one stream with &lt;code&gt;progressive&lt;/code&gt; as &lt;code&gt;True&lt;/code&gt; and &lt;code&gt;res&lt;/code&gt; as &lt;code&gt;360p&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;Stream: &lt;span class="nv"&gt;itag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"18"&lt;/span&gt; &lt;span class="nv"&gt;mime_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video/mp4"&lt;/span&gt; &lt;span class="nv"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"360p"&lt;/span&gt; &lt;span class="nv"&gt;fps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"25fps"&lt;/span&gt; &lt;span class="nv"&gt;vcodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"avc1.42001E"&lt;/span&gt; &lt;span class="nv"&gt;acodec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"mp4a.40.2"&lt;/span&gt; &lt;span class="nv"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"True"&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"video"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, go ahead and experiment with it and don't forget to check out the &lt;code&gt;progressive&lt;/code&gt; attribute.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Next?
&lt;/h3&gt;

&lt;p&gt;Well, that's it from me. If you followed this and ran the final script, you would be seeing a downloaded video in your working directory.&lt;/p&gt;

&lt;p&gt;You can take it to the next level by creating a UI for this utility. You can host this as a service that would be strictly private to you.&lt;/p&gt;

&lt;p&gt;What are you waiting for then? Use Tkinter, PyQT, Kivy, Flask or Django and make a UI with more functions like a select option for audio and video, for video resolution and for audio ABR, etc. and say &lt;code&gt;NO&lt;/code&gt; to all those annoying ads, privacy issues, several authorisations to different extensions and have total control over your downloaded videos.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>application</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Python - List Vs Tuple Memory Management</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Thu, 06 May 2021 05:10:24 +0000</pubDate>
      <link>https://dev.to/siddharth2016/python-list-vs-tuple-memory-management-4056</link>
      <guid>https://dev.to/siddharth2016/python-list-vs-tuple-memory-management-4056</guid>
      <description>&lt;p&gt;This article is going to be a short read, we will focus on how memory is managed in Python for objects like &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;tuple&lt;/code&gt; and what can be the key takeaways.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  List
&lt;/h3&gt;

&lt;p&gt;Python list as we all know is a mutable dynamic array that behaves like a linked list as well. Let's look at some code for creating and updating a list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./list_1.py
&lt;/span&gt;
&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;# A new list gets created
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial List'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# Updating list using in-built append method
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Append 4, List updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;         &lt;span class="c1"&gt;# Pop last item
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pop, List updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;# Updating list by adding a new list to given list
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new list, List updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following will be the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Initial List &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3]
Append 4, List updated to &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3, 4]
Pop, List updated to &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3]
Add new list, List updated to &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3, 5, 6]
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see what happens in memory when we run the above script, here we will be using &lt;code&gt;sys&lt;/code&gt; to help us out on viewing system-related information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./list_2.py
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;
&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;# A new list gets created
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial List'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial List Address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial List Memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# Updating list using in-built append method
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Append 4, List updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Append 4, List Address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Append 4, List Memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;         &lt;span class="c1"&gt;# Pop last item
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pop, List updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pop, List Address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Pop, List Memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;# Updating list by adding a new list to given list
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new list, List updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new list, List Address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new list, List Memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at the output first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Initial List &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3]
Initial List Address 139688582615624
Initial List Memory 88
Append 4, List updated to &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3, 4]
Append 4, List Address 139688582615624
Append 4, List Memory 120
Pop, List updated to &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3]
Pop, List Address 139688582615624
Pop, List Memory 120
Add new list, List updated to &lt;span class="o"&gt;[&lt;/span&gt;1, 2, 3, 5, 6]
Add new list, List Address 139688582615624
Add new list, List Memory 120
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following points we can find out after looking at the output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initially, when the list got created, it had a memory of &lt;code&gt;88&lt;/code&gt; bytes, with 3 elements.&lt;/li&gt;
&lt;li&gt;Later on, after appending an element &lt;code&gt;4&lt;/code&gt; to the list, the memory changes to &lt;code&gt;120&lt;/code&gt; bytes, meaning more memory blocks got linked to list &lt;code&gt;l&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Even after popping out the last element the created blocks memory remains the same and still attached to list &lt;code&gt;l&lt;/code&gt;. And when we add another list with 2 more elements to the given list &lt;code&gt;l&lt;/code&gt;, we see that occupied memory is still the same &lt;code&gt;120&lt;/code&gt; bytes. Hence, we can conclude that it is behaving more like a dynamic array, which updates the size of the list to contain more elements at once to avoid repetitive callbacks (to create and attach new memory address) at every insertion of the new element.&lt;/li&gt;
&lt;li&gt;We also notice that the list address remains the same, even if we do append, pop or add a new list to it (as long as we don't assign it to a new variable). This advocates the nature of the mutability of python lists (it means, more or less, that at the same address we are able to update values or extend it further).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's look at the tuples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tuple
&lt;/h3&gt;

&lt;p&gt;Python tuple as we all know are immutable and do not extend in the memory further after the initial declaration. Now, let's look at the above same scenario but for tuples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./tuple_1.py
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;
&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# A new tuple gets created
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial Tuple'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial Tuple Address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Initial Tuple Memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;     &lt;span class="c1"&gt;# Updating tuple by adding a new tuple to given tuple
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new tuple, Tuple updated to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new tuple, Tuple Address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Add new tuple, Tuple Memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Initial Tuple &lt;span class="o"&gt;(&lt;/span&gt;1, 2, 3&lt;span class="o"&gt;)&lt;/span&gt;
Initial Tuple Address 140452527817280
Initial Tuple Memory 72
Add new tuple, Tuple updated to &lt;span class="o"&gt;(&lt;/span&gt;1, 2, 3, 5, 6&lt;span class="o"&gt;)&lt;/span&gt;
Add new tuple, Tuple Address 140452528572728
Add new tuple, Tuple Memory 88
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following points we notice after looking at the output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since, tuples are immutable, once created cannot be updated unless a new tuple gets added to the previous one, resulting in the creation of a new tuple.&lt;/li&gt;
&lt;li&gt;Once a tuple gets created, its allocated memory will not be changed.&lt;/li&gt;
&lt;li&gt;Updating a tuple after adding another tuple to it, updates the address of the resulting tuple, this advocated its immutability property.&lt;/li&gt;
&lt;li&gt;We can see it allocates &lt;code&gt;72&lt;/code&gt; bytes for 3 values and &lt;code&gt;88&lt;/code&gt; for 6 values, it does not allocate the unnecessary amount of memory, only what is required is allocated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at some miscellaneous points.&lt;/p&gt;

&lt;h3&gt;
  
  
  MISC
&lt;/h3&gt;

&lt;p&gt;What happens when we create an empty list and empty tuple?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ./misc.py
&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;

&lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List l1 address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List l2 address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List l1 memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List l2 memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List t1 address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List t2 address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List t1 memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'List t2 memory'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What did we get?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;List l1 address 139766851756616
List l2 address 139766851761800
List l1 memory 64
List l2 memory 64
List t1 address 139766871547976
List t2 address 139766871547976
List t1 memory 48
List t2 memory 48
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What did we find?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating 2 empty lists have the same amount of memory allocated, whereas the address of both lists is different. It makes sense because, list address will have to act as mutable and cannot be the same at a given time, which means 2 different lists will be different unless they are the same (not in value but in reference point of view).&lt;/li&gt;
&lt;li&gt;Creating 2 empty tuples have the same amount of memory allocated and will point to the same address initially. It makes sense because of its immutability property. Consider integer &lt;code&gt;1&lt;/code&gt; value in python that is immutable and always point to the same address unless changed to some other integer. Similarly, the initial empty tuple will always point to the same address unless changed to some other tuple.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Well, that's it from me.&lt;/p&gt;

&lt;p&gt;We explored quite a few points on how memory is allocated and how it could be managed in Python.&lt;/p&gt;

&lt;p&gt;To know more about memory management, I encourage you to explore their code available on python's &lt;a href="https://github.com/python/cpython"&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feeling it's difficult to go through the source code of python? Don't worry, we can always use available &lt;code&gt;sys&lt;/code&gt; module methods and other functions like &lt;code&gt;id&lt;/code&gt; to develop an understanding of what could be happening behind the scenes.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Shell Script Basics</title>
      <dc:creator>Siddharth Chandra</dc:creator>
      <pubDate>Wed, 05 May 2021 10:54:06 +0000</pubDate>
      <link>https://dev.to/siddharth2016/shell-script-basics-1jnk</link>
      <guid>https://dev.to/siddharth2016/shell-script-basics-1jnk</guid>
      <description>&lt;p&gt;The best thing about GNU/Linux is that it’s packed with utilities that improve productivity immensely. One such utility is a shell that can help accomplish complex tasks with just a few sequences of commands. Most of the time, users operate shell in an interactive way. &lt;/p&gt;

&lt;p&gt;However, it becomes really productive when we leverage the scripting capabilities of the shell.&lt;/p&gt;

&lt;p&gt;In this article, we are going to explore the basics of shell scripting, how simple &lt;code&gt;echo&lt;/code&gt; command can be used in a shell script file, how to write comments, working with variables and conditionals.&lt;/p&gt;

&lt;p&gt;Keeping it short and simple, let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  Shell Script Printing
&lt;/h3&gt;

&lt;p&gt;When we are working with shell scripts printing on the output screen is called echoing which can be done using &lt;code&gt;echo&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello Shell"&lt;/span&gt;
Hello Shell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's do this inside a shell script file. Follow the below steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a file, name it whatever you want with the &lt;code&gt;.sh&lt;/code&gt; extension. Let's say &lt;code&gt;echo_script.sh&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add following inside the file - &lt;code&gt;echo "Hello Shell"&lt;/code&gt;, hit &lt;code&gt;Save&lt;/code&gt; and open a terminal on the file location.&lt;/li&gt;
&lt;li&gt;On the terminal, write - &lt;code&gt;$ bash echo_script.sh&lt;/code&gt; and hit enter, you should be able to see - &lt;code&gt;Hello Shell&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Shell Script Comments
&lt;/h3&gt;

&lt;p&gt;Comments are used to improve the readability of the script. Shell uses the pound &lt;code&gt;#&lt;/code&gt; symbol for comments. A line beginning with the pound &lt;code&gt;#&lt;/code&gt; symbol is ignored by the shell interpreter during execution. &lt;/p&gt;

&lt;p&gt;The script given below shows the usage of the comment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Display Hello Shell using echo command of shell, filename.sh&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello Shell"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to terminal and type - &lt;code&gt;$ bash filename.sh&lt;/code&gt; (replace &lt;code&gt;filename&lt;/code&gt; with whatever name you have given) and hit enter. You should be able to see - &lt;code&gt;Hello Shell&lt;/code&gt; without printing anything given after the &lt;code&gt;#&lt;/code&gt; symbol.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shell Script Variables
&lt;/h3&gt;

&lt;p&gt;We can define variables to store information, which can be accessed within the shell script. There are certain rules we need to follow while defining variables:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The variable name can contain any combination of letters from A-Z or a-z, digits from 0-9 or an underscore (_) character.&lt;/li&gt;
&lt;li&gt;The variable name should start with a letter or an underscore character.&lt;/li&gt;
&lt;li&gt;Variables are case sensitive.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By convention, shell variables are defined in the upper case. Given below is a simple script, which shows the usage of a variable within it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;hello.sh
&lt;span class="nv"&gt;NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"CodeKaro"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello &lt;/span&gt;&lt;span class="nv"&gt;$NAME&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; command is used to display the content of a file given after that. Try your hands on it.&lt;/p&gt;

&lt;p&gt;So, we have a file &lt;code&gt;hello.sh&lt;/code&gt; which contains a variable &lt;code&gt;NAME&lt;/code&gt; which we used in the &lt;code&gt;echo&lt;/code&gt; statement as &lt;code&gt;$NAME&lt;/code&gt; that is after the &lt;code&gt;$&lt;/code&gt; symbol. Here we saw variable declaration and how inside double quotes a variable can be used.&lt;/p&gt;

&lt;p&gt;If you hit - &lt;code&gt;$ bash hello.sh&lt;/code&gt; and hit enter, you should be able to see - &lt;code&gt;Hello CodeKaro&lt;/code&gt; on the terminal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shell Script Conditionals
&lt;/h3&gt;

&lt;p&gt;Like other programming languages, shell supports conditional expressions like – &lt;em&gt;if&lt;/em&gt;, &lt;em&gt;if-else&lt;/em&gt; and &lt;em&gt;case&lt;/em&gt;. Let us understand this with simple examples:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;if&lt;/code&gt; expression: In case we want to verify whether a file exists or not, we can use the if expression, as follows:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;file.txt &lt;span class="c"&gt;#this creates a file&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;file_existance.sh
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; file.txt &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; “file.txt file exists”
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script given above generates the following output when it is executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bash file_existance.sh
file.txt file exists
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;if-else&lt;/code&gt; expression: Let us modify the above script to generate output when the file does not exist:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;rm &lt;/span&gt;file.txt
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;file_existance.sh
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; file.txt &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; “file.txt file exists”
&lt;span class="k"&gt;else
&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; “file.txt file does not exists”
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type following and hit enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bash file_existance.sh
file.txt file does not exists
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;case&lt;/code&gt; statement: Shell provides a switch statement-like functionality using the case statement. Let us understand this with the example given below:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;case_statement.sh
&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; “Enter the day: “ DAY

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nv"&gt;$DAY&lt;/span&gt; &lt;span class="k"&gt;in
&lt;/span&gt;Mon&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; “Today is Monday”
&lt;span class="p"&gt;;;&lt;/span&gt;

Tue&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; “Today is Tuesday”
&lt;span class="p"&gt;;;&lt;/span&gt;

Wed&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; “Today is Wednesday”
&lt;span class="p"&gt;;;&lt;/span&gt;

&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; “Unknown day”
&lt;span class="p"&gt;;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script given above generates the following output when it is executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bash case_statement.sh
Enter the day: Mon
Today is Monday
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bash case_statement.sh
Enter the day: Tue
Today is Tuesday
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bash case_statement.sh
Enter the day: Invalid
Unknown day
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;case&lt;/code&gt; statement, an asterisk (*) denotes a default case.&lt;/p&gt;

&lt;p&gt;Well, that's it from me. &lt;/p&gt;

&lt;p&gt;We covered printing, variables, comments and conditionals, not too much, right? &lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Just starting your Open Source Journey? Don't forget to check &lt;a href="https://github.com/siddharth2016/hello-open-source"&gt;Hello Open Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to showcase your Python project or just out there to explore? Check &lt;a href="https://github.com/siddharth2016/awesome-python-repos"&gt;Awesome Python Repos&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to make a simple and awesome game from scratch? Check out &lt;a href="https://github.com/siddharth2016/PongPong"&gt;PongPong&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Want to &lt;code&gt;++&lt;/code&gt; your GitHub Profile README? Check out &lt;a href="https://github.com/marketplace/actions/quote-readme"&gt;Quote - README&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Till next time!&lt;/p&gt;

&lt;p&gt;Namaste 🙏&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Image Credits - &lt;a href="https://www.freepik.com/vectors/book"&gt;pch.vector&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>shell</category>
      <category>scripting</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
