Python is a high-level, object-oriented interpreted dynamically typed language. Nowadays, it is almost used in our everyday life since it is simple and easy to understand, and easy to read as well.
People may find some difficulty learning to code in Python which is quite apparent since most of us have not mastered it yet but there is still room to do it now and that is what I am going to help you with.
In this article, I will provide the reader with an idea of What is the use of Syntax in Python and will talk about one of the most crucial syntaxes of Python.
Today we will be discussing what is Syntax with some examples, What we mean by Subscripting Sequences why is it required, the purpose of using Colons and Double Colons in Python along with a few examples of working with the two types of colons.
What is Syntax in Python?
The syntax is one of the basic requirements that we must know to code in any language. Python is beginner-friendly and its syntax is similar to the English language which makes it easier to get a clear concept of coding with Python.
Syntax can be defined as a rule about how a program can be written and interpreted in Python.
However, only having a definition of the syntax will not help much unless we acquire a piece of practical knowledge about it.
An Example To create or define a function In Python using def:
def hey(): print("Good Morning America!") hey()
This Will Output:
#output Good Morning America!
For a better understanding, below is the image of the code along with the output:
In the code mentioned above, at first, we created a function named “hey()” which returns the output “Good Luck Learning Python” with the keyword ‘def‘.
Let us see one more example to get the concept crystal clear.
Following is an easy Example of how to Create a Variable in Python using an integer and string:
x=23 y="Good Morning America!" print(x) print(y)
Here Is The Output:
#output 23 Good Morning America!
Hereby is an image of how the code and the output will appear on Python IDE:
To learn more about Python Syntax, refer to this documentation.
Now as we got to know about the basics of syntax, it is time to describe one of the crucial syntaxes, which is the Double Colon (::).
Subscript in Python
Let us first get a small idea of what we mean by the Subscripting Operator in Python.
Elements of string, list, and tuple can be accessed with this method. It is defined as square brackets.
All sequences including strings, which can be thought of as sequences of characters may also be subscripted. It might be single elements or slices.
Subscripting also allows viewing a portion of a sequence of relative constraints.
For Better Clarification, Here Is An Example Of How Can We Access A Single Character Of A String:
String = "Hello"  #printing the character at the 0th index print(String)
Take a look at the attached image for clarification along with the output:
In the above example, we have taken “Hello” as a sequence of the element “String”. We have subscripted single element .
Subscript can be sliced on one end as well.
In This Example We Are Going To Explore How Subscripting Slices On One End Works:
String = "Hello"[1:] #Slicing the 1st indexed string print(String)
Below is the image of the code after the implementation:
Subscripting of slices can be negative further.
Following Is An Illustration Of The Negative Slicing Of A Sequence:
String = "Hello" [-3:-1] #Substring will be starting from the 3rd character to the end print(String)
This Will Output:
After implementing the code, this result will be the following:
Why is Subscripting required?
- Using Subscript Operator, a new value can be added to an existing value.
- Any value from a Python dictionary can be retrieved corresponding to a key.
Double Colon (::) in Python
Till now we have discussed the definition and some basic examples of syntax. Let us now take a look at a special syntax that is Double Colon(::) and its features that make the syntax unique and useful. We will also illustrate it with some examples.
Before getting into the topic, let’s define What is Colon in Python.
A colon is an essential syntax used in Python. It can be used for various purposes, for example, representing indenting blocks, fetching an indexed array, or the range of a specific position of an element, and declaring a function as well.
Another major purpose of the colon is Slicing.
There are namely 2 types of Colon in Python:
- Colon (:)
- Double Colon (::)
Today, we are going to learn about the Double Colon (::).
The Double Colon is a Slice Operator in Python. With the help of this syntax, every item of an element gets sliced and it jumps elements in multiple axes.
For Illustration, Here Is An Example Of Slicing A Sequence Using A Double Colon:
String = "Programming"[4::] #Starting the slicing from the 4th index print(String)
The Output Will Be:
The image after implementing the code:
In the above example, we have taken “Programming” as a String and the string is sliced till index 4.
The syntax for slicing with Double Colon is [Start: Stop: Step] where Start indicates where the slicing of an object starts, and Stop defines the object till where the slicing takes place lastly, Step may be defined as the step to consider for the range.
Let Us Look Into The Following Example Using The [Start: Stop: Step] Method:
String = "Good Morning America!" [9:16:2] #Slicing starting from the 9th upto 16th character print(String
This Will Output:
This is how the code would look:
Here, the interpreter executed and printed index 9 to index 16.
However, we can reduce the range by using the operator ::
However, you may refer to the code to acquire the result:
The operator :: in front indicates that any start or end indices have not been specified.
This particular documentation assisted me to extract a few pieces of information.
To know more about Double Colon :: Operator refers to this article
Here’s an article on Slicing arrays with colons that also can be used for slicing tuples and strings.
In this topic, we have discussed some most basic yet crucial syntaxes and which according to me are quite easy to understand and implement.
We have also learned about the mentioned two Colons and their features, utilization, and function through some examples.
Thank you for your attention and stay tuned for more tutorials.