0% found this document useful (0 votes)
13 views

Python First I,II,III IV Notes

The document provides an introduction to Python programming, covering its history, features, and applications. It distinguishes between scripting and programming languages, explains the differences between compilers and interpreters, and lists various Python versions. Additionally, it highlights Python's advantages, such as ease of use, readability, and its extensive standard library.

Uploaded by

thrishareddy2151
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python First I,II,III IV Notes

The document provides an introduction to Python programming, covering its history, features, and applications. It distinguishes between scripting and programming languages, explains the differences between compilers and interpreters, and lists various Python versions. Additionally, it highlights Python's advantages, such as ease of use, readability, and its extensive standard library.

Uploaded by

thrishareddy2151
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 74

Python Programming

B.Tech. I Year II Sem – CSE – A & C

Introduction to Python

V.L.Kartheek
B.Tech(CSE),M.Tech(CST),
(Ph.D(CSIS))

Dept of CSE
TEXT BOOK:
1. Core Python Programming, Wesley J. Chun, Second Edition, Pearson
Topics to discuss:-
 Program vs Script,
 What is Python,
 History of Python
 Scope of Python,
 Advantages of Python,
 Fascinating Python Applications in the Real time,
 Code Editor vs IDE,
 Installation of Python,
 Python Code Execution.
Date : 18-04-2023
Day: Tuesday CLASS-I

Scripting Languages vs Programming Languages:


Scripting Languages:-
Scripting is loosely or weakly typed or light weight
programming.
Scripting languages are more popular due to the
emergence of web based applications.
All scripting languages are programming languages.
The scripting language is basically a language where
instructions are written for a run time environment.
They do not require the compilation step and are
rather interpreted.
These are only Interpreter based programming
languages.
A scripting language is a programming language
designed for easy integration and communication
with other programming languages.
Implicit declaration of data types.
Example:- JavaScript, Python, PHP and Ruby are all
examples of scripting languages.
Programming Languages:-
A programming language is a computer
language programmers use to develop software
programs, scripts, or other sets of instructions for
computers to execute.
A programming Language may Compiler based or
Interpreter based or both Compiler and Interpreter based.
Explicit declaration of data types.
Difficult to integrate with other technologies.
.
Examples for Pure Compiler based Programming
Languages:
C, C++, Erlang, Haskell, Rust, and Go
Examples for Both Compiled and Interpreted
Programming Languages:
An. Java can be considered both a compiled and an
interpreted language because its source code is first
compiled into a binary byte-code. This byte-code runs
on the Java Virtual Machine (JVM), which is usually a
software-based interpreter
Types of Scripting Languages:-

Scripting languages are classified into 2 types.


1.Client Side Scripting languages.
2.Server Side Scripting languages.

1. Client Side Scripting languages:-


The script which is running within the browser is called as
client side scripting languages.
Ex:- 1. Live Script 5. HTML 9. AJAX
2. Java Script 6. CSS 10. VB Script
3. Type Script 7. JQuery
4. Action Script 8. Angular JS
1. Server Side Scripting languages:-
The script which is running within the webserver is called as
client side scripting languages.
Ex:- 1. Python ==> Simple HTTP Server
2. ASP ==> IIS(Internet Information Services)
3. JSP ==> Tomcat/Sun Java System Web Server
4. PHP ==> Apache
5. NodeJS ==> Any Server Side Java Script
Date : 19-04-2023
Day: Wednesday CLASS-II
1. PYTHON BASICS
What is PYTHON?
Python is a high-level, interpreted, interactive and object-oriented scripting
language.
Python is Interpreted:
 Python is processed at runtime by the interpreter. We do not need to
compile our program before executing it. This is similar to PERL and PHP.
 Python is Interactive:
You can actually sit at a Python prompt and interact with the interpreter
directly to write your programs.
Python is Object-Oriented:
Python supports Object-Oriented style or technique of programming that
 Python is a dynamic programming language which supports several different
programming paradigms:
Procedural programming(C)
Object oriented programming(C++,java)
Functional programming(LISP, PYTHON, Erlang…..)
Scripting languages(shell script, PERL, Ruby, Powershell)
Modular programming(Modula-3)

 Python= powerful programming language + powerful scripting language

Python made programming more easier than c/c++, java.


By using List, tupuls, Dictionary concepts we can simplify data structure programs.
Python is more efficient for string pattern matching than PERL.
Python made more easier to access system calls than Tcl/Tk language
Scripting language based applications are run by interpreter.
Scripting language based applications are not required explicit compilation
Ex: shellscript, python , perl, Ruby, PowerShell
These languages are run by interpreter.

Programming languages based applications are run by compiler


Programming languages based applications are required explicit compilation
Programming languages based applications can’t run directly without
compilation.
Ex: C,C++,JAVA .NET
These languages are run by compiler
Python is designed to be highly readable and its syntax allows programmers to
express concepts in fewer lines of code.

 It has fewer syntactical constructions than other languages.

Python is a great language for the beginner level programmers and supports the
development of a wide range of applications.

Python is a programming language that allows you work quickly and integrate
systems more efficiently.
History of Python:
Python was developed by Guido van Rossum, a Dutch computer programmer in

December 1989 at the National Research Institute for Mathematics and Computer

Science in the Netherlands.

This was a successor to the ABC programming language which was capable of

exception handling and interfacing with the Amoeba operating system.

Python is derived from many other languages, including ABC, Modula-3, C, C+

+,Algol-68, SmallTalk, and Unix shell and other scripting languages.


 Web Development
 Game Development
 Machine Learning and Artificial Intelligence
 Data Science and Data Visualization
 Desktop GUI
 Web Scraping Applications
 Business Applications
 Audio and Video Applications
 CAD Applications
 Embedded Applications
Topics Code Editors IDE
Definition Text editors with advanced The IDE is a collection
built-in features and specific of software
features designed to simplify development tools
and speed up the code editing designed to simplify
process are called code the coding process. It
editors. simplifies the whole
software program
development
procedure by
combining various pc
program additives into
one GUI.
Languages It is only supporting specific It supports various
languages. kinds of languages.
Auto Complete It supports auto-complete. It is also supports auto
complete.
Examples Atom, Sublime Text, Brackets, Eclipse, Intellij IDEA,
Visual Studio Code etc. Visual Studio, NetBeans
etc.
 We generally write a computer program using a high-level
language. A high-level language is one that is understandable by us,
humans. This is called source code.
 However, a computer does not understand high-level language. It
only understands the program written in 0's and 1's in binary, called
the machine code.
 To convert source code into machine code, we use either
a compiler or an interpreter.
 Both compilers and interpreters are used to convert a program
written in a high-level language into machine code understood
by computers. However, there are differences between how an
interpreter and a compiler works.
 Compiler:-
A compiler converts complete source code into
machine code at once. And finally, your program
get executes.
 The entire compilation steps of source code are operated
into two phases: Analysis Phase and Synthesis Phase.
 Analysis Phase: This compiler phase is also known as the
front end phase in which a source code is divided into
fundamental parts to check grammar, syntax, and semantic
of code; after that, the intermediate code is generated. The
analysis phase of the compilation process includes a lexical
analyzer, semantic analyzer, and syntax analyzer.
 Synthesis Phase: The Synthesis phase is also known as
the back end phase in which the intermediate code (which
was generated in Analysis Phase) is optimized and
generated into target machine code. The synthesis phase
of the compilation process includes code optimizer and
code generator tasks.
 An interpreter is also a software program that
translates a source code into a machine language.
However, an interpreter converts high-level
programming language into machine language line-
by-line while interpreting and running the program.
 Here interesting fact is- Python is named after the BBC TV comedy show

Monty Python’s Flying Circus.

 It is not named after the Python snake.


Python Versions:
Release dates for the major and minor versions:
Python 1.0 - January 1994
Python 1.5 - December 31, 1997
Python 1.6 - September 5, 2000
Python 2.0 - October 16, 2000
Python 2.1 - April 17, 2001
Python 2.2 - December 21, 2001
Python 2.3 - July 29, 2003
Python 2.4 - November 30, 2004
Python 2.5 - September 19, 2006
Python 2.6 - October 1, 2008
Python 2.7 - July 3, 2010

Python 2.7 is latest version of python2


 Python 3.0 - December 3, 2008
Python 3.1 - June 27, 2009
Python 3.2 - February 20, 2011
Python 3.3 - September 29, 2012
Python 3.4 - March 16, 2014
Python 3.5 - September 13, 2015
Python 3.7.0 - June 27, 2018
Python 3.7.1 - October 13, 2018
Python 3.7.2 - December 24, 2015
Python 3.7.3 - March 25, 2019
Python 3.7.4 - July 8, 2019
Python 3.7.4 is latest version of python 3
Python 3 is not backward compatible with Python 2.
Features of Python Programming:
1. Easy:
a. Easy to code:
Python is very easy to code. Compared to other popular languages like Java
and C++ . Anyone can learn python syntax in just a few hours.
b. Easy to read:
Being a high-level language, Python code is quite like English. Looking at
it, you can tell what the code is supposed to do.

2. Expressive
First, let’s learn about expressiveness. Suppose we have two languages A and
B, and all programs that can be made in A can be made in B using local
transformations. However, there are some programs that can be made in B,
but not in A, using local transformations. Then, B is said to be more
expressive than A.
3. Free and Open-Source
Firstly, Python is freely available. You can download it from the following
link
https://www.python.org/downloads/.

Secondly, it is open-source. This means that its source code is available to


the public. You can download it, change it, use it, and distribute it. This is
called FLOSS(Free/Libre and Open Source Software).
4. High- Level
It is a high-level language. This means that as programmers, we don’t need
to remember the system architecture. Nor do we need to manage the
memory. This makes it more programmer-friendly.

5. Portable
We can take one code and run it on any machine, there is no need to write
different code for different machines. This makes Python a portable language.
However, you must avoid any system-dependent features in this case.
6. Interpreted:
If you’re any familiar with languages like C++ or Java, you must first compile it,
and then run it. But in Python, there is no need to compile it. Internally, its source
code is converted into an immediate form called bytecode.

So, all you need to do is to run your Python code without worrying about linking
to libraries, and a few other things.

By interpreted, we mean the source code is executed line by line, and not all at
once. Because of this, it is easier to debug your code.

Also, interpreting makes it just slightly slower than Java, but that does not matter
compared to the benefits it has to offer.
7. Object-Oriented
A programming language that can model the real world is said to be object-
oriented. It focuses on objects, and combines data and functions.

Python supports both procedure-oriented and object-oriented programming


which is one of the key python features.

It also supports multiple inheritance, unlike Java

8. Extensible
If needed, you can write some of your Python code in other languages like C++.
This makes Python an extensible language, meaning that it can be extended to other
languages.
9. Embeddable
 We can put our Python code in a source code in a different language like C++.
 We can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
CPython is a python integrated with C/C++ language.
Similarly JPython is a purely integrated language.

10. Large Standard Library


Python downloads with a large library that you can use so you don’t have to write your
own code for every single thing.

There are libraries for regular expressions, documentation-generation, unit-testing,


web browsers, threading, databases, CGI, email, image manipulation, and a lot of
other functionality.
11.GUI Programming
You can use Tk to create basic GUIs
.

12. Dynamically Typed


Python is dynamically-typed. This means that the type for a value is decided at
runtime, not in advance. This is why we don’t need to specify the type of data while
declaring it.
Apart from the above-mentioned features, Python has a big list of good features.
A few are listed below-
 It supports functional and structured programming methods as well as
OOP.
It can be used as a scripting language or can be compiled to byte-code for
building large applications.
It provides very high-level dynamic data types and supports dynamic type
checking.
It supports automatic garbage collection.
Supports exception handling
Automatic memory management
What can Python do?
Python can be used on a server to create web applications.

Python can be used alongside software to create workflows.

Python can connect to database systems. It can also read and modify files.

Python can be used to handle big data and perform complex mathematics.

Python can be used for rapid prototyping, or for production-ready software


development.
Why Python?
Python is also a cross platform language which means that the code written
for one operating system like Windows ,will work equally well with Linux or
MacOS without any changes to the python code.

Python has a simple syntax similar to the English language.

Python has syntax that allows developers to write programs with fewer lines
than some other programming languages.

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.

Python can be treated in a procedural way, an object-orientated way or a


Organizations using Python :
1) Google(Components of Google spider and Search Engine)
2) Yahoo(Maps)
3) YouTube
4) Mozilla
5) Dropbox
6) Microsoft
7) Cisco
8) Spotify
9) Quora
Advantages of Python:
1. Freeware: Python is open source language. It doesn't require any activation
key or subscription to work on it. It is free to use software and all the tool
available on python are absolutely free.
2. Less Coding: The syntax written in python is very simple and use common
English language which makes it more user-friendly.
3. Efficient: Python relies on indentation, using white space, to define scope;
such as the scope of loops, functions, and classes. Other Programming Languages
often use curly-brackets for this purpose.
4. Portability: Python can run any operating system. Also, we use the Python
Code written on one system onto another system without making any changes to
the code.
Disadvantages of Python:
1. Python is slower than C and C++. So the time taken to execute a task by
python is more than compared to C and C++ but python is a high-level, unlike
C or C++.

2. As we know python is flexible of data-types so memory consumption is


also high. So due to this python is not good choice for memory intensive task.

3. Code protecting is difficult.


4. Python is not good for High graphic 3D game take lot of CPU.

5. Python is not a very good language for mobile development. It is weak


language for mobile computation that why very few mobile application are built
on python
Why Code protecting is difficult in Python?
Python source code requires more effort to secure than C++, for example,
because Python is an interpreted language. This means that the source code
which developers write is stored in human readable form right up to the point
of execution.
C++ on the other hand, is a compiled language, which means that it is
translated into a form which is not readable to humans – a form called
machine language – and stored in this form up till the point of execution.
Both ways of storing code have advantages. But an important disadvantage
of interpreted languages like Python is that Python source code is more easily
stolen.
The best solution to this vulnerability is to encrypt Python source code.
Encrypting Python source code is a method of “Python obfuscation,” which
has the purpose of storing the original source code in a form that is unreadable
to humans. There are actually programs available to reverse engineer
or uncompile C++ code back to human readable form.
Date : 21-04-2023
Day: Friday CLASS-III

Statements and Syntax:


1. Python Statement
By default, the Python interpreter treats a piece of text terminated by
hard carriage return (new line character) as one statement.
It means each line in a Python script is a statement. (Just as in
C/C++/C#, a semicolon ; denotes the end of a statement).
Example:
msg="Hello World"
code=123
name= 20.5
2.Multi-line statements:
Continuation of Statement ( \ ):
If we want to show the text spread over more than one lines to be a single
statement by using the backslash (\) as a continuation character
msg= "Hello world! \
Welcome to Python Programming \
I am simple than other languages”

Multiple Statements in Single Line ( ; )


If you want to write multiple statements in a single line. Use semicolon(;)
Ex:
msg="Hello World"; code=123; name="Steve"
3.Comments in Python
In a Python script, the symbol # indicates the start of a comment line. It is effective till
the end of the line in the editor.
It can be used also in the middle of a line. The text before it is a valid Python expression,
while the text following is treated as a comment.
Example:
# this is a comment
print ("Hello World")
print ("Welcome to Python Tutorial") #this is also a comment but after a statement

In Python, there is no provision to write multi-line comments, or a block comment. (As
in C#/C/C++, where multiple lines inside /* .. */ are treated as a multi-line comment).
 Each line should have the # symbol at the start to be marked as a comment. Many
Python IDEs have shortcuts to mark a block of statements as a comment. In IDLE, select the
block and press Alt + 3.
Date : 25-04-2023
Day: Tuesday CLASS-IV
4.Indentation:
One of the most distinctive features of python is use of indentation to mark blocks of code.
Groups of individual statements making up a single code block are called "suites“(:)
Generally, we use curly brackets { } in C,C++,java languages but where as in python curly
braces are not allowed to indicate block of code for class ,function definition or flow of control.
So, in python block of code are denoted by indentation.
Instead of curly braces { }, python uses whitespaces(spaces and tabs) to define program
blocks.
The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all
statements within the block must be the indented same amount.
When a block is to be started, type the exclamation symbol (:) and press Enter.
In order to signal the end of a block, the whitespace is de-dented by pressing the backspace
key
In the following program, the block statements have no indentation.

This is a program with single space indentation.


This is a program with single tab indentation.

Instructions:
Use 4 spaces per indentation and no tabs.
Do not mix tabs and spaces. Tabs create confusion and it is recommended
to use only spaces.
5. Python identifiers:
A identifier is a name used to identify a variable, function, class, module or other object.
An identifier start with a letter A t o Z (or) a to z an underscore(_) followed by zero or
more letters, underscores and digits(0-9).
Python doesn’t allows punctuation characters such as @,$,% with identifiers.

1.An identifier should not start with digit but it can contains digit.
Ex:
>>> 12ab=20 syntax error: invalid syntax
>>> ab12=20 20
2. An identifier can contain underscores (single, double, and also triple)
3. An identifier must not contain special characters except underscores.
4. Identifiers are case sensitive, so python is a case sensitive language.

>>> a=100
>>> A=200
>>> print(a)
100
>>> print(A)
200
>>> var='python'
>>> print(Var)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Var' is not defined

5. Keywords or reserved words can’t be used as identifiers

>>> if=100 >>> return=10


File "<stdin>", line 1 File "<stdin>", line 1
if=100 return=10
^ ^
SyntaxError: invalid syntax SyntaxError: invalid syntax
>>>
6.Keywords:
Computer programming language comprises of a set of predefined words which are called
keywords.
Similarly, Python 3.x interpreter has 35 keywords defined in it.
They cannot be used for any other purpose like constants or variable or identifier names.
>>> help("keywords“)
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
Note: only False, None,
as elif in try
True are capitalized
assert else is while
async except lambda with
await finally nonlocal yield
break for not
7.Python variable:
a)Variable Assignment:
A variable is a container for a value. It can be assigned a name, you can use it
to refer to it later in the program.
When you create a variable you reserve some space in memory and you can
store integers, decimals and characters.
 Based on the value assigned, the interpreter decides its data type. You can
always store a different type in a variable.
So, in Python, variables need not be declared or defined in advance.
Assignment is done with a single equals sign (=)
1. Assigning single value to single variable:
 The operand to the left of the = operator is the variable and the right of the =operator is
the value stored in the variable
For example, >>> n = 300 >>m=20.5 >>>p=‘python’
>>>n >>>m >>>p
3 20.5 python

2. Assigning multiple values to multiple variables: ("Multuple"


Assignment)
>>> a,b,c=10,20.5,'python'
>>> a,b,c
(10, 20.5, 'python‘)
Swapping of 2 numbers using "multuple" assignment in python:
If you have ever needed to swap values in other languages like C, you will be reminded that
a temporary variable, i.e., tmp, is required to hold one value while the other is being
exchanged:
/* swapping variables in C */
tmp = x;
x = y;
y = tmp;
One interesting side effect of Python's "multuple" assignment is that we no longer need a
temporary variable to swap the values of two variables.
# swapping variables in Python
>>> x, y = 1, 2
>>> x
1
>>> y
2
>>> x, y = y, x
>>> x Note: Python performs evaluation before making assignments.
2
>>> y
1
3. Assigning same values to multiple variable:

>>> x=y=z=50
>>> x
50
>>> y
50
>>> z
50
>>>
4. To check the data type of specific variable we can use type( ) function.

>>> a,b,c=10, 20.5, 'python'


>>> a,b,c
(10, 20.5, 'python')
>>> type(a)
<class 'int'>
>>> type(b)
<class 'float'>
>>> type(c)
<class 'str‘>
5. In python, assignments are not treated as expressions:

In C, you know that assignments are treated as expressions. This is not the case in
Python, where assignments do not have inherent values.
Statements such as the following are invalid in Python:
>>> x = 1
>>> y = (x = x + 1) # assignments not expressions!
File "<stdin>", line 1
y = (x = x + 1)
^
SyntaxError: invalid syntax
b) . Augmented Assignment:
Augmented assignment refers to the use of operators, which imply both an arithmetic
operation as well as an assignment.
Ex:

>>> a=2
>>> a+=1
>>> a
3
>>> a*=2
>>> a
6
>>> a-=3
>>> a
3
>>> a/=3
>>> a
1.0
>>> a%=3
>>> a
1.0
>>>
9.OUTPUT Statement:
In some languages, such as C, displaying to the screen is accomplished with a function,
e.g., printf(),
while with Python and most interpreted and scripting languages, print is a statement.
 Many shell script languages use an echo command for program output.

Example:
>>> myString = 'Hello World!'
>>> print (myString)
Hello World!
>>> myString
'Hello World!‘

Note: The underscore (_) also has special meaning in the interactive interpreter: the last
evaluated expression.
>>> _
Hello World!
In python2, print statement, paired with the string format operator ( % ), supports
string substitution, much like the printf() function in C:
Ex:
>>> print "%s is number %d!" % ("Python", 1)
Python is number 1!

 %s means to substitute a string while %d indicates an integer should be substituted.

%f for floating point numbers.

>>> p,q=4,3
>>> print "addition of %d and %d is %d" %(p,q,p+q)
addition of 4 and 3 is 7
In python3, The print() function to display output prints the specified message to the

screen, or other standard output device.


1.print( ) function without argument:
Just it prints new line character
Ex: >>> print()
>>>
2. prints the specified message to the screen:
>>> print("hello world!")
hello world!
>>> print("hello\n world!") #you can use escape character also
hello
world!
>>> print(10*"hello") # you can use reptation operator also
hellohellohellohellohellohellohellohellohellohello
>>> print("hello" + "world!") #you can use +operator for concatenation
helloworld!
3. print() with variable number of arguments:
>>> a, b, c=1,2,3
>>> print("values of a, b, c are:",a,b,c)
values of a, b, c are: 1 2 3
 By default, output values are separated by space.
If we want to specify separated by using “sep” attribute.
Ex:
>>> a,b,c=10,20,30
>>> print("values of a,b,c are with seperated:",a,b,c,sep=',')
values of a,b,c are with seperated:,10,20,30
>>> print("values of a,b,c are with seperated:",a,b,c,sep=':')
values of a,b,c are with seperated:10:20:30
4. print() with end attribute:
 if we want output in the same line with space.
print("hello")
print("python")
print("programming“)

o/p:
hello
python
Programming

print("hello",end='')
print("python",end='')
print("programming")

o/p:
hello python programming

Note: The default value for end attribute is \n ,which is nothing but new line character
5.print(object) statement:
We can pass any object (list,tuple,set..) as an argument to the print() statement.
Ex:
>>> list=[1,2,3,4]
>>> tuple=(10,20,30,40)
>>> print(list)
[1, 2, 3, 4]
>>> print(tuple)
(10, 20, 30, 40)
>>>
6.print(string, varaible list):
We can use print() statement with string and any number of arguments
Ex:
>>>s="python"
>>> s1="Guido van Rossum"
>>> s2=1989
>>> print("Hello",s,"was developed by",s1,"in the year",s2)
Output:
Hello python was developed by Guido van Rossum in the year 1989
EX2:
>>> a=5
>>>b=6
>>>c=7
>>>print("a=",a,"b=",b,"c=",c);
OUTPUT:- a= 5 b= 6 c= 7
7. print(formatted string)
%d--------------------int
%f--------------------float
%s--------------------string

Ex:
>>> s="IV CSE A"
>>> list=[1,2,3,4]
>>> print("hello %s.....the list of items are %s“ %(s,list))

Output:
hello IV CSE A.....the list of items are [1, 2, 3, 4]
8. print() with replacement operator:
>>> s="python"
>>> s1="Guido van Rossum"
>>> s2=1989
>>> print("Hello {0} was developed by {1} in the year {2}".format(s,s1,s2))

Output:
Hello python was developed by Guido van Rossum in the year 1989
10.Reading data from the keyboard:
Here Python2 provides us with two inbuilt functions to read the input from the
keyboard.
1. raw_input ( prompt )
2. input ( prompt )
1. raw_input ( ):
This function takes exactly what is typed from the keyboard, convert it to string and
then return it to the variable in which we want to store.
Ex:
>>> key=raw_input("Enter your name")
Enter your name RAJEEV
>>> type(key)
<type 'str'>

>>> a=raw_input("Enter your age:")


Enter your age:26
>>> type(a)
<type 'str'>
2. input ( ) :
This function first takes the input from the user and then evaluates the expression, which
means Python automatically identifies whether user entered a string or a number or list.
 If the input provided is not correct then either syntax error or exception is raised by
python
This function treats the received data as string if it is included in quotes or otherwise
the data is treated as numbers.
Ex:
>>> x=input('enter any value:')
enter any value:'10'
>>> type(x)
<type 'str'>

>>> x=input('enter any value:')


enter any value:10
>>> type(x)
<type 'int'>

Here we are not required typecasting


In python 3, we have only input() function. raw_input method is not available.
python 3 input() function behavior exactly same as raw_input() method of
python2.
Every input() function of python2 is renamed as input() in python3.
Whatever you enter as input, input function convert it into a string.
>>> x=input("Enter any value")
Enter any value'10'
>>> x
"'10'"
>>> type(x)
<class 'str‘>
 If you enter an integer value still input() function convert it into a string.
>>> x=input("Enter any value")
Enter any value10
>>> x
'10'
>>> type(x)
<class 'str'>

You might also like