Day 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Understanding print() function

 The print() function prints the specified message to the screen, or other standard output
device.
 The message can be a string, or any other object, the object will be converted into a string
before written to the screen.
 The python print() function is unique and more versatile.

Syntax:
print(Object(s), sep=separator, end=end, file=sys.stdout,flush=false) Parameters of print()

function:
Paramter Description
Object(s) Anything that has to be printed, Converted into String before printed
sep Optional – specifies how to separate objects, if there is more than one then
default is ‘ ‘
end Optional – Specifies what to be printed at the end,default is ‘\n’ (new line)
file Optional - must be an object with write(string) method. If omitted it,
sys.stdout will be used which prints objects on the screen.
flush If True, the stream is forcibly flushed. Default value: False

Example of print() function:

print("Rama")
print("Krishna")
output:
Rama
Krishna

By Default the 'End' is \n

print("Rama",end="")
print("krishna")
output:
RamaKrishna

print("Hello","Rama","Krishna")
Output:
Hello Rama Krishna

By Defalt is sep=" "

print("Rama",end=" ")
print("krishna")
output:
Rama Krishna

print("Hello",end=" ")
print("Rama",end=' ')
print("krishna")
Output:
Hello Rama Krishna

print("Hello",end="\n")
print("Rama",end='\n')
print("krishna")
Output:
Hellow
Rama
Krishna

print("Hello",end="\t")
print("Rama",end='\t')
print("krishna")
Output:
Hello Rama Krishna

print("Helo",end="\t")
print("Rama",end='\t')
print("kris")
Output:
Helo Rama Kris

print(Variable List):-This is used to Display the values of a Variable or a list of Variables


a=1000
print("The value of a is:",a)
Output:
The value of a is: 1000

Concatenating string with variable by default it given 1 space between the sting and
variable

a=1000
print("The value of a is: ",a)
Output:
The value of a is: 1000

it Displays 2 space the between string and variable

a=1000
b=2000
print("The value of a and b is :",a,b)
Output:
The value of a and b is 1000 2000

Concatenating String with Multiple variables

a=1000
b=2000
c=3000
print(a,b,c)
Output:
1000 2000 3000

name='Rama Krishna'
age=45
print("My Name is",name," and Age is",age)
Output:
My Name is Rama Krishna and Age is 45

a=1000
b=2000
c=3000
print(a,b,c,sep=",")
Output:
1000,2000,3000

a=1000
b=2000
c=3000
print(a,b,c,sep="-")
Output:
1000-2000-3000

a=1000
b=2000
c=3000
print(a,b,c,sep="\n")

Output:(Dispalyed in separate lines)


1000
2000
3000

a=5
print("a =", a, sep='00000', end='\n\n\n')
print("a =", a, sep='0', end='')

print("Rama Krishna")
print("\n\n\n\n\n")
print("Coding Careen Expert")
Output:
Rama Krishna

Coding Career Expert

print ("Rama Krishna")


print (5 * "\n")
(or)
print("\n"*5)
print ("Coding Career Expert")

Output:
Rama Krishna

Coding Career Expert


ex2.py
a=000005
Error:

a=5
print("a =", a, sep='00000', end='\n\n\n')
output
a=000005

print("a =", a, sep='0', end='')


Output
a=05

Python Identifiers:
 An identifier is a name given to the components of a program, ie variable,
function, module, class, or object.
 An identifier can start with an alphabet (A-Z or a-z) or an underscore(_) followed by zero
or more letters, underscores and digits (0-9).
 Special characters are not allowed in identifier names.
 Python is a case sensitive language,so identifiers are also case sensitive ie salary, Salary
and SALARY are different.
 An identifier starting with two leading underscores indicates a strong private
identifier.
 Keywords or Reserved words shouldnot be used as identifiers.
 List of valid identifiers
o a123=100
o net_sal=100
o _netsal=100
o netsal_=100

 List of invalid identifiers


o 123salary=100
o @total=100
Identifiers are case sensitive:
A=10
a=20
print(A)
10
print(a)
20
Keywords:
Keywords are the Reserved words of the language. Keywords have some predefined
functionality, they cannot be used as identifiers, or constants.

Python contains 33 keywords, out this 33 only True, False and None are capitalized.

Help In Python:
Use the command help() to get the list of all available keywords of python.

>>> help("keywords")
Use help("specific keyword") to get help of specific keyword.

>>> help("if")

Or use the following command to display keywords list.

>>> import keyword


>>> keyword.kwlist

Multiline statements in python:


Slash (\) in python is used as a line continuation character.
Ex:
>>> str="this is "+\
"to test "+\
"the "+\
"course"
>>> str
Output:
'this is to test the course'

Statements contained with the [],{}, or () brackets do not need to use the line
continuation character.
days=['Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday']
print(days)

Output:
Monday,Tuesday, Wednesday,Thursday,Friday,Saturday
Quotations in python:
Python accepts single('), double(") and triple (' ' ' or " " ") quotes to denote string literals,as
long as the same type of quote starts and ends the string.

Generally triple quotes are used to write the string across multiple lines.

For example:
gender='Male'
name="Raj Kumar Jain"
str="""This is a multi line statement.
It is denoted by triple quotes"""

Comments in Python:
Comments are used to describe the purpose of the code. Commented code is ignored by
Compilers and interpreters.
Hash sign (#) is used to comment in python.

Ex:
# This is a comment.

Some of the basic functions in python:


print() : This function displays output on the console.
type(): This function checks datatype of a variable.

Variable:
 Variable is an entity which holds data.
 Variable is an container to stores values.
 Variables are nothing but names of memory locations where values are stored.

In python variables datatype is assigned after the initialization, based upon type of the
data.

Ex:
>>> x=100
>>> print(x)
100
id():
The id() function returns identity (unique integer) of an Variable/object.
>>> print("Hello Python")
Hello Python
>>> x=10
>>> type(x)
<class 'int'>
>>> id(x)
496851360
>>>
Note:
Identity of the object is unique and remains constant during its lifetime.

Constant values in Python:


In Python we don’t have concept of constant keyword.
>>> PI=3.14
>>> PI
3.14
>>> PI=3.14
>>> PI
3.14

Note: Just By Declaring Variable in Capital PI we can just show our intention that this is
constant variables, but still it can be modified.

In Python if two variables has the same value, then they will have same id(python is
memory efficient):
>>> a=10
>>> b=a
>>> id(a)
1654302784
>>> id(b)
1654302784
>>> a
10
>>> b
10
>>> id(10)
1654302784
>>>
Additing of two numbers:
>>> a=10
>>> b=20
>>> c=a+b
>>> print(c)
30

Varaible can be modified:


>>> x=10
>>> x=x+100
>>> x
110

How to use output of previous operation:


>>> _+90
200
Note: _ gets the output of the previous operation.

Ex 1:
>>> x=10
>>> print(x)
10
>>> type(x)
<class 'int'>
>>> id(x)
501373344

Ex 2:
>>> y=12.50
>>> print(y)
12.5
>>> type(y)
<class 'float'>
>>> id(y)
33493168

Ex:3
>>> s="Raj"
>>> print(s)
Raj
>>> type(s)
<class 'str'>
>>> id(s)
34989728

We can assign single value to single variable, multiple values to multiple variables or
Single valule to multiple variables.
Assigning Single value to Single variable:
>>> a=10
>>> a
10

Assigning multiple values to multiple variables:


>>> a,b,c=10,1.25,"prem"
>>> a
10
>>> b
1.25
>>> c
'prem'

Assigning single value to multiple variables:


>>> x=y=z=100
>>> x
100
>>> y
100
>>> z
100
>>> id(x)
501374784
>>> id(y)
501374784
>>> id(z)
501374784

Escapesequence characters:
\n
\t
\'
\"
Etc..

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy