- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does colon ':' operator do in Python?
The : symbol is used for more than one purpose in Python
As slice operator with sequence −
The − operator slices a part from a sequence object such as list, tuple or string. It takes two arguments. First is the index of start of slice and second is index of end of slice. Both operands are optional. If first operand is omitted, it is 0 by default. If second is omitted, it is set to end of sequence.
>>> a=[1,2,3,4,5] >>> a[1:3] [2, 3] >>> a[:3] [1, 2, 3] >>> a[2:] [3, 4, 5] >>> s='computer' >>> s[:3] 'com' >>> s[3:6] 'put'
The − symbol is also used to start an indent suite of statements in case of if, while, for, def and class statements
if expr: stmt
while expr: stmt1 stmt2
for x in sequence: stmt1 stmt2
def function1(): stmt1 stmt2
- What does 'in' operator do in Python?
- What does 'is' operator do in Python?
- What does 'not in' operator do in Python?
- What does 'is not' operator do in Python?
- What does '?' do in C/C++?
- What does 'weight' do in tkinter?
- What does the '@' prefix do in PHP?
- What does 'by' keyword do in Kotlin?
- What does 'show processlist' command do in MySQL?
- How to do string concatenation without '+' operator in Python?
- How does the 'in' operator work on a tuple in Python?
- What does the 'tearoff' attribute do in a Tkinter Menu?