PYTHONTUTORIALS

Python Tutorial Volume 2 Part 1: Python Syntax,Variables and Numbers

0
python tutorials

Hits: 305

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 1We learnt that Python syntax can be executed by writing directly in the Command Line:

 print(“Hello, World!”)
Hello, World!

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:

C:\Users\Your Name>python myfile.py

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

  1. Python Indentations

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

Correct Indentation

Correct Indentation

Correct Indentation output

Correct Indentation output

Below is the image of a WRONG Indentation and the corresponding Output

Not Indented in Python

Not Indented in Python

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:

E.G

#This is an example of a comment in python.
print(“Hello, World!”)

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.

Creating Variables

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.

Example

Type the following in your text Editor

x = 5
y = “Knowledgebase”
print(x)
print(y)
output
5
Knowledgebase
the Variables in python can be changed  even after it has been created

Example

x = 78  # x is an integer data type
x = “Knowledgebase” # x is now string data type
print(x)
output
Knowledgebase

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).

Displaying  Variables

The Python print command is often used to Produce an output variables.

we usually use the + sign to combine a text and a Variable

Like Below

x = “By Knowledge”
print(“Python Tutorial ” + x)
Output
Python Tutorial By Knowledge

the + character  can also be used to add a variable to another variable:

Example

x = “Python Tutorial for ”
y = “Beginners”
z =  x + y
print(z)
Output
Python Tutorial for Beginners

When operating on number the + character serves as numerical operator

Example

x = 9
y = 11
print(x + y)
Output
20

Please Note that you can not use the + character to operate on string and Integers

Example

x = 8
y = “KnowledgeBase”
print(x + y)
The above Block of code above will definitely flag an error because you are attempting to add an integer to a string.
Please Dont forget to drop your Comment as it encourages us to do more or to improve Quality. Dont hesitate to Drop your Questions we will be glad to help.
You can also read the last volume Here

Related Post

READ  How To Use Team Viewer to Remotely Control Your Friends Computer

Python Tutorial Volume 1: Introduction to python Programming

Previous article

How to Flash Any Number Without Airtime with Your Airtel Sim

Next article

You may also like

Comments

Leave a reply

Your email address will not be published.