Day 2
Day 2
Day 2
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
print("Rama")
print("Krishna")
output:
Rama
Krishna
print("Rama",end="")
print("krishna")
output:
RamaKrishna
print("Hello","Rama","Krishna")
Output:
Hello Rama Krishna
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
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
a=1000
b=2000
print("The value of a and b is :",a,b)
Output:
The value of a and b is 1000 2000
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")
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
Output:
Rama Krishna
a=5
print("a =", a, sep='00000', end='\n\n\n')
output
a=000005
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
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")
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.
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.
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
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
Escapesequence characters:
\n
\t
\'
\"
Etc..