Skip to main content

Posts

Showing posts from January, 2021

Learn Python : Python Functions

  Python Functions In Python, function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable. Syntax of Function def function_name (parameters) : """docstring""" statement(s) Above shown is a function definition which consists of following components. Keyword  def  marks the start of function header. A function name to uniquely identify it. Function naming follows the same  rules of writing identifiers in Python . Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of function header. Optional documentation string (docstring) to describe what the function does. One or more valid python statements that make up the function body. Statements must have same indentation level ...

Learn python:Python Strings and usage of strings

  Python Strings String is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters. Computers do not deal with characters, they deal with numbers (binary). Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0's and 1's. This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encoding used. In Python, string is a sequence of Unicode character. Unicode was introduced to include every character in all languages and bring uniformity in encoding. How to create a string? Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings. my_string = 'Hello' print(my_string) my_string = "Hello" print(my_string) my_string = '''Hello...

Python Modules: Usage of Python Modules

  Python Modules Modules refer to a file containing Python statements and definitions. A file containing Python code, for e.g.: example.py, is called a module and its module name would be example. We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs. Let us create a module. Type the following and save it as example.py. # Python Module example def add (a, b) : """This program adds two numbers and return the result""" result = a + b return result Here, we have defined a function add() inside a module named example. The function takes in two numbers and returns their sum. How to import modules in Python? We can import the definitions inside a module to another module or the interactive interpreter in Python. We use the import keyword ...

Learn Python Variables : Usage of Python variable

  Python Variables A variable is a location in memory used to store some data (value). They are given unique names to differentiate between different memory locations. The rules for writing a variable name is same as the rules for writing identifiers in Python. We don't need to declare a variable before using it. In Python, we simply assign a value to a variable and it will exist. We don't even have to declare the type of the variable. This is handled internally according to the type of value we assign to the variable. Variable assignment We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned to any valid variable. a = 5 b = 3.2 c = "Hello" Here, we have three assignment statements. 5 is an integer assigned to the variable a. Similarly, 3.2 is a floating point number and "Hello" is a string (sequence of characters) assigned to the variables b and c respectively. Multiple assignments In Python, multiple assignme...