Jump Start With Python - Part 3

This article is in a continuation of my previous articles:
  • Variables and data types in Python adhere to standard nomenclature of an alphanumeric name beginning with a letter (A-Z or a-z or 0-9) or underscore ("_").
  • Variables do not need to be declared and variable names are case sensitive.
  • The datatype of variables in Python is automatically inferred from the assignment statement.
    Python currently supports the following data types:

    • Boolean
    • Integer
    • Float
    • String
    • List
    • Tuples
    • Dictionary
    • Objects
    • None
Syntax/Example
 
Variable Datatypes
 
Figure 1: Syntax and Example for Variable and data types (input)


Figure 2: Syntax and Example for Variable and data types (Output)

Expressions and Statements in Python
  • Some basic statements are allowed in Python as follows:

    1. print

      It is used to output strings, integer or any other datatype.

      Example
      1. print("Jump Start with Python - Part 3")  
    2. Assignment Statement

      It is used to assign a value to a statement.

      Example
      1. x=139 #139 is assigned to variable x  
      2. y="Python" #Python is assigned to variable y  
    3. input / raw_input

      It is used to get input values from the user such as int, string, bool and so on.

      Example
      1. x=input("Enter Value")  
      2. #This will take input from the user, we can also parse specific input type such as  
      3.   
      4. y=int(input("Enter Integer Value"))  
      5. #y will accept only integer value  
    4. Import

      It allows importing modules in Python, for example, os, math and so on.

  • Passing Statements

    Python allows the passing of statements typically in either of two ways and they are explained in the following.

    1. Single Line Statement
      1. #this is a example of single line statement  
      2. sum=x + y + z 
    2. Multi-Line Statements
      1. #this is example of multi-line statements  
      2. sum= a + \  
      3. b + \  
      4. c
  • String Quotations

    Python allows (') Single, (") Double and (""" or ''') Triple quotes for string literals and they are used at the beginning and ending point of a string respectively.

    Example
    1. #Example of Single quote  
    2. x='Hello'  
    3.   
    4. #Example of Double quote  
    5. y="C# Corner" 
    Triple quotes are used for spanning a string to multiple lines. It is usually used to either get a paragraph as input or output.

    Example
    1. #Example of Triple quote  
    2. t1='''''''This is 1st 
    3. example of triple quote.'''  
    4.   
    5. t2="""This is 2nd 
    6. example of triple quote."""  
Operators in Python

Operators are the most important part of a programming language since they provide and enhance the computational and functional capabilities of a programming language. There are various types of operators available in Python and they are categorized under the following categories.
  1. Arithmetic Operators

    They are used to do arithmetic operations and are as in the following:

    • Modulus (%)
    • Division (/)
    • Multiplication (*)
    • Addition (+)
    • Subtraction (-)
    • Exponent (**)
    • Floor Division (//)

    Syntax and Example

    Arithmetic Operators

    Figure 3: Syntax and Example for Arithmetic Operators (Input)

    Example for Arithmetic Operators

    Figure 4: Syntax and Example for Arithmetic Operators (Output)

  2. Relational/Comparison Operators

    They are used to relate or compare the values on variables present on both sides of these operators. Relational/Comparison operators are as listed below:

    • Equality (==)
    • Not Equal (!=)
    • Less Than (<)
    • Greater Than (>)
    • Less Than Equals (<=)
    • Greater Than Equals (>=)

    Syntax and Example

    Relational Comparison Operators

    Figure 5: Syntax and Example for Relational/Comparison Operators (Input)

    Note: I will explain about Conditional Programming in Python (If statement) used in this example in another article.

    Example for Relational Comparison Operators

    Figure 6: Syntax and Example for Relational/Comparison Operators (Output)

  3. Bitwise Operators

    These operators are also known as Binary Operators. They work on bits and perform bit-by-bit operations and they are listed as in the following:

    • Binary AND (&)
    • Binary OR (|)
    • Binary XOR (^)
    • Binary One's Complement (~)
    • Binary Left Shift (<<)
    • Binary Right Shift (>>)

    Syntax and Example

    Bitwise Binary Operators

    Figure 7: Syntax and Example for Bitwise/Binary Operators (Input)

    Example for Bitwise Binary Operators

    Figure 8: Syntax and Example for Bitwise/Binary Operators (Output)
  4. Logical Operators

    They are used to perform logical operations and are listed below:

    • AND (and)
    • OR (or)
    • NOT (not)

    Syntax and Example

    Logical Operators

    Figure 9: Syntax and Example for Logical Operators (Input)

    Example for Logical Operators

    Figure 10: Syntax and Example for Logical Operators (Output)

  5. Membership Operators

    These operators check for membership in a sequence, such as strings, tuples, dictionary or lists and so on and they are listed as in the following:

    • IS (is)
    • IS NOT (is not)

    Syntax and Example

    Membership Operators

    Figure 11: Syntax and Example for Membership Operators (Input)

    Example for Membership

    Figure 12: Syntax and Example for Membership Operators (Output)

  6. Identity Operators

    These operators are used to compare the memory locations of two objects and they are listed as in the following:

    • IS (is)
    • IS NOT (is not)

    Syntax and Example

    Identity Operators

    Figure 13: Syntax and Example for Identity Operators (Input)

    Example for Identity Operators

    Figure 14: Syntax and Example for Identity Operators (Output)

  7. Assignment Operators

    They are used for assigning values to variables and are listed as in the following:

    • Assignment Operator (=)
    • Modulus AND (%=)
    • Divide AND (/=)
    • Multiply AND (*=)
    • Add AND (+=)
    • Subtract AND (-=)
    • Exponent AND (**=)
    • Floor Division AND (//=)

    Syntax and Example

    Assignment Operators

    Figure 15: Syntax and Example for Assignment Operators (Input)

    Example for Assignment Operators

    Figure 16: Syntax and Example for Assignment Operators (Output)