Python Introduction
What is Python?
Python is a famous programming language. It was once created with the aid of
Guido van Rossum, and launched in 1991.
It is used for:
- web improvement (server-side),
- software development,
- mathematics,
- system scripting.
What can Python do?
- Python can be used on a server to create net applications.
- Python can be used alongside software program to create workflows.
- Python can join to database systems. It can additionally study and alter files.
- Python can be used to manage huge information and function complicated mathematics.
- Python can be used for fast prototyping, or for production-ready software program development.
Why Python?
- Python works on specific systems (Windows, Mac, Linux, Raspberry Pi, etc).
- Python has a easy syntax comparable to the English language.
- Python has syntax that approves builders to write packages with fewer traces than some different programming languages.
- Python runs on an interpreter system, that means that code can be accomplished as quickly as it is written. This potential that prototyping can be very quick.
- Python can be handled in a procedural way, an object-oriented way or a purposeful way.
- The most current essential model of Python is Python 3, which we shall be the use of in this tutorial. However, Python 2, even though now not being up to date with something different than safety updates, is nevertheless pretty popular.
- In this tutorial Python will be written in a textual content editor. It is feasible to write Python in an Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which are in particular beneficial when managing large collections of Python files.
Python Syntax in contrast to other programming languages:-
- Python used to be designed for readability, and has some similarities to the English language with affect from mathematics.
- Python makes use of new strains to entire a command, as adversarial to different programming languages which regularly use semicolons or parentheses.
- Python depends on indentation, the usage of whitespace, to outline scope; such as the scope of loops, features and classes. Other programming languages regularly use curly-brackets for this purpose.
Example:-
print("Hello, World!")
Variables
Variables are containers for storing statistics values.
Creating Variables:-
- Python has no command for declaring a variable.
- A variable is created the second you first assign a cost to it.
Example:-
x = 5
y = "John"
print(x)
print(y)
Variables do now not want to be declared with any unique type, and can even trade kind after they have been set.
Example:-
x = four # x is of kind int
x = "Sally" # x is now of kind str
print(x)
Try it Yourself»»
Casting:-
If you favor to specify the facts kind of a variable, this can be performed with casting.
Example:-
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Try it Yourself»»
Get the Type:-
You can get the information kind of a variable with the type() function.
Example:-
x = 5
y = "John"
print(type(x))
print(type(y))
String variables can be declared both with the aid of the use of single or double quotes:
Example:-
x = "John"
# is the identical as
x = 'John'
Try it Yourself»»
Case-Sensitive:-
Variable names are case-sensitive.
Example:-
This will create two variables:
a = 4
A = "Sally"
#A will now not overwrite a
Try it Yourself»»
Python - Variable Names
Variable Names:-
A variable can have a brief title (like x and y) or a extra descriptive title
(age, carname, total_volume). Rules for Python variables:
- A variable identify ought to begin with a letter or the underscore character.
- A variable identify can't begin with a number.
- A variable identify can solely include alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (age, Age and AGE are three exceptional variables).
Example:-
Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Try it Yourself»»
Example:-
Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Multi Words Variable Names:-
Variable names with greater than one phrase can be tough to read.
There are countless methods you can use to make them extra readable:
Camel Case:-
Each word, without the first, starts off-evolved with a capital letter:
myVariableName = "John"
Pascal Case:-
Each phrase begins with a capital letter:
MyVariableName = "John"
Snake Case:-
Each phrase is separated by means of an underscore character:
my_variable_name = "John"
Python Variables - Assign Multiple Values
Many Values to Multiple Variables:-
Python approves you to assign values to more than one variables in one line:
Example:-
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Try it Yourself»»
Note: Make positive the wide variety of variables suits the quantity of values,
or else you will get an error.
One Value to Multiple Variables:-
And you can assign the equal cost to a couple of variables in one line:
Example:-
x = y = z = "Orange"
print(x)
print(y)
print(z)
Try it Yourself»»
Unpack a Collection:-
If you have a series of values in a list, tuple etc. Python lets in you extract
the values into variables. This is referred to as unpacking.
Example:-
Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Try it Yourself»»
Python - Output Variables
Output Variables:-
The Python print declaration is regularly used to output variables.
To mix each textual content and a variable, Python makes use of the +
character:
Example:-
x = "awesome"
print("Python is " + x)
Try it Yourself»»
You can additionally use the + personality to add a variable to every other
variable:
Example:-
x = "Python is "
y = "awesome"
z = x + y
print(z)
Try it Yourself»»
For numbers, the + persona works as a mathematical operator:
Example:-
x = 5
y = 10
print(x + y)
Try it Yourself»»
If you strive to mix a string and a number, Python will provide you an error:
Example:-
x = 5
y = "John"
print(x + y)
Try it Yourself»»
Python - Global Variables
Global Variables:-
Variables that are created outdoor of a characteristic (as in all of the
examples above) are acknowledged as international variables.
Global variables can be used by means of everyone, both inner of features and
outside.
Example:-
Create a variable outdoor of a function, and use it interior the function
x = "awesome"
def myfunc( ):
print("Python is " + x)
myfunc( )
If you create a variable with the equal title interior a function, this
variable will be local, and can solely be used internal the function. The world
variable with the equal title will stay as it was, global and with the unique
value.
Example:-
Create a variable internal a function, with the equal identify as the global variable
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc( )
print("Python is " + x)
Try it Yourself»»
The world Keyword:-
Normally, when you create a variable internal a function, that variable is
local, and can solely be used internal that function.
To create a global variable internal a function, you can use the global keyword.
Example:-
If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Try it Yourself»»
Also, use the global key-word if you desire to trade a world variable interior a
function.
Example:-
To exchange the fee of a world variable inner a function, refer to the variable
through the use of the world keyword:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Try it Yourself»»
Python Comments
- Comments can be used to provide an explanation for Python code.
- Comments can be used to make the code extra readable.
- Comments can be used to stop execution when trying out code.
Creating a Comment:-
Comments begins with a #, and Python will skip them:
Example:-
#This is a comment
print("Hello, World!")
Try it Yourself»»
Comments can be positioned at the quit of a line, and Python will bypass the
relaxation of the line:
Example:-
print("Hello, World!") #This is a comment
Try it Yourself»»
A remark does no longer have to be textual content that explains the code, it
can additionally be used to forestall Python from executing code:
Example:-
#print("Hello, World!")
print("Cheers, Mate!")
Multi Line Comments:-
Python does no longer truly have a syntax for multi line comments.
To add a multiline remark you should insert a # for every line:
Example:-
#This is a comment
#written in
#more than simply one line
print("Hello, World!")
Try it Yourself»»
Or, no longer pretty as intended, you can use a multiline string.
Since Python will skip string literals that are now not assigned to a variable,
you can add a multiline string (triple quotes) in your code, and location your
remark inner it:
Example:-
"""
This is a comment
written in
more than simply one line
"""
print("Hello, World!")
Try it Yourself»»
As lengthy as the string is now not assigned to a variable, Python will study
the code, however then skip it, and you have made a multiline comment.
0 Comments
If you are a good learner please leave a challenging question on comment box for us when you visited this website ( Coding with Fun).