0% found this document useful (0 votes)
8 views6 pages

Python Programming Basics Explained

The document is a comprehensive tutorial on Python, covering its applications, syntax, and variable management. It explains the importance of indentation, variable declaration, assignment, and the distinction between local and global variables. Additionally, it details Python's number data types, including integers, long integers, floating-point numbers, and complex numbers.

Uploaded by

nagpathuri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Python Programming Basics Explained

The document is a comprehensive tutorial on Python, covering its applications, syntax, and variable management. It explains the importance of indentation, variable declaration, assignment, and the distinction between local and global variables. Additionally, it details Python's number data types, including integers, long integers, floating-point numbers, and complex numbers.

Uploaded by

nagpathuri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Tutorial

What is Python – Python is a popular programming language.

 Web development (server side)


 Software development
 Mathematics
 System scripting

Why Python

 Python works on different platforms


 Python has a simple syntax similar to the English language.
 Python has syntax that allow developers to write programs with fever lines that some other
programming languages
 Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. That means that prototyping can be very quick.
 Python can be treated in a procedural way, an object-oriented way or a functional way.

Python syntax compared to other programming languages

 Python was designed for readability, and has some similarities to the English language with
influence form mathematics.
 Python uses new lines to complete a command, as opposed to other programming
languages which often use semicolons or parentheses.
 Python relies on indentation, using whitespace, to define scope; such as scope of loops,
functions and classes. Other programming languages often use curly-brackets for this
purpose.

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.
 The number of spaces is up to you as a programmer, but it has to be at least one.
 We have to use the same number of spaces in the same block of code, otherwise Python will
give an error.

What is variable in Python?


A variable is a memory address that can change, and when the memory address cannot change then
that variable is known as a constant. Variable is the name of the memory location where data is
stored. Once a variable is stored, the space is allocated in memory. It defines a variable using a
combination of numbers, letters and the underscore character.

Creating and declaring Python variables

Python does not have a specific command just to declare or create a variable; however, there are
some rules that we need to keep in mind while creating Python variables.

 The name of a variable cannot start with a number. It should start with either an alphabet or
the underscore character.
 Variable names are always case sensitive and can contain alphanumeric characters and the
underscore character.
 Reserved words cannot be used as variable names
 Python variables are always assigned using the equal to sign followed by the value of the
variable.
 A variable in Python is created as soon as we assign a value to it.
 Python also does require specifying the data type of the variable.

Assigning Values to Python variable

There is no need for an explicit declaration to reserve memory. The assignment is done using the
equal to = operator.

A = 10

B = “Naga”

Multiple Variable assignment

We can assign a single value to multiple variables as follows

a=b=c=5

Also, we can assign multiple values to multiple variables as follows

a,b,c=2,25, ‘abc’

Note: Python is a type inferred language, i.e, it automatically detects the type of the assigned
variable.

Example

test=1

type(test)

output:

int

Redeclaring a variable in Python

After we have declared a variable, we can again declare it and assign a new value to it. Python
interpreter discards the old value and only consider the new value. The type of the new value can be
different than the type of the old value

Example

a=1

print(a)

a=’Naga’

print(a)
output:

Naga

Constants in Python

A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely use
constants in Python. Constants are usually declared and assigned on a different module / file.

Example

PI=3.14

GRAVITY=9.8

We have to import Constant to the main file

import constant

print([Link])

print([Link])

Python class variables

A class variable in Python is shared by all the object instances of the class. They are declared when
the class is constructed and not in any of the methods of the class. Because these class variables in
Python as owned by the class itself, they are shared by all instances of that class.

class languages:

lang_name = ‘Python’

Python private variables

In Python, ‘Private’ instance variables that can’t be accessed except inside an object, do not
practically exist. But most Python coders use two underscores at the beginning of any variable or
method to make it private.

Scope of variables in Python

All variables in Python may not be accessible at all locations. This will depend on where these
variables were declared. Scope of the variable defines where in the program the variable will be
accessible. The scope can be either local or global.

Local variables in Python

A variable that is declared inside a Python function or a module can only be used in that specific
function or Python module. This kind of variable is known as a local variable. Python interpreter will
not recognize that variable outside that specific function or module and will throw an error if that
variable is not declared outside of that function.

Example
a=100

print (f)

def some_function()

f = ‘Intellipaat’

print(f)

some_function()

print(f)

Output:

100

Intellipaat

100

Global variable in Python

On the other hand, the global variable in Python is a variable that can be used globally anywhere in
the program. It can be used in any function or module, and even outside the functions, without
having to redeclare it

Example

a = 100

print (a)

def som_function():

global a

print (a)

a = ‘Intellipaat’

some_function()

print (a)

Output:

100

100

Intellipaat

Deleting Python variables


Python provides a feature to delete a variable when it is not in use so as to free up space. Using the
command del ‘variable name’, we can delete any specific variable.

a = 10

print (a)

del a

print (a)

Python Numbers

In Python, the number data type is used to store numeric values. Numbers in Python are in
immutable data type. Being an immutable data type means that if we change the value of an already
allocated number data type, then that would result in a newly allocated object.

Categories of Number Data Type

The number data type is divided into the following five data types in Python.

 Integer – Python integers are nothing but whole numbers, whose range dependents on the
hardware on which Python is run. Integers can be of different types such as positive
negative, zero and long.
 Long Integers – L suffix is used for the representation of long integers in Python. Long
integers are used to store large numbers without losing precision.
 Octal and Hexadecimal – In Python, we also have another number data type called octal and
hexadecimal numbers.
o To represent the octal number which has base 8 in Python, add a preceding 0 (zero)
so that the Python interpreter can recognize that we want the value to be in base 8
and not in base 10.
Example

I = 11
#Then in Octal we will write –
I = 011
print (I)

Output:
9
o To represent the hexadecimal number ( base 16 ) in Python, add a preceding 0x so
that the interpreter can recognize that we want the value to be in base 16 and not in
base 10.
Example

I = 0x11
print (I)

Output:
17

 Floating point numbers – Floating point numbers symbolize the real numbers that are
written with a decimal point dividing the integer and fractional parts.
Floating point numbers may also come with scientific notation with E or e, indicating the
power of 10.

Example
5.6e2 that means 5.6 * 102.
I = 2.5
J = 7.5e4

 Complex Numbers – Complex numbers are of the form. ‘a + bj’, where a is real part floating
value and b is the imaginary part floating value, and j represents the square root of -1.\

Example
2.5 + 2j

You might also like