Python 1
Python 1
fune tion is
used to disploy teut
he prin
Coade min(tezt)
Output teyt
Pinting Quotes
Code
print(The secret to Sucess is,
'keep Codin
Output:
<Synta z Exvor : bad inpu t on
ine 1>
Using dou ble quotes oi thin a sting defined by double
quotes will give output as Syntax Fxor when you Iun
the code
Solutio 1 -
Mixing Quot
Quotes.:
bu (on
diffevent types of guotes JP your tring
contain s double 2uotes (") wrap the entire string in cingle
guotes (') and vice ver sa. is woy, you'I get the epected
output.
Solu tion 2:
Code :
Voviable Syntox
In Code
name =
Jomes Smith"
lav iable
name
Value
Assignment
PpeNator
Need of Vorables
Code:
1.fruit - apple ')
2print(I ike'ttrujt)
3' Print ("An"+ truit +
c'a day keeps the
doctor away")
Output:
I ike apple
An apple day keeps the docbr aoy
Fleeibi liy: Once
Code you modify a variable
1. s tatu s= "online "
2. print ou are
3. Curenty+ stotu)
4.status = ofP ine
5-pvìnt(Vo are cuTrentd y"+ Status)
t print (Yeur latest status
is 4 sta tu s)
Dutput:
Vo
ave curren Hy on ine
You are curently DPfine
latest statusis fetne
’Avoid ketyping Data
Voviable Noming
Cha obsing cleas vosible name s enhance s code veada biliky
To preven t confusion always choo se descipti ve
vas iable nameS
E: u u ses name Joge |ID - .
So
u-'Tames Smitn" )
user.nome James Smith "
Inital Vaviable
aviable names sh ould sta t oith a lette (either lpuer
Case or uppey ase) 6x an undex SCore.
used later, but
Numbers Can be
not as Aret
tke below character. So,
Code:
12 nammes ="Mary Brotus" Output'
2'print (2name s) Syntax
code
4tuwonames = Mary
Broun"
Qu tput
Mary Brouon
’ No Speci al
chavacBer s
Variab le name9 can have
and undles ecoves (). letter s (a-z,A-2), numberS
No special (o-)
ave pemitted in variable namescharactevs o spo ceS
3i ubers te
L print (user @simaybvouonblogges corn Oxtput
te) $ynta Evoy
’ Sa fe Voviable Nlaming:
Words ike cctela ss special eyoovd in Python
Just li keprint and Shouldn' be used as Vaigble
names Avoid uging
Coce Output!
Syntay Fvor:
2 print (cla ss)
So use
Code Output
1 CasS-name =Pexson
2 print (c pevson
tass-neme
’ Avoiding Common Error s
Code: Output'
1 namme : "Aice "
2 psint(Name) Syntar Enoy:
Input functon
’inpetl) function allous you program to ask the user
for iformation and waits fo Yespon se. It's a
Jour Code and the usey to communicate.
E9.
Code
name
=input ("what is your namey'")
Vaiable
1
Gtoves the mess age asking
foy
input respnse) inpu t
Output
whot I6 youY name q
5 ome on e
pressess Entev, then types à nome
input will be 5 toed
the var iable name
’ Rint
6 tore USer inpu k:
Wse
provides input you hove choiceS- You Can
When
ditec tly display tne inpu t ueing the print() kunebon
Save it in variable for f ture
you can
y'ce the
enterS the name John, tne prin t
fune tion fom the previous Code i5 executed displaying,
John' on net line
Eg: code:
2- name = input ("what is your nameg")
2- print(name)
Output:
what is your nameo John
John
Tndentobon ExYor
In python the way you start£ ine oP ode
Shiftng it ighnt sith spaces s known as inden matters,
ta tion,'
T Code fs misaligned, Piython gets
confused, leading to on
Indenta tion Extor"
Eg: Co de
1 name
inputWhat is your nameg )
20ge - input hat is yous ages")
I Space oCeAYS hese
Symbol
fol lowed by the commenl
"Comments don' affec t your prog ram Anything aftet
the t symbol is just a no te, and py thon ig n ors
i
when Tunning the Co de
Eg: Code : Output:
1: #print("Th) s i's o th a commen t'") This is without a
2pyint (c° This is without a corment) Co mmen L
Code'
This is a Product Name Creotor