Python tips

Concatenating strings.

You can use ‘+’ to concatenate strings in the following manner.

# See how to use '+' to concatenate strings.

    >>> print('Python' + ' Coding' + ' Tips')

# Output:

    Python Coding Tips

Conditional Expressions.

Python allows for conditional expressions. Here is an intuitive way of writing conditional statements in Python. Please follow the below example.

# make number always be odd

number = count if count % 2 else count - 1

# Call a function if the object is not None.

data = data.load() if data is not None else 'Dummy'
print("Data collected is ", data)

and = operators.

Python uses ‘==’ for comparison and ‘=’ for assignment. Python does not support inline assignment. So there’s no chance of accidentally assigning the value when you want to compare it.

Dynamic typing.

In Java, C++, and other statically typed languages, you have to specify the data type of the function return value as well as the kind of each function argument. On the other hand, Python is a dynamically typed language. In Python, you don’t explicitly provide the data types. Based on the value you’ve assigned, Python keeps track of the datatype internally. Another good definition of dynamic typing is as follows. “Names are bound to objects at run-time with the help of assignment statements. And it is possible to attach a name to the objects of different types during the execution of the program.” The following example demonstrates how a function can examine its arguments. And do different things depending on their types.

# Test for dynamic typing.

from types import *

def CheckIt (x):
    if type(x) == IntType:
        print("You have entered an integer.")
        print("Unable to recognize the input data type.")

# Perform dynamic typing test
    # Output:
    # You have entered an integer.

    # Output:
    # Unable to recognize the input data type.

Multiple Arguments function

This is an awesome tip and trick, let say you are creating a function and don’t know how many arguments are coming from the user so that trick will help you to accept unlimited arguments from the user.

def number(*num):    for n in num:        print(n)number(2,3,5,6)number(4,5,6,2,6,7,9,10)

The data type SET.

The data type “set” is a kind of collection. It has been part of Python since version 2.4. A set contains an unordered collection of unique and immutable objects. It is one of the Python data types which is an implementation of the <sets> from the world of Mathematics. This fact explains, why the sets unlike lists or tuples can’t have multiple occurrences of the same element. If you want to create a set, use the built-in set() function with a sequence or another iterable object.

# *** Create a set with strings and perform search in set

objects = {"python", "coding", "tips", "for", "beginners"}

# Print set.

# Use of "in" keyword.
if "tips" in objects:
    print("These are the best Python coding tips.")

# Use of "not in" keyword.
if "Java tips" not in objects:
    print("These are the best Python coding tips not Java tips.")

# ** Output

    {'python', 'coding', 'tips', 'for', 'beginners'}
    These are the best Python coding tips.
    These are the best Python coding tips not Java tips.
# *** Lets initialize an empty set
items = set()

# Add three strings.


# ** Output

    {'Python', 'coding', 'tips'}

Deep Copy

You can do a Shallow copy in python but how you can do a deep copy ?. Well, this tip will help you do a deep copy in Python in an easy way.

import copylist1 = ["ferb", "haider", "Jenny"]list2 = copy.deepcopy(list1)

Remember the built-In functions.

Python comes with a lot of batteries included. You can write high-quality, efficient code, but it’s hard to beat the underlying libraries. These have been optimized and are tested rigorously (like your code, no doubt). Read the list of the built-ins, and check if you’re duplicating any of this functionality in your code.

Reverse List

in Python, we could reverse the list by using reverse method and slicing signature ::-1

# example using reverse
num=[1, 2, "3"] 
['3', 2, 1]

# example using `::-1`
num=[1, 2, "3"]
['3', 2, 1]

Passing List as Function Arguments

  • operator could be used to passing list data type as function arguments
data = ["La", "Asada", 1121]
def person(firstname, lastname, user_id):
    print(firstname, lastname, user_id)

"La" "Asada" 1121