Python is considered as a base or startup language for new programmers because of its simplicity, ease to write English sentence like syntax. Syntax is basically structure and set of rules for programming language such as we have English, Hindi, or Urdu. Each programming language has a different syntax. In this article we will go through some basics of python and we will write our first code
So, we will start with the first code to print hello python on the screen. To tell computer In python to write some text to the screen we have to use keyword print like this
Print() is a built-in function in Python to display things on screen. Built-in function is predefined in the language and can be used directly. Now let us run this on the IDE or CMD whatever your preference is. Remember whatever you want to display remains in quotes “ “.
Congratulations we have written our first python program. Give it a try yourself!
Now the next thing is comments. Comments are basically written so that the function performed could be understood easily. For example, you multiplied two numbers in your code, so you write that here I multiplied some numbers for some other function I want to perform to remind yourself when you open your code again. Comments are optional but an important part and good programming practice. How to write comments in python? Well let us see here.
Any sentence followed by a “ # ” is comment. Comments do not affect program flow as they are ignored by compiler.
Data type is attribute of data that defines which type of value(s) data possess or what is the type of data on which we will perform functions. Is it a number, sentence or characters?
Python has various data types like other languages. That provide support to different types to data. Most used data types are
- Floating number
- Long INT
- Boolean (True or False)
- Integer (int)
Next thing is variables. In programming variables are used to store some values such as a bottle is used to store water, or a box is used to store something just like that. These values can be either numbers, alphabets, sentence, characters etc. Python is smart enough to understand what type your variable does store when it is declared despite other languages in which you must mention for example in C++to store integer in variable you have to mention int. To store a value in variable in python we use
Variable name = value to be stored
Let us do this. Here a is variable name and 60 is value stored in it.
NOTE: to store string we must write inside double quotes such as “c” variable value as in the example
Let us consider you are writing a program to multiply two numbers; how will you know those two numbers? Either you will assign two numbers yourself or you will ask user of your program to assign two number to get data from user . So how you can do that? Let us understand this with an example.
Note: Remember while programming you must keep in mind that you are developing the program for user so think from user perspective
In this example we asked user to input First number and second number for which he want multiplication. Then simply multiplied two variables in which user input was taken by using Input() function as explained earlier
Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.
Remember while using the variables we must define their type such as we defined integer type in above example. It is python language rule to define the type while using variables unlike other languages which require to define type while declaration of variable
Practice your Code online here
Software engineer| technical writer and a cybersecurity enthusiast