In the Python Tutorial Volume 1: Introduction to python Programming we discussed introduced you to python, a little background, how to check for python, where to download and install python and how to run the program using command line. We even wrote our first program. I think its fun to right?
In Python Tutorial Volume 1, We learnt that Python syntax can be executed by writing directly in the Command Line:
and it can also be executed by creating a python file on the server, using the .py file extension, and running it in the Command Line:
In this volume we are going to be discussing about the syntax, Variable and Numbers in Python.
but before we get there i would like to briefly define our Terms
Syntax: Syntax refers to the programming language spelling and grammar. Computers are inflexible machines that only understand what you type if you type it in the exact form expected by the computer. The form expected is called syntax. we will discuss this syntax under the following Headings
In other Programming Languages indentation is only for the sake of clarity and readability but in Python indentation is very important.
Python uses indentation to specify a code block.
Below is the image of a correct Indentation and the corresponding Output
Below is the image of a WRONG Indentation and the corresponding Output
Forgive me i cant draw a circle with a bottle base *wink* but not the circled Error is solely because of absence of expected Indentations.
the second Syntax i would like to talk about is
Comments in Pythons
Python allows Commenting to encourage in-code Documentation.
Every Comment in python starts with a #, and Python will renders what ever follows as a comment:
#This is an example of a comment in python.
The above code Renders the first Line as Comment and proceeds to the Second line and print the Hello World to screen.
then Next Syntax is
Docstrings in Python (Extended Documentation)
Python has also increased the ability to document, called docstrings.
This can be one line, or multiline. this is just like the multiline Comment Variation in other programming Languages
Python uses Three quotation Marks (“””) at the beginning and end of the docstring:
“””This is a
Single line docstring.”””
“””This is a
multiple line docstring.”””
Variable:A variable is a value that can change in programming depending on the conditions or information passed to the program. A program typically consists of instructions telling the computer what to do and the data the program uses when it runs.
Unlike other programming languages, Python Does not require Data Type declaration when declaration a Variable
Data types is Assigned to a variable the moment a value is assigned to it.
Type the following in your text Editor
y = “Knowledgebase”
x = “Knowledgebase” # x is now string data type
Variable Declaration Rules
Just like in any other Programming Languages, Python also has some set of rules that guides its declaration of Variables as highlighted Below:
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Any other thing not Violating this rules is acceptable by Python.
It is important to Note That: Variables are case sensitive ( Which means A and a does not mean the same thing).
The Python print command is often used to Produce an output variables.
we usually use the + sign to combine a text and a Variable
print(“Python Tutorial ” + x)
the + character can also be used to add a variable to another variable:
y = “Beginners”
z = x + y
When operating on number the + character serves as numerical operator
y = 11
print(x + y)
Please Note that you can not use the + character to operate on string and Integers
y = “KnowledgeBase”
print(x + y)