Shortcut to Comment out Multiple Lines in Python

Shortcut to Comment out Multiple Lines in Python

We often need to comment out block of codes in python while testing or debugging the code. When a block is turned into a python comment, it doesn’t contribute in output of the program and helps to determine which function or block is generating error in the program. In this article, we will look at some shortcut to comment out multiple lines of code at once in different python IDEs. Lets see examples for each IDE one by one. Shortcut to comment out multiple lines in Spyder In spyder python IDE, we can comment a single line of code by selecting the line and then using the key combination ctrl+1 . This will turn the selected single line to a comment as shown below. The function given in the example adds a number and its square to a python dictionary as a key-value pair. print(“This line will be commented out.”) def add_square_to_dict(x,mydict): a=x*x mydict[str(x)]=a return mydict After pressing ctrl+1: #print(“This line will be commented out.”) def add_square_to_dict(x,mydict): a=x*x mydict[str(x)]=a return mydict The shortcut to comment out multiple lines of code in spyder IDE is to first select all the lines which need to be commented out and then the key combination ctrl+4 is pressed.…

How to create Multiline Comments in Python

How to create Multiline Comments in Python

Comments are used to enhance readability and understandability of source code by providing proper information about the code. Comments are not executed by an interpreter or compiler and they are used in the code only for assistance of the programmer. In this article, we will understand how to create multiline comments in python. What is a multiline comment in python? As we can see in the name itself, a multiline comment is a python comment which expands to multiple lines i.e. multiline comments are those comments which expand to two or more lines in the source code. Theoretically speaking, there is no syntax for multiline comments in python but we can implement multi line comment using single line comment or triple quoted strings. We will look at both methods to implement multiline comments one by one. Comment out a block of code in Python How to write comments in Python Difference between Comments and Docstrings in Python How to create multiline comments in python using # symbol? As we know that single line comment in python are implemented by adding # symbol before the comment and it terminates whenever a line break occurs, we can put a # symbol at the start of each…

How to Comment Inside a Python Dictionary

How to Comment Inside a Python Dictionary

Comments in python are very handy in increasing the readability and maintainability of code. Generally we use comments to describe functions and class descriptions, for documentation purposes or to explain why a statement has been written in the source code but there may be a situation that we need to explain why we have included certain data in a dictionary or a list. In this article, we will see the basic functioning of python dictionaries and will try to understand how we can add comment inside a python dictionary. Convert Dictionary Values List Python | How to Convert Dictionary Values to a List in Python Python Programming – Python Dictionary Python Programming – Dictionaries Working of a python dictionary In Python, dictionaries are data structures that are used to store data in the form of key and value pairs.A python dictionary is defined using curly braces and key and value pairs are inserted in the dictionary separated by colon “:” while initializing or may be added after initialization using assignment statement. A key-value pair in a python dictionary is called an item. The simplest way to initialize a dictionary is as follows: website_details={“name”:”Pyhton For Beginners”, “domain”:”pythonforbeginners.com” } print(“dictionary is:”) print(website_details) print(“Keys in…

Difference between Comments and Docstrings in Python

Difference between Comments and Docstrings in Python

Comments are used to increase the readability and understandability of the source code. A python comment may be a single line comment or a multiline comment written using single line comments or multiline string constants. Document strings or docstrings are also multiline string constants in python but they have very specific properties unlike python comment. In this article we will look at the differences between comments and docstrings in python. Declaration of comments in python Single line comments in python are declared using a # sign as follows. #This is a single line comment. Python does not primarily have multiline comments but we can write multiline comments in python using multiple single line comments as follows. The function given in the example adds a number and its square to a python dictionary as key value pair. #This is a multiline comment #written using single line comments def add_square_to_dict(x,mydict): a=x*x mydict[str(x)]=a return mydict We can also implement multiline comments in python using multiline string constants as follows. Here the multiline string is declared but isn’t assigned to any variable due to which no memory is allocated for it and it works just like a comment. “””This is a multiline comment written using multiline strings…

Python Collections Counter

Python Collections Counter

Overview of the Collections Module The Collections module implements high-performance container datatypes (beyond the built-in types list, dict and tuple) and contains many useful data structures that you can use to store information in memory. This article will be about the Counter object. Counter A Counter is a container that tracks how many times equivalent values are added. It can be used to implement the same algorithms for which other languages commonly use bag or multiset data structures. Show most requested URL in apache Python generators and the yield keyword How to Check for Anagrams In Python Importing the module Import collections makes the stuff in collections available as: collections.something import collections Since we are only going to use the Counter, we can simply do this: from collections import Counter Initializing Counter supports three forms of initialization. Its constructor can be called with a sequence of items (iterable), a dictionary containing keys and counts (mapping, or using keyword arguments mapping string names to counts (keyword args). import collections print collections.Counter([‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘b’]) print collections.Counter({‘a’:2, ‘b’:3, ‘c’:1}) print collections.Counter(a=2, b=3, c=1) The results of all three forms of initialization are the same. $ python collections_counter_init.py Counter({‘b’: 3, ‘a’:…

Sending Emails with Python

Sending Emails with Python

Python includes several modules in the standard library for working with emails and email servers. smtplib Overview The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. SMTP stands for Simple Mail Transfer Protocol. The smtplib modules is useful for communicating with mail servers to send mail. Sending mail is done with Python’s smtplib using an SMTP server. Actual usage varies depending on complexity of the email and settings of the email server, the instructions here are based on sending email through Gmail. Sending Emails Using Google Python Code Examples How to use FTP in Python smtplib Usage This example is taken from this post at wikibooks.org “””The first step is to create an SMTP object, each object is used for connection with one server.””” import smtplib server = smtplib.SMTP(‘smtp.gmail.com’, 587) #Next, log in to the server server.login(“youremailusername”, “password”) #Send the mail msg = ” Hello!” # The /n separates the message from the headers server.sendmail(“you@gmail.com”, “target@example.com”, msg) To include a From, To and Subject headers, we should use the email package, since smtplib does not modify the contents or headers at all. Email Package…