Basic Syntax and Structure in Python

Code Computer Skills Python Web Development

Now that you have the IDE ready, it’s time we actually write some code to see how python works. I will introduce you to the basic syntax and structure of Python. Like every programming language, we will test the baby program i.e. the Hello World! program.

Creating a New Project

  1. Select Create New Project in the first opening page.
    Pycharm Create New Project
  2. Give your project any name you like. The default name is untitled. Here we have our program name as testpython.Pycharm New Project Name
  3. Create your file. Next, you will see the IDE creating some Virtual Environment. It is a setup to establish the Python Interpreter and Debugger in the back-end. You will find this option triggering every time you start a new python program.
    Pycharm New Project Set up
  4. Next, we need to create an editable python file under our project name. To do that, right click on your project name (here it is testpython) -> New -> Python File and click on it.Pycharm New Python File Name
  5. You will see a dialogue box prompting you to enter a name. Keep the name the same as that of your project. Then click on OK.
  6. Now you can see an editor with the text cursor blinking. This is the place where we will write all our code for the next few modules and test different features of Python. Go on! Give it a shot with the following code:
    # to test a simple python program to print Hello World!
    print("Hello World!")
  7. Next, you have to execute the program. You can press the shortcut combination of Alt+Shift+F10 or else go to Run in the menu above and select Run… from the given options.PyCharm Run Python File
  8. You can see a tab popping up from below showing the path, your OUTPUT and with an exit code.

    Run PyCharm Hello World

    Congratulations! You ran your first Python program successfully. And I know, most of you must be thinking how easy it is to print a statement as compared to other programming languages.

    Points to remember

    • In python programming, python interpreter treats each line as a syntax. And it is read by the python parser.
    • A comment in python is followed by a hash ‘#’ symbol. Comments are allowed only on a single line. There is no multiline or block comment facility.
    • If your syntax is too long to fit in one line then it can be broken into multiple lines by using the backslash ‘\’ character.
    • Python supports multiple syntax statements in one line. However, you need to separate each line followed by a semi-colon ‘;’.

    Pycharm Output

      • NOTE: There is a small underline at line number 4 at the semi-colon. The warning suggests avoiding Multiple statements in one line. And in actual practice as well, we should try to write code individually in each line. It enables good programming practice over time.
    • Python language doesn’t support any form of brackets while modularizing condition statements. The code is modularized using white spaces and indentations as and whenever necessary. We will get to know more about it once we get started with conditions in Python.
    • A total of 33 reserved words are there in python. These special words cannot be used as ordinary identifiers in the program. The words are:

    False     class     finally     is     return     None

    continue     for     lambda     try     True     def

    from     nonlocal     while     and     del     global

    not     with     as     el     if     or

    yield     assert     else     import     pass

    break     except     in     raise

    Now since you are familiar with the basic structure and formatting of program syntax, it’s time to learn the various type of data we can deal within Python and how they are taken as input.