Previous Page Next Page Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}. Keys are unique within a diction
Previous Page Next Page A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Creatin
Previous Page Next Page The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth. Python has six built-in types of sequences, but the most common one
Previous Page Next Page Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example − var1
Previous Page Next Page Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them. For example − var1 = 1 var2 = 10
Previous Page Next Page In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times. Programming languages provide va
Previous Page Next Page Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine whic
Previous Page Next Page Operators are the constructs which can manipulate the value of operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. Types of Operator Python language supports the following types of operators. Arithmetic Oper
Previous Page Next Page Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserv
Previous Page Next Page The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. First Python Program Let us execute programs in different modes of programming. Interactive Mode Programming Invoking the interpr
Previous Page Next Page Python is available on a wide variety of platforms including Linux and Mac OS X. Lets understand how to set up our Python environment. Local Environment Setup Open a terminal window and type python to find out if it is already installed and which version is ins
Previous Page Next Page Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than othe
In case you missed it: Python 2 is officially not supported as of January 1, 2020. If you’re still on Python 2.7, upgrade now. If you’re not sure what version you’re runing, check your Python version. Many package maintainers have migrated to Python 3. Some still
You can check for the Python version in your code, to make sure your users are not running your script with an incompatible version. Use this simple check: = (3, 5): # Kindly tell your user (s)he needs to upgrade # because youre using 3.5 features
IPython is an enhanced Python REPL, and it’s actually the core for Jupyter notebook. In short, Jupyter is an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. If you use the interactive shell a lot
With sys.getsizeof() you can check the memory usage of an object: import sys mylist = range(0, 10000) print(sys.getsizeof(mylist)) # 48 Woah… wait… why is this huge list only 48 bytes? It’s because the range function returns an iterable object that only behaves like a list of numbers, but inter
With Python, we can return multiple values at once. Obviously, most functions in Python return a single value, usually the result of the work done by that function. In this article, you’ll learn that you can return multiple values in Python too, and you don’t need a dictionary, list,
A Python data class is a regular Python class that has the @dataclass decorator. It is specifically created to hold data. Since Python version 3.7, Python offers data classes through a built-in module called dataclass. There are several advantages over regular Python classes which we’ll ex
With this neat little trick, you can swap two Python variables without using a third variable: a = 1 b = 2 a, b = b, a print (a) # 2 print (b) # 1 It’s just one line of code! As you can see in line 3, no temporary variable is needed to swap variables in Python.
With this trick, you can quickly convert a Python string to title case. To quote from the Wikipedia article: Title case is often used, both in offline and online printing. This site itself uses title case for all its articles. If you look closely, you’ll notice most websites, newspapers,
Python