Python Introduction
Python Introduction
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.
development.
Why Python?
•Python was designed for readability, and has some similarities to the English
•Python relies on indentation, using whitespace, to define scope; such as the scope
of loops, functions and classes. Other programming languages often use curly-
2) Expressive Language
Python language is more expressive means that it is more understandable and readable.
3) Interpreted Language
Python is an interpreted language i.e. interpreter executes the code line by line at a time.
This makes debugging easy and thus suitable for beginners.
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, Unix and
Macintosh etc. So, we can say that Python is a portable language.
5) Free and Open Source
offical web address.The
Python language is freely available at
6) Object-Oriented Language
Python supports object oriented language and concepts of classes and objects come into
existence.
7) Extensible
It implies that other languages such as C/C++ can be used to compile the code
and thus it can be used further in our python code.
8) Large Standard Library
Python has a large and broad library and provides rich set of module
and functions for rapid application development.
Syntax:
print (value,……….,value N,sep=‘ ‘,end=‘ ‘)
Example: Output Description
print(10,20,30) 10 20 30 Space is the default separator
print(10,20,30,sep=“*”) 10*20*30 * As separator
print(10,20,30,sep=“-*-”) 10-*-20-*-30 -*- as separator
print(10,20,30,sep=“,”) 10,20,30 , as separator
print(10,20,30,sep=“\n”) 10 \n(new line character) as separator
20
30
What is the use of end=‘ ‘ in print() function?
Example:
print( 10 , 20 , 30 , sep=“,” , end=“!!!!\n” )
Output:
10 , 20 , 30 !!!!
print "hello" * 5 hellohellohellohellohello
print(2*"world") worldworld
print(10**3) 1000
print(10*3) 30
Tokens Of pythons
It is the smallest element of a python program that is
meaningful to the compiler. The programmer can write a
program by using tokens.
1. String literals:
(integers)
Numbers( can be both
positive and negative) with
no fractional part.eg: 100
(floating point)
Python provides various standard data types that define the storage method on
each of them. The data types defined in Python are given below.
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
Numbers
Number stores numeric values. Python creates Number objects
when a number is assigned to a variable.
For example;
a = 3 , b = 5 #a and b are number objects
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides
what can be stored in the reserved memory.
Example:
x=10
y=20
Example: Multiple Assignments
x=y=z=10
Or
x,y,z=10,20.5,”ABC”
Python Data Types
Variables can store data of different types, and different types can do different
things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Getting the Data Type
You can get the data type of any object by using the type() function:
x=5
print(type(x))
Type Conversion
You can convert from one type to another with the int(), float(), and complex()
methods
Python Casting
There may be times when you want to specify a type on to a variable. This can be
done with casting. Python is an object-orientated language, and as such it uses
classes to define data types, including its primitive types.
int() - constructs an integer number from an integer literal, a float literal (by
rounding down to the previous whole number), or a string literal (providing the
string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string
literal (providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings,
integer literals and float literals
How to accept the value from user in python
Python has an input function which lets you ask a user for some text input.
You call this function to tell the program to stop and wait for the user to key
in the data. By default input function provide value in string.
input() Function
If the input function is called, the program flow will be stopped until
the user has given an input and has ended the input with the return
key. The input of the user will be interpreted. If the user e.g. puts in an
integer value, the input function returns this integer value.