Syntax in Python and it’s Examples

3
417

Introduction to Syntax in Python

Python is a general-purpose programming language that is becoming ever more popular for data science, to learn a python we need to start by knowing the syntax of the python.

What is Syntax?

Syntax refers to the formation of the language (i.e., what develops a perfectly-formed program).

In this article, we are going to see the syntax of all the parameters used in python.

Modules In Python

These are the list of modules which is help us to check the syntax in Python:-

  • Variable Assignment
  • Accessing Values From Variable
  • Built-In Data Types
  • Comparisons
  • Functions
  • Imports
  • String Manipulation
  • Basic Arithmetic Operators
  • Comments

Variable Assignment: This is used to create a variable and pass a value to Variable, without assigning a value to the variable we cannot perform an operation on it.

Here is some datatype in which we are going to create a variable.

Let’s check the syntax in Python for the variables.

  • For integer Variable: When we are creating a variable that is having integer datatype inside it at that we have to pass some numeric value for the variable.
  • Syntax To create a variable for integer datatype:
    • variable_name= integer_value
  • For example:
    • Integer(Datatype)= 100 (numeric value).
    • x= 100
    • Here variable x is having a value 100 inside it.
  • For String datatype: When we are creating a variable that is having String datatype inside it at that we have to pass some character value for the variable.
  • The string variable is used to store the character data like the Name of the person.
  • Syntax To create a variable for String datatype:
    • variable_name= “string_value”
    • Always pass a string value in Quotes.
  • For example:
    • String(Datatype)=” Fireblaze”(Character).
    • Y=” Fireblaze”
    • Here variable Y is having a String value “Fireblaze” inside it.
  • For Tuple Variable: While creating a tuple variable make sure you used round brackets while passing the data.
    • A tuple is used to create immutable data. It is like a list difference is tuple is immutable whereas the list is mutable.
  • Syntax To create a variable for Tuple datatype:
    • variable_name= (value1,value2,value3)
  • For example:
    • Tuple = (110,120,130) Always use Round Brackets.
    • Here the value of variable Tuple is 110,120,130
  • For List Variable: While creating a List variable make sure you used square brackets while passing the data.
    • A list used to pass multiple items inside the variable when your ar working on more than one item at that time you declare a list variable.
  • Syntax To create a variable for list datatype:
    • variable_name= [value1,value2,value3]
  • For example:
    • List= [110,120,130] Always use Square Brackets.
    • Here the value of variable Tuple is 110,120,130
  • For Dictionary Variable: While creating a dictionary variable make sure you used { } brackets while passing the data.
    • A dictionary is used to create a key-value pair of the data its work like the real lif dictionary, the left side of the colon always considers as a key and right of the colon always consider as value.
  • Syntax To create a variable for String datatype:
    • variable_name={ key: value,key:value}
  • For example:
    • Dictionary={‘fireblaze’: ”data science”}
    • Here in this example, the key is fireblaze and the value is data science.
  • Class_ instances
  • Syntax to create a class instance:
    • Class_Instance= Class name (argument).

Accessing Values From Variable: This is used to access value from  Variable.

Dictionary particular value

Syntax: For Dictionary Value= DictionaryName[key]

Dictionary Keys

Syntax: For Dictionary keys= DictionaryName.keys()

Dictionary Values

Syntax: For Dictionary Values= DictionaryName.Values()

Lists value

Syntax: For List Value=List[index value]

Class variable

Syntax: For Class Variable=ClassName.Class_Variable

Built-In Data Types: There is some Built-In Data types in python which is frequently used.

  • Str -> This is used for character type variable when you want to pass any String inside the variable.
  • Int ->  This Datatype is used when you pass the integer or Number to the Variable.
  • Bool -> This Datatype is having True and False value.
  • Float -> When we pass a value with a decimal point (25.3) the data type of that value is Float
  • List-> When we pass multiple items to a variable by using square brackets, The data type of a particular variable is the list.
  • Dictionary-> While creating a dictionary make sure you pass the value od key and values inside the curly brackets{}. It stores the value by using there key and we can also retrieve the value from the dictionary by using the key of the dictionary.
  • Tuple -> While creating tuple you must follow the round brackets, after creating a tuple you can’t make a change inside it because the tuple is an immutable data type.

Comparisons: Comparison is used to compare a two-variable with the help of its value and data type.

  • Equal to Comparison
    • value1== value2
    • “String1”==” String2”

Here we compare value one with value two if it is equal it will return, True otherwise it will return False.

  • Not Equal to Comparison
    • value1 != value2
    • “String1”!=” String2”

Here we compare value one with value two if it is not equal it will return, True otherwise it will return False.

  • Greater Than  Comparison
    • Value 1> Value2

Here we compare value one with value two if value one is greater than the value two it will return, True otherwise it will return False.

  • Greater than Equal to Comparison
    • Value 1 >= Value 2

Here if the condition Greater than equal to get satisfied then it will return a True otherwise False.

  • Less Than Comparison
    • Value 1 < Value 2

Here If Value 1 is less than Value 2 Then it will return a True otherwise False.

  • Less than Equal to Comparison
    • Value 1 <= Value 2

Here if the condition Less than equal to get satisfied then it will return a True otherwise False.

  • Check the value in a list or not:-
    • Value IN list====> 1 IN [1,2,3]

If the value present in the list then it returns True otherwise False.

Functions:- Function Is a small program which helps us when we are using the same condition or the same calculations in the whole program at that time we create a function and we call the function instead of creating calculation every time.

Syntax to define a function:

 def function_name(argumnet1,argument2,....)

     variable_name= operation using arguments

     return variable_name

Note:- def -> define ,function_name -> pass some name to function

            argument1->pass some argument for operation

            variable_name->Pass a variable name to store a result of operation.

            return -> Return the output to function.

Example:

def addition(x,y):

   z=x+y

   return z

          In this example function_name is addition,x and y are arguments,z is the variable to store a result of the operation x+y and z returns the result to the function name.

Output:- addition(10,20)

Result:- 30.

Comments:- Comments is used to write related information about the function and operator in which we are working.

  • # symbol Use after a define the code or function you pass which is not considered as a code it is considered as a comment.
  • Example:-  A=X+Y # here we are doing addition.

Imports:-Imports is used to import a library and the function from the library there are three methods to import the library and function.

  • Import library
    • Syntax: Import name of the library.
    • Example: Import Numpy

                 Here we import the library NumPy from Python by using the import method.

  • Import library using alise method
    • Syntax: Import name of the library AS new_name
    • Example: Import Numpy AS np

                 Here we import NumPy and Alise it as name np.

  • Import a Function Directly from the Library.
    • Syntax: from  library name import function name
    • Example: from NumPy import random.

                Here We import function random directly from the library NumPy.

String Manipulations:- String manipulation is used to perform an operation on the string.

  • Adding Two Strings
    • “String1”+” String2”
    • Example:- “XYZ”+” ABC”
  • Substring In String
    • Substring” in “ String”
    • Example:-” Fire” in “Fireblaze” => True.
  • String Start with Character
    • String”.startwith(“prefix”)
    • Example:-”Fireblaze”.startwith(“F”) => True

Basic Arithmetic Operators

Basic arithmetic operators are used to perform an arithmetic operation on the variable or Values.

  • C = A+B

Here we perform the Addition of the A and B and store the result in variable C.

  • Z= X-Y

Here we perform the Subtraction of X and Y and store the result in variable Z.

  • M= N*O

Here we perform the Multiplication of N and O and store the result in variable M.

  • A=B/C

Here we perform a Division od B and C, C divide by B, and the result will store in variable A.

  • X=Z**2

Here we perform a Power operation Z to power 2 means it will return the Z to the power 2 results in X.

  • I= A%B

Here we perform Modulus operation on A and B and it will return a result which is a reminder in l.

Conclusion

This Article will help you to get a better understanding of Python syntax and there uses.

3 COMMENTS

  1. whoah this blog is wonderful i really like reading your articles. Keep up the great paintings! You realize, a lot of people are hunting round for this info, you could help them greatly.

LEAVE A REPLY

Please enter your comment!
Please enter your name here