Basic information about python programming language
Credit: https://realpython.com/ |
Python Introduction
What is Python?
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
No 1. Python IDE
Note: Make sure you have downloaded the latest version of Python. In this tutorial we will work with Python 3. Make sure you doownloadedthe latest version.
Now let's check if we downloaded the latest version of Python. Please follow these steps-
C:\Users\Your Name>python --version
python --version
No 2. PyCharm
No 3. Visual Studio Code
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!")
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
x = 5
y = "Hello, World!"
Comments
#This is a comment.
print("Hello, World!")
You will learn more about variables in the Python Comments chapter.
Python Comments
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!")
#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
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)
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Get the Type
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
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
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)
x = "Python is "
y = "awesome"
z = x + y
print(z)
x = 5
y = 10
print(x + y)
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()
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)
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.
1 comment