Change language

Python 3 basics

| |

Therefore, before moving on & # 8230; let’s follow the most popular tradition of "HelloWorld" and therefore compare the syntax of Python with C, C++, Java (I took these 3 because they are the most famous and mostly used languages).

# Python code for "Hello World"
# don’t print anything else ... let’s see how simple the syntax is.


print ( "Hello World"

Note: Note that Python does not depend on curly braces ({}) for its scope, instead it uses indentation for your area. 
Now moving on Let’s start our Python basics . I’ll cover the basics in some small sections. Just go through them and trust me, you can learn Python basics very easily.

  • If you are running in Linux / Unix mode , just open a terminal and on 99% Linux OS Python comes pre-installed. Just enter "python3" in the terminal and you are ready to go. 
    It will look like this:
  • The ”& gt;"" ”Represents the python shell and its ready to take python commands and code.

    Variables and data structures

    In other programming languages ​​such as C, C++ and Java, you will need to declare a type variables, but in Python you don’t need to do that. Just enter a variable and when values ​​are passed to it, then it will automatically know if the given value will be int, float or char or even String.

    # Python- program for declaring variables

    myNumber = 3

    print (myNumber)


    myNumber2 = 4.5

    print (myNumber2)


    myNumber = "helloworld"

    print (myNumber)

    < / td>

    Exit :

     3 4.5 helloworld 

    See how easy it is, just create a variable and assign it whatever value you want, then use the print function to print it. Python has 4 types of built-in data structures, namely:

    # Python program to illustrate the list

    # creates an empty list

    nums = [] 

    # add data to the list

    nums.append ( 21 )

    nums.append ( 40.5 )

    nums.append ( "String" )


    print ( nums)


     [21, 40.5, String] 

    Comments :

     # is used for single line comment in Python "" "this is a comment "" "is used for multi line comments 

    Login and Logout

    In this section, we will learn accept input from the user and therefore manipulate it or just display it. The input () function is used to receive input from the user.

    # Python program for illustration
    # getting information from the user

    name = input ( "Enter your name:" )

    # user entered name & # 39; harssh & # 39;

    print ( "hello" , name)


     hello harssh 

    # Python3 program to get user input

    # accepts an integer from the user

    num1 = int ( input ( " Enter num1: " )) 

    num2 = int ( input ( "Enter num2:" ))


    num3 = num1 * num2

    print ( "Product is:" , num3)


     Enter num1: 8 Enter num2: 6 (’Product is:’, 48) 


    Selection in Python is done using two keywords & # 39; if & # 39; and & # 39; elif & # 39; and else (elseif)

    # Python program to illustrate
    # select a statement


    num1 = 34

    if (num1" 12 ):

    print ( "Num1 is good" )

    elif (num1" 35 ):

    print ( "Num2 is not gooooo ...." )

    else :

    print ( " Num2 is great " )

    Exit :

     Num1 is good 


    You You can think of functions as a collection of code that is designed to perform a specific task throughout the entire Python script. Python used the "def" keyword to define a function. 
    Syntax :

     def function-name (arguments): #function body 

    # Python program for illustration
    # functions

    def hello ():

    print ( " hello " )

      print ( " hello again " )

    hello ()

    # calling function
    hello () 


     hello hello again hello hello again 

    Now, as we know, any program starts from a "main" function ... let’s create a main function like in many other programming languages.

    # Python program for illustration
    # main function

    def getInteger ():

      result = int ( input ( " Enter integer: " ))

    return result


    def Main ():

    print ( " Started " )

    # call the getInteger function and
    # save the returned value in the output variable

    output = getInteger () 

    print (output)

    # now we must tell Python
    # for the "Main" function to exist

    if __ name__ = = "__ main__" :

    Main ()


     Started Enter integer: 5 

    Iteration (looping)

    As the name suggests, this repeats over and over. Here we will use the most popular for loop.

    # Python program for illustration
    # simple loop


    for step in range ( 5 ): 

    print (step)

    Exit :

     0 1 2 3 4 


    Python has a very rich library of modules that has several functions to accomplish many tasks. You can read more about the Python standard library by

    # Python program for illustration
    # math module

    import math


    def Main ():

    num = float ( input ( "Enter a number: " ))

      # fabs is used to get the absolute decimal value

    num  = math.fabs (num) 

      print (num)

    if __ name__ = = "__main__" :

    Main ( )


     Enter a number: 85.0 

    These are some of the very basics of the Python programming language, and I will cover both intermediate and advanced Python topics in my next articles.

    This article courtesy of Harsh Wardhan Chaudhary . If you are as Python.Engineering and would like to contribute, you can also write an article using or by posting an article contribute @ Check out your article on the Python.Engineering homepage and help other Geeks users. Your article will first be reviewed by the Geeks for Geeks team prior to publication.

    Please write in comments if you find anything wrong or if you would like to share more information on the topic discussed above.


    Learn programming in R: courses


    Best Python online courses for 2022


    Best laptop for Fortnite


    Best laptop for Excel


    Best laptop for Solidworks


    Best laptop for Roblox


    Best computer for crypto mining


    Best laptop for Sims 4


    Latest questions


    Common xlabel/ylabel for matplotlib subplots

    12 answers


    How to specify multiple return types using type-hints

    12 answers


    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers


    Flake8: Ignore specific warning for entire file

    12 answers


    glob exclude pattern

    12 answers


    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers


    Python CSV error: line contains NULL byte

    12 answers


    csv.Error: iterator should return strings, not bytes

    12 answers


    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python


    How to specify multiple return types using type-hints


    Printing words vertically in Python


    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries


    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically