First of all! Congratulations on getting python setup on your computer. In the last section, we were able make and run our very first python script
Hello, World!!!. In this tutorial we’ll learn some basic python scripts that will help you understand the working of Python.
A variable is a kind of container or a box storing information. Just like a box in our daily life, stores something, variable in python contains information. Which means a variable will have a name and some value.
name = "Techie"
In the above example, name is name of the variable and “Techie” is the value. Note that the name of the variables are case sensitive which means that if you type
name and press enter, it ‘ll give you the below output –
>>> name = "Techie" >>> name "Techie"
But if you’ll type
NAMEand press enter, you’ll get a similar error –
>>> name = "Techie" >>> NAME Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'NAME' is not defined
We also need to make sure that we don’t use Python keywords as variable names as they are reserved words in Python.
Variable name is also called an Identifier. An identifier is basically a name given to variables, class, functions, etc to differentiate from another. There are a few rules that you need to keep in mind while writing identifiers –
- An identifier cannot start with a number/digit (0-9) but can end with one. 1name is invalid but name1 is valid.
- It can be a combination of lowercase letters (a-z), uppercase letters (A-Z), numbers (0-9) or an underscore
_. Names for example,
- You cannot use Keywords as Identifiers as explained previously.
- Special symbols like !, @, #, $, %, etc, cannot be used as an Identifier.
Just like Python is a dynamically typed language, variables are also dynamically typed which means each variable will have a certain type. We can check this by using a
>>> name = "Techie" >>> type(name) <class 'str'> >>> number = 1 >>> type(number) <class 'int'>
For name, it shows
str which means the variable holds a string value while
number shows int which means it hold an integer value. It is different from other programming languages where we have to define the type of variable before creating one.
So, we learned that variables are certain type of boxes that store information. Each variables has a name and a value. Each information stored in a variable has a certain type as python is a dynamically typed language.
Indentation in Python
Python uses blank spaces in the beginning of a statement to identify a block of code. This is generally used in conditional statements like if…else and loops like for and while loop. Python by default uses 4 spaces for indentation but you can change this and use 2 spaces as well. Let’s check this by the below syntax.
if (condition): statement else: statement
If you don’t use indentation, it will give you an error when the code is executes.
IndentationError: expected an indented block
Comments in Python
In Python, hash symbol
# is used to write comments. Comments are used to write an explanation to a block of code like what will this code do.
# this is a comment a = 1 b = 2
Python also supports multi-line comments and this is done using triple quotes
''' at the beginning and end of a comment.
''' This is a multi line comment '''
Getting User’s Input
input() function to get input from the user. when this function is executed, the program will wait until the user has provided the input and pressed enter key.
>>> name = input("Enter your name: ") Enter your name: Techie >>> name 'Techie'
In this example, we took input from the user and assigned it to the name variable. In the next line, we access the name variable.
How to display an output in Python
Now we know how to store value in a variable and taking user’s input. Now how do we display the output? We use print() function to display anything.
>>> print("Hello world!!!") Hello world!!! >>> print("Techie Hours") Techie Hours