Basic information about python programming language

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. ... Python's simple, easy to learn syntax emphasize
Credit: https://realpython.com/

 Python Introduction

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:

1. web development (server-side),

2. software development,

3. mathematics,

4. system scripting.

Why we will use Python?

1. Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).

2. Python has a simple syntax similar to the English language.

3. Python has a syntax that allows developers to write programs with fewer lines than some other programming languages.

4. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.

5. Python can be treated in a procedural way, an object-oriented way, or a functional way.

What can we do with Python?

1. Python can be used on a server to create web applications.

2. Python can be used alongside software to create workflows.

3. Python can connect to database systems. It can also read and modify files.

4. Python can be used to handle big data and perform complex mathematics.

5. Python can be used for rapid prototyping, or for production-ready software development.

Is syntax hard or easy?

1. Python was designed for readability and has some similarities to the English language with influence from mathematics.

2. Python uses new lines to complete a command, as opposed to other 3. programming languages which often use semicolons or parentheses.

3. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions, and classes. Other programming languages often use curly brackets for this purpose.

What I can learn from this tutorial?

1. The most recent major version of Python is Python 3, which we shall be using in this tutorial. However, Python 2, although not being updated with anything other than security updates, is still quite popular.

2. In this tutorial, Python will be written in a text editor. It is possible to write Python in an Integrated Development Environment, such as Thonny, Pycharm, Netbeans, or Eclipse which are particularly useful when managing larger collections of Python files.


Python Setup

Let's know about the python setup. Don't be panic. I am talking about a very easy method to set up your Pc/laptop ready to code with python.

Let's get started!!!

No 1. Python IDE

Many PCs and Macs will have python already installed. But if you haven't downloaded it you can download it from here  https://www.python.org/
Note: Make sure you have downloaded the latest version of Python. In this tutorial we will work with Python 3. Make sure you doownloaded 
the latest version. 

Now let's check if we downloaded the latest version of Python. Please follow these steps-

 Search in the start bar for Python or run the following on the Command Line   (cmd.exe):

C:\Users\Your Name>python --version
For Linux users open the command line and Mac users open the terminal then write
python --version
Now you are ready to code.

No 2. PyCharm

Let's know about PyCharm. PyCharm is a dedicated Python Integrated Development Environment (IDE) providing a wide range of essential tools for Python developers, tightly integrated to create a convenient environment for productive Python, web, and data science development.

PyCharm has 2 different versions:
1. Professional
2. Community

We recommend you to use the Community version first then use Professional. One more thing PyCharm is also available on Mac and Linux.

Want to download PyCharm?

No 3. Visual Studio Code

Let's know about our last IDM VS code. Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS, and Linux. It comes with built-in support for Python, JavaScript, TypeScript, and Node.

Want to download VS Code?




Python Syntax

In this lesson, you will know what is the basic syntax. If you never write a single code ever don't worry after this lesson you will be able to write a very basic code. Now let's jump to the lesson- 

In the previous lesson, you know how to download an IDE. Now open our IDE and write the following code-

>>> print("Hello, World!")

Hello, World!


Don't be afraid to see the code if you choose PyCharm and VS Code you should type this code-

print('Hello, World!')

and your input is Hello, World!

Here you will see we write print(). In python called it to print() function. The print() function prints the specified message to the screen or other standard output device. The message can be a string or any other object, the object will be converted into a string before being written to the screen.

Now, we wrote Hello, World! inside the print() function that's the value of the function.


Python Indentation

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.

Let's see an example to clear this-

if 5 > 2:
  print("Five is greater than two!")
You will see an Error if you skip the Indentation-
Let's see an example-
if 5 > 2:
print("Five is greater than two!")

Wrong-Way

So, be careful about Indentation.

One more thing if you give multiple spaces as an Indentation no problem.

Let's see an example:

if 5 > 2:
 print("Five is greater than two!"

if 5 > 2:
        print("Five is greater than two!"

Another thing if you do not use the same number of spaces in the same block of code you will see an Error.

Let's see an example:

if 5 > 2:
 print("Five is greater than two!")
        print("Five is greater than two!")

Wrong-way

Hopefully, you are all enjoying our tutorial. If you have any questions you can post a comment and also send mail via Contact Form. Stay sticky with us and enjoy our tutorial. 


Python Variables

In Python, variables are created when you assign a value to it-
x = 5
y = "Hello, World!"
Python has no command for declaring a variable.

You will learn more about variables in the Python Variables chapter.

Comments

Python has a commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
#This is a comment.
print("Hello, World!")

You will learn more about variables in the Python Comments chapter.


Python Comments

Q . What is the main purpose to use comments?

Ans: Comments can be used to explain Python code.

         Comments can be used to make the code more readable.

         Comments can be used to prevent execution when testing code.

You have already know which purposes we use comments. Ok alright now we tell you about comments in more detail.

Ok, we know why we use comments but if you know how you start to write a comment? 

The answer is just follow!!!

Comments start with a #. Now see an example to clear your doubt.

#This is a comment
print("Hello, World!")
You know how to write a single-line comment. Maybe one question rounding in your head "How to write multi-line comments?" Ok wait we will tell you everything

Here is two way to write multi-line comments-
1. To add a multiline comment you could insert a for each line.
2. you can add a multiline string (triple quotes) in your code, and place your comment.

Here are some examples-
#This is a comment
#written in
#more than just one line
print("Hello, World!")
"""
This is a comment
written in
more than just one line
"
""
print("Hello, World!")


Python Variables

In this lesson, you will learn about python variables. For your help we divide this lesson into five parts:
1. Python Variables
2. Variable Names
3. Assign Multiple Values
4. Output Variables
5. Global Variables

Variables

What are variables?

A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing. Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc.

No need to memorize this definition just remember Variables are containers for storing data values.

Creating Variables

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they have been set.
x = 4       # x is of type int
x = "Sally" # x is now of type str
print(x)

Get the Type

In this lesson, you should know what is type() function and what it works, and how to use it. In this lesson, you will know everything. Be patient and read this lesson properly.
x = 5
y = "John"
print(type(x))
print(type(y))

Single or Double Quotes?

String variables can be declared either by using single or double quotes. Let's jump to the Example.

x = "John"
# is the same as
x = 'John'

This thing comes in handy when you have to include a single or double quote in your text. Like, when you want to print, Dalai Lama said, “The purpose of our lives is to be happy”. We have to assign it like this -
print (‘Dalai Lama said, “The purpose of our lives is to be happy”’).
And if that sentence included single quotes, we had to write it like,
 print (“Dalai Lama said, ‘The purpose of our lives is to be happy’”)
So, it should be clear that if you use single quotes as your wrapping, you have to use double quotes inside, if you want to, single quotes won’t work, as the single quote will end the statement.

Case-Sensitive

Python is a case-sensitive language. This means variables are not the same. Multiple words can be separated using an underscore. Do Variable names are case-sensitive.
a = 4
A = "Sally"
#A will not overwrite a

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, car name, total_volume). Rules for Python variables:

1. A variable name must start with a letter or the underscore character

2. A variable name cannot start with a number

3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

4. Variable names are case-sensitive (age, Age, and AGE are three different variables)

RIGHT WAY

myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

WRONG WAY

2myvar = "John"
my-var = "John"
my var = "John"

Multi Words Variable Names

Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable:

Camel Case

Each word, except the first, starts with a capital letter:

my_variable_name = "John"

Pascal Case

Each word starts with a capital letter:

MyVariableName = "John"

Snake Case

Each word is separated by an underscore character:

my_variable_name = "John"


Assign Multiple Values

Python allows you to assign values to multiple variables in one line:

x, y, z = "Orange""Banana""Cherry"
print(x)
print(y)
print(z)

One Value to Multiple Variables

you can always assign the same value to multiple variables in a single line:

x = y = z = "Orange"
print(x)
print(y)
print(z)

Unpack a Collection

If you have a collection of values in a list, tuple. Python allows you to extract the values into variables. This is called unpacking.

fruits = ["apple""banana""cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)

Output Variables

The Python print statement is often used to output variables. For combining both text and a variable, Python uses the + character:

x = "awesome"
print("Python is " + x)
You can use the character to add a variable to another variable:
x = "Python is "
y = "awesome"
z =  x + y
print(z)
The + chracter can use for matmathical operator:
x = 5
y = 10
print(x + y)
If you try to combine a string and a number, Python will give you an error. Here is the example below:
x = 5
y = "John"
print(x + y)

Global Variables

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

x = "awesome"

def myfunc():
  print("Python is " + x)

myfunc()
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

The global Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
Also, use the global keyword if you want to change a global variable inside a function.
x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)

Connect with us to know more. Soon we will explain every single item upcoming article.