Learn Python In One Blog That Makes You Comfortable With The Python Code

Rate this post

In this blog, we will be covering the fundamentals of python language with some in-depth concepts that make you comfortable with the python code. You just need to make sure that you hav

Learn Python In one blog
learn python in one blog

In this blog, I am assuming that you people have installed Python for your system if not then checkout the www.python.org website for your python installation.

Python Language works on the basis of an interpreter. It means that when we will write a python program the code will be executed line by line from left to right manner. The interpreter enables us to find and debug our code whenever there is an error in our code from the top of it and mention the line number where the error occurred. Whatever code we are writing in python it is also said that writing scripts in python, therefore, it is also known as Scripting Language.

Here Scripting Language is the programming language that comes under high-level languages it constructs to interpret and execute one command at a time.

To run python code we need to follow the below-mentioned point:

  • To write code we need an editor for it In this case we will be taking “Notepad” as our editor for writing the python program
  • After opening Notepad copy paste below line:
    • print(“Hi I am running python code”)
    • #this is a print method used in python to print strings and variables we will see this in detail in the below section.
  • After putting this code save your file with the “.py” extension here py represents python which will be referred to while running from the command prompt
  • Save your program with the ‘example.py’ file name and extension(.py)
  • To run the ‘example.py’ file open the command prompt and point it to the ‘example.py’ location.
  • Type “python example.py” and it will give you output like this “Hi I am running python code”.

Fundamentals Of Python:-

In every programming language, we need to follow certain rules and regulations like in the real world we follow.

So python is also that kind of thing.

  • Variables In Python:-
    • In Python, we don’t need a declaration to declare a variable type like integer a, or string s before any identifier. We simply write a variable name also called an identifier which follows certain rules for example a variable name should start with an alphanumeric/underscore but not with the number and the variable name should be unique to define its own content.
    • In python we have below data types:
      • Integer
        • for example:- 1, 2, 3, …etc
        • a = 1
      • Float
        • for example:- 1.1, 3.1, 2.2…etc
        • a = 2.1
      • String
        • for example:- any word, any sentence, any paragraph which contains some element blank space is also considered a string element
        • website_name = “codewithdc.com”
      • List
        • a = [1, 2, 3, 4, 5, 6]
        • the list data type can be traced through an index like a[0] will result in 1.
        • note index starts with 0 and is incremented as data is appended to the list
      • Tuple
        • a = (1, 2, 3, 4, 5)
        • In python tuple data types are immutable means once they are assigned they cannot be changed further.
      • Set
        • a = {1, 2, 3, 4, 5, 6}
        • In python, sets are used to hold unique elements and they cannot have duplicates init it we insert 6 twice and print will get only one 6 due to set propeties
        • Set is mostly used in machine learning or any mathematical logic
      • Boolean
        • a = True
        • b= False
        • In python boolean values hold two values only Like True or False.
        • Note the first letter is capital in both the values other interpreter will give you an error
      • Dictionary
        • Dictionary holds data in key-value pair format
        • a = { “name”: “Anup”, “last_name”: “Chaurasiya”, “Address”: { “Pincode”: “2222”, “city”: “Thane”}
        • To access the key we use a dot operator with the variable name like below:
          • a.name will result in “Anup”
          • a.Address.Pincode will result in “2222”
      • Range
        • range(start, end)
        • the range mainly works with a looping statement and the loop start with initiating the value of start but ending before that element
        • range(1, 10) will be incremented from 1 to 10
  • Printing In Python:-
    • To print strings or variables in python there are certain ways to do that let’s discuss that all.
    • considering “a” as a variable name then we can print the content inside the variable “a”
      • print(“value of a is “, a)
      • print(f”value of a is {a}”}
      • print(“value of a is {0}”.format(a))
      • print(“value of a is ” + str(a)) ………………note here I have written str() function to convert the integer variable into a string because we can only concatenate string inside the print function
  • Conditional Statement In Python:
    • if condition or if else condition in python we use comparison operator (i.e <=, >=, !=, ==)
    • if the given condition is not satisfied then else part will be executed otherwise if statement will be executed. The syntax for the same is as follows.
      • if a == 2:
        • Some statement
      • elif a ==1:
        • some Statement
      • else:
        • some statement
      • Note: there is a space before some statement of every element otherwise python interpreter will give an indentation error which means not given indent/space before the statement.
    • Loops In Python:
      • While Loop:
        • while a ==4:
          • print(“while loop is executing”)
        • else:
          • print(“else part is executing”)
        • note else part is optional and if the statement is of one line only then we can write it as.
          • while a == 4: print(“while loop is executing”)
      • For in Loop:
        • for a in range(1,10):
          • print(a)
          • this statement will print values from 1 to less than 10
    • we can use the break or continue keywords for controlling the flow of execution in a loop.
      • break: break keyword get out of the loop and continue with the flow of execution.
      • continue: when to continue is encountered it executes the next flow of execution without carrying of below statement of continue keyword in a loop.
    • Functions In Python
      • we can create function’s in python using the ‘def’ keyword.
        • def function_name(parameter):
          • statement
          • return “some statement”
    • Classes In Python
      • class CodeWithDc:
        • def __init__(self, name, last_name):
          • self.name = name
          • self.last_name = last_name
        • def method_name(self, a, b):
          • self.a = a + b
          • return self.a
        • note here self is used for referencing the same instance of that class self name can be anything but it should be the first parameter that is just used for referencing instances.
    • Objects In Python
      • Will create an object of class CodeWithDc
        • cdc_obj = CodeWithDC(“Deepak”, “chaurasia”)
        • print(cdc_obj.name, cdc_obj.last_name)
    • Import In Python
      • Using the “import” keyword we can import methods, classes, objects, OR some prebuilt libraries for computations. for example, if the file cdc.py contains class CodeWithDc then we can access it using the below statement.
        • import cdc
        • from cdc import CodeWithDc
        • cdc_obj = CodeWithDc()
        • Note from keyword is used to pick up particular things from the same file

Leave a Comment Cancel Reply

Exit mobile version