top of page
Writer's pictureMayur K. T.

Unraveling Python's Core: Comments, Indentation, and Data Types

Updated: Feb 11


 

Table of content

 

Introduction


Ahoy, Code Adventurers! Welcome aboard our coding ship as we set sail into Lesson 1 of the Learning Series - 1. I trust you've explored the treasures of our previous lessons – if not, they're safely pinned below for your perusal.

Now, brace yourselves for today's nautical odyssey where we navigate the vast sea of basic syntax and explore the mysterious islands of data types. Hoist the coding sails and ready your interpreter; our sea adventure in the world of Python is about to unfurl!


Comments


Behold the humble comment – the code whisperer, the digital bard of anecdotes! Picture this: a vibrant carnival of characters and syntax, where our code stands as the grand stage. Now, imagine comments as the masked jesters, weaving tales, cracking jokes, and perhaps sharing a digital wink or two.


In this pixelated masquerade ball, commenting on code is an art, a ballet of wit and wisdom. It's not just about syntax and semantics; it's about injecting life into the otherwise stoic script.


Do our comments convey admiration, like complimenting a dapper line of code, or are they the rebels, shouting, "What sorcery is this?" in the face of complex algorithms? Picture a Shakespearean play unfolding in the brackets and slashes, where the comments are the soliloquies that reveal the inner musings of our code's characters.


In Python, the '#' symbol i.e. used for commenting, is like a special marker. When you put it in front of something in your code, it tells the interpreter to ignore that part. It's like telling the interpreter, "Hey, don't pay attention to this right now."


So, developers use '#' to write notes or explanations in their code. It helps them and others understand what different parts of the code are doing. It's like leaving little hints or comments for anyone who reads the code later.


Also, if there's a line of code that you don't want to run for some reason, you can just put a '#' in front of it. The interpreter will skip over it as if it's not even there.

In simple terms, '#' is a way for developers to talk to the computer and to each other, making the code easier to read and understand.


There are two ways of commenting in python:

  • Single line comment : Here we use the symbol # and particular text after # is commented.

  • Multi line comment: Here we use """ tiple quotes and everything within quotes will be commented.




Indentation


Indentation in Python is a crucial aspect of syntax, using spaces or tabs at the start of code lines to denote code relationships within blocks. It is mandatory, not optional, and helps maintain code structure and readability. Unlike other languages that use braces, Python relies on indentation to indicate code blocks. This practice is fundamental for writing clean and organized Python code.


Now how many number of spaces are required? Well it is up to you. To make it easier, just press tab, your code line will automatically be indented.


What will happen if we don't indent or indent incorrectly? Well it is a syntax error. The interpreter will return IndentationError.


Let's look at a problem statement:

Write a python program to find whether the input number is even or odd. Take user input


Don't worry about the syntaxes. Basically it takes input from user and using logic it finds out whether the input number is odd or even. There is use of comments to make users understand the code purpose and there is indentations, which improves readability.



Data Types


Understanding data types is a fundamental concept applicable not only in Python but across various programming languages. Data types refer to the distinct categories of data values, such as integers, characters, or sets, that are inherently built into the language. These types serve as the foundational components for representing and manipulating data within a program.


In programming, variables, as discussed in a prior lesson, are entities that hold values. These values inherently possess a specific type, known as the data type. The data type of a value dictates how it behaves within the program and influences the operations that can be performed on it. Recognizing and appropriately utilizing data types is crucial for writing effective and error-free code in any programming language.


There are two types of built-in data types: Mutable Types and Immutable Types





Immutable Type

Immutable data types are data types that cannot be changed after they are created. This means that the value of an immutable object cannot be modified after the object is created. Immutable data types are useful for storing data that needs to be protected from accidental changes.


Numeric Types: Numeric data types are used to store numbers.

  • Integers: Integers are whole numbers, such as 1, 2, 3, and -10.

  • Floats: Floats are numbers with decimal points, such as 3.14, 5.25, and -1.5.

  • Complex numbers: Complex numbers are numbers of the form a + bi, where i is the square root of -1. Complex numbers are used in advanced mathematics and physics.

String data types: String data types are used to store text. Strings can be any combination of letters, numbers, and symbols. Strings are enclosed in single quotes (') or double quotes (").


Boolean data types: Boolean data types are used to store True or False values. Boolean data types are often used in conditional statements, such as if and elif statements.

Tuple data types: Tuples are similar to lists, but they are immutable. Tuples are enclosed in parentheses ( ).


Dictionary data types: Dictionaries are used to store key-value pairs. Keys can be any type of data, and values can be any type of data. Dictionaries are enclosed in curly braces ({ }) with colons (:) separating the keys from the values.


Mutable data types


Sequence Type

list tuple range

Numeric Type

int float complex

Mapping Type

dict

​Boolean Type

bool

Set Type

set frozenset

​Text Type

str

​None Type

NoneType

In the context of finding odd and even numbers, the `input()` function is employed to obtain user input. It is a built-in function that captures input from the user, treating it as a string by default and returning it as such. Even when the user inputs a numerical value, the function interprets and presents it as a string data type.


However, in scenarios where numeric input is desired for further numerical operations, it becomes essential to convert the input from string to numeric types. Two common methods for achieving this are `int()` and `float()`. The `int()` function is used to convert the input into an integer data type, while `float()` is employed for converting it into a floating-point number. By employing these conversion functions, the input is transformed into a numeric type, allowing for numerical computations and comparisons in the program.


Let's look at these two snippets:



Consider the scenario where the user inputs the value "9". The output, in both instances, is "9". While the outcomes appear identical, the distinction lies in the code's efficiency and usability. The latter code snippet, though functionally equivalent, demonstrates a more concise form. This brevity not only enhances readability but also contributes to the efficiency of processor utilization.


Reducing the complexity of code algorithms is a fundamental principle in programming. In more intricate code structures, such optimizations become significant. While this example might seem modest, it underscores a broader concept known as data structures. Though not immediately relevant, it emphasizes the importance of maintaining code simplicity and brevity. The ability to keep code succinct and straightforward is a hallmark of effective programming.


Now it's your turn to practice and play around different data types and examples






Conclusion:


And thus, the anchors descend, signaling the end of today's coding voyage. But fret not, fellow mariners of the code sea, for this isn't the finale but a mere pause in our nautical odyssey. Secure those metaphorical sails and stay attuned to the rhythmic hum of the code waves, for future lessons await, shimmering like undiscovered islands on the horizon.


As the coding ship temporarily docks, I implore you: keep your programming compass close and your curiosity compass even closer. Let the lessons learned today be the wind in your coding sails, propelling you toward the uncharted waters of upcoming exciting lessons.


So, my fellow sailors, bask in the afterglow of today's lesson, and remember, a well-seasoned coder is like a masterful captain – always ready for the next thrilling adventure on the ever-expansive sea of code. Until the next tide of knowledge, stay buoyant, keep coding, and never let your learning spirit be cast adrift!




40 views0 comments

Comments


bottom of page