Sie sind auf Seite 1von 7

PYTHON-VARIABLES

(You first step begins with a snake__)

Hello everyone I am Virat Singh and today I am going tell you


about Variables in Python.

First of all What is Python?(Its not really a snake afterall :))


Well according to Devlopers-

Python is a powerfull and fast programming language.


Runs everywhere(i.e platform independent, means you can
run it in diffrent operating system like Windows,Linux etc.)
It is very friendly and easy to learn.
Its opensource(software for which the original source code
is made freely available and may be redistributed and
modified.)

So these are some general points on Python.To add some


more python is a interpreting language or say scripting
language which means it process the commands or program
written in it line by line during the runtime.So you do not need
to compile your program before executing it.This is kind of
simlar to Perl and PHP programming languages.
I know if you guys are new to programming you will be like, ok
how is this going to help me! I dont know but its just some
usefull information.

So lets begin and we are starting with our first topic which is
Variables , First thing you will ask me is what are variables?
I would say variables are really boxes,boxes that contain some
values or say information but before we get into that you need
to know where to write python code because you cant just run
python on your computer.
So for this you can download Python for Windows, OS X, and
Ubuntu for free from http://python.org/downloads/ .Youll
find Python installers for 64-bit and 32-bit computers for each
operating system on the download page, so first figure out
which installer you need. If you bought your computer in 2007
or later, it is most likely a 64-bit system(but be sure).
There are mainly two versions of python availaible Python2.7
and Python 3 (such as 3.4.0) theres a bit difference between
the two but as a beginner it doesnt really matter which
vesrion you download but I will recommend you to install
python 3 as I am using it to explain Python to you all.

So now we have our python installed ,you now need an actual


platform or say shell to write our python code, but not
spending much time on how to set up that or I may tell you
that later plus you can search across the web or youtube
because there are lots of videos just for that and for diffrent
operating system as well.

So lets go to our shell it looks something like this--


(I want to make clear to you all that I am using Ubuntu
operating and above picture is of terminal where python3 is
opened.)
But the shell in any operating system looks similar to this.

So coming back to What are variables?


As i said earlier variables are boxes which are used to store
information,the computer program has a memory but it has a
limited memory just like we do not have unlimited memory, so
what we are going to do is reserve a space in computers
memory so that the computer remembers it and in order to do
so we use variables and in python thats very easy.

Enter this in your python shell--


>>> 2 + 2
4
>>>

In Python, 2 + 2 is called an expression, which is the most


basic kind of programming instruction in the language.
Expressions consist of values (such as 2) and operators (such
as +,there are many other you may know later), and they can
always evaluate (that is, reduce) down to a single value. That
means you can use expressions anywhere in Python code that
you could also use a value.

In the previous example, 2 + 2 is evaluated down to a single


value, 4. A single value with no operators is also considered
an expression, though it evaluates only to itself, as shown
here:

>>> 2
2
Errors are Okay!

Programs will crash if they contain code the computer cant


understand, which will cause Python to show an error
message. An error message wont break your computer,
though, so dont be afraid to make mistakes. A crash just
means the program stopped running unexpectedly.Just ponder
and try to solve the error yourself or use the web.

So what if you want to store the value of the expression


evaluated in above example(>>>2+2) for this we need
variables.If you want to use the result of an evaluated
expression later in your program, you can save it inside a
variable.

Youll store values in variables with an assignment statement.


An assignment statement consists of a variable name, an
equal sign (called the assignment operator), and the value to
be stored. If you enter the assignment statement king = 1,
then a variable named king will have the integer value 1
stored in it.We will see more about variable names later below.
>>>king = 1
And now if you type king in your shell it will display the value
stored in your box named king or a space of named king or
whatever you want to say .
>>>king
>>>1
enter the following into the interactive shell:
>>> king = 1
>>> king
1
>>> queen = 2
>>> king + queen
3
>>> king + queen + king
4
>>> king = king + 2
>>> king
3

A variable is initialized (or created) the first time a value is


stored in it . After that, you can use it in expressions with
other variables and values . When a variable is assigned a
new value , the old value is forgotten, which is why king
evaluated to 3 instead of 1 at the end of the example. This is
called overwriting the variable.This means the box named
king which earlier contained the value 1 , value 1 is thrown
out of it and new value 3 is put or stored in it.
Variables does not only store number but can also store string
values such as word or sentences for example-
>>>king1 = hello everyone
>>>king1
hello everyone

(dont forget to use quotation single or double( or ) while


asssignment of a string value to a variable otherwise python
will display a error, try it yourself of you dont believe me.)

So now we know we can store different values like 1 , hello


everyone etc in a variable but what are these values ofcourse
we know that 10 is an integer number and hello everyone is
a string of characters, but in python the diffrent types of values
stored have a type and these are called as data types.
So the,
Common Data Types Are

Data type Examples

Integers -2, -1, 0, 1, 2, 3, 4, 5

Floating-point numbers -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25


(means number with decimal point)

Strings 'a', 'aa', 'aaa', 'Hello!', '11 cats'

(type -- 0.5 in your shell and see what happens? )

Variable Names:
In previous examples we used variable names like king and
queen etc .You can name a variable anything as long as it
obeys the following three rules:

It can be only one word.


It can use only letters, numbers, and the underscore (_)
character.
It cant begin with a number.

Variable names are case-sensitive, meaning that king, KING,


King and kInG are four different variables. It is a Python
convention to start your variables with a lowercase letter.
Some Valid and Invalid Variable Names
(Examples)

Valid variable names Invalid variable names

Balance current-balance (hyphens are


not allowed)

CurrentBalance current balance (spaces are


not allowed)

current_balance 4account (cant begin with a


number)

_spam 42 (cant begin with a


number)

SPAM total_$um (special characters


like $ are not allowed)

Account4 'hello' (special characters like '


are not allowed)

A good variable name describes the data it contains. Imagine


that you moved to a new house and labeled all of your moving
boxes as Stuff. Youd never find anything!
Instead if you name them like cups ,books etc it will be easy
for you to handle and use them.
So thats all from me on variables in python.I hope You
enjoyed .
Thank You.

Das könnte Ihnen auch gefallen