Home Show/Hide Menu
PYTHON TOPICS
➜ Introduction
➜ How to start
➜ Syntax
➜ Simple programs
➜ comment lines
➜ Assigning Values
➜ Dynamic Typing
➜ Input / Output
➜ Character Set
➜ Tokens
➜ Variables
➜ Operators
➜ Literals
➜ Numbers
➜ Type casting
➜ Strings
  - Python Strings
  - Format Strings
  - Slicing Strings
  - Modify Strings
  - String Concatenation 
  - Escape Characters
  - String Methods
➜ Booleans
➜ Lists
  - Access List Items
  - Change List Items
  - Add List Items
  - Remove List Items
  - List using Loop
  - List Comprehension
  - Sort Lists
  - Copy Lists
  - Join Lists
  - List Methods
➜ Tuples
  - Access Tuples
  - Update Tuples
  - Unpack Tuples
  - Tuples using Loops
  - Join Tuples
  - Tuples Methods
➜ Sets
  - Access Set Items
  - Add Set Items
  - Remove Set Items
  - Loop Sets
  - Join Sets
  - Set Methods
➜ Dictionaries
  - Access Items
  - Change Items
  - Add Items
  - Remove Items
  - Loop Dictionaries
  - Copy Dictionaries
  - Nested Dictionaries
  - Dictionaries Methods
➜ if...else
➜ For Loops
➜ While Loops
➜ Lambda
➜ Arrays
➜ Class / Objects
➜ Inheritance
➜ Iterators
➜ Scope
➜ Modules
➜ Dates
➜ Maths
➜ JSON
➜ RegEx
➜ PIP
➜ Try...Except
➜ User Input
➜ File Handling
  - File Handling
  - Read Files
  - Write/Create Files
  - Delete Files
➜ Python Modules
  - NumPy Tutorial
  - Pandas Tutorial
  - SciPy Tutorial
➜ Python Matplotlib
  - Intro
  - Get Started
  - Pyplot
  - Plotting
  - Markers
  - Line
  - Labels
  - Grid
  - Subplots
  - Scatter
  - Bars
  - Histograms
  - Pie Charts
➜ Machine Learning
  - Getting Started
  - Mean Median Mode
  - Standard Deviation
  - Percentile
  - Data Distribution
  - Normal Data Distribution
  - Scatter Plot
  - Linear Regression
  - Polynomial Regression
  - Multiple Regression
  - Scale
  - Train/Test
  - Decision Tree
➜ Python MySQL
  - Get Started
  - Create Database
  - Create Table
  - Insert
  - Select
  - Where
  - Order By
  - Delete
  - Drop Table
  - Update
  - Limit
  - Join
➜ Python MongoDB
  - Get Started
  - Create Database
  - Create Collection
  - Insert
  - Find
  - Query
  - Sort
  - Delete
  - Drop Collection
  - Update
  - Limit
➜ Python Reference
  - Python Overview
  - Python Built-in Functions
  - Python String Methods
  - Python List Methods
  - Python Dictionary Methods
  - Python Tuple Methods
  - Python Set Methods
  - Python File Methods
  - Python Keywords
  - Python Exceptions
  - Python Glossary
➜ Module Reference
  - Random Module
  - Requests Module
  - Statistics Module
  - Math Module
  - cMath Module
➜ Python How To
  - Remove List Duplicates
  - Reverse a String
  - Add Two Numbers
➜ Python Examples
  - Python Examples
  - Python Compiler
  - Python Exercises
  - Python Quiz
  - Python Certificate

Python Syntax


The syntax of the any programming language is the set of rules that defines how a program will be written and interpreted (by both the runtime system and by human readers).
Python syntax can be executed either using Command Line Or by creating a python file on the server, using the .py file extension and execute it in the Command Line.

Writing directly in the Command Line:

>>> print("Hello, Students!")
    Hello, Students!

By creating a python file and running it in the Command Line:
C:\Users\Your Name>python file1.py


  1. Python Indentation

  2. Indentation in Python helps to create block of code.Without proper indentation python code will not get compiled and displaying IndentationError.Where in other programming languages such as C, java use braces({}) to define a block of code,Python uses indentation.
    Indentation refers to the spaces at the beginning of a code line.
    Example:

    if 7 > 2:
      print("seven is greater than two!")
    

    The same number of spaces must be used in the same block of code,Otherwise Python will show error .
    if 9>5:
      print("9 is greter than 5")
      print("5 is less than 9")	
    
    The above code is correct.

    if 9>5:
      print("9 is greter than 5")
         print("5 is less than 9")		
    
    The above code is incorrect.Error will be displayed.

    Example of Python Syntax

    Python is a user friendly language because of its intelligent syntax structure.
    Let’s do a simple Python program and you will get an idea of how programming in Python looks like.
    #Simple Python Program to see whether a user is Minor or Adult.
    # getting user’s name 
    print("Enter your name:")
    name = input()
    # getting user’s age
    print("Enter your age:")
    age = int(input())
    # condition to check whether user is Minor or Adult
    if( age >= 18 ):
        print( name, ' is Adult.')
    else:
        print( name, ' is Minor.')
    
    Output
    
    Enter your name:
    Rishi
    Enter your age:
    19
    Rishi is Adult.
    

    Carefully, look at all print() statements given above.Could you notice that first two print()statements are enclosed in double quotes and the next two print() statements are enclosed in single quotes.
    print("Enter your name:")
    print("Enter your age:")
    statements enclosed in double quotes

    print( name, ' is Adult.')
    print( name, ' is Minor.')
    statements enclosed in single quotes

  3. Python Multiline Statements

  4. In Python a new line means a new statement. But sometimes, you may want to split a statement in two or more lines.
    It may be to aid readability. You can do so in the following ways.

    i. Use a backward slash
    >>> print("Hello students\
     how are you?")
    
    	output
    	Hello students how are you?
    
    You can also use it to distribute a statement.
    >>>x\
    =\
    5
    >>> print(x)
    
    Output:
    
    5	
    
    ii. Put the String in Triple Quotes
    >>> print("""Hello students
           how are you?""")	
    
    Output:
    
    Hello students
         how are you?	
    
    >>> ns='''he\
    ll\
    o'''
    >>> print(ns)	
    
    hello	
    
    iii. Multiple Statements in One Line
    >>>x=5;print(x);	
    
    output:
    5	
    

  5. Python Variables

  6. Data type is not necessary to declare a variable in Python Programming language.Python variables are created by assigning values of desired type.
    Example
    Variables in Python:
    >>>x = 5
    >>>print(x)
    5
    >>>x="Hello, World!"
    >>>print(x)
    Hello, World	
    

  7. Python Comments

  8. Comments are used for code documentation.
    Python Comments start with a #
    Example
    Comments in Python:
    #This is a comment.
    print("Hello, students!!")	
    



ADVERTISEMENT