Introduction

Answer:

  • In a weakly typed language a compiler / interpreter will sometimes change the type of a variable. For example, in some languages (like JavaScript) you can add strings to numbers 'x' + 3 becomes 'x3'.
  • In a strongly typed language (like Python) you can't perform operations inappropriate to the type of the object - attempting to add numbers to strings will fail
Discuss It

Answer:

  • In a statically typed language, the type of variables must be declared before it is used. Attempting to use it before declaration will raise an error.
  • In a dynamically typed language, objects still have a type, but it is determined at runtime. You are free to bind names (variables) to different objects with a different type. So long as you only perform operations valid for the type the interpreter doesn't care what type they actually are.
Discuss It

Answer:

The future module
With in-built future module, we can import python 3.x functions to python 2.x.

Division operator
Integer division ‘/’ gives an integer in python 2 but it gives a float in python 3. Python 3 has a floor division ‘//’ to get integer value.

Print function
Python 2’s print statement has been replaced by the print() function in python 3

Unicode
Python 2 has ASCII str() types, separate unicode(), but no byte type.
In Python 3, we have Unicode (utf-8) strings, and 2 byte classes: byte and bytearrays

Xrange
In Python 3, the range() is like the xrange() function in python 2. xrange() function does not exist in python 3.

Discuss It

Answer:

Keywords are the reserved words in Python. In side python these words have its own meaning and usability. You can always get the list of keywords in your current version by typing the following.

import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 
'del', 'elif','else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Remember these key words are specific to a particular version of python. Some keywords may be different in different versions.

Discuss It

Answer:

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type. For example, integer objects are managed differently within the heap than strings, tuples or dictionaries because integers imply different storage requirements and speed/space tradeoffs. The Python memory manager thus delegates some of the work to the object-specific allocators, but ensures that the latter operate within the bounds of the private heap.

Discuss It

Answer:

Python supports following operators

• Arithmetic Operators (+, -, , /….)
• Comparison Operators (>, <, !=, ==, >=, <= …)
• Assignment Operators (=, +=, -=,
=, /=…)
• Boolean Operators (or, and, not)
• Bitwise Operators (&, |, ^, ~, )
• Membership Operators (in, not in)
• Identity Operators (is, is not)
• Shifting Operators (<<, >>)

Discuss It

Answer:

help() and dir() both are built-in functions of python.

help() function:
If no argument is given “help()”, the interactive help system starts on the interpreter console.
If argument is given then it shows its documentation string along with modules, keywords and attributes.

>>> def myfunc():
...     '''This is myfunc'''
...     x = 2
...     return x
...
>>> help(myfunc)
Help on function myfunc in module __main__:

myfunc()
    This is myfunc

dir() function:

Without arguments, it returns the list of names in the current local scope.
With an argument, attempt to return a list of valid attributes for that object.

>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'myfunc']
>>> dir(myfunc)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
Discuss It

Answer:

To make a Python Script executable on Unix

• Script file's mode must be executable and
• the first line must begin with # ( #!/usr/local/bin/python)

Discuss It

Quiz

on
Introduction

Tutorial

on
Introduction

Programs

on
Introduction
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description