0% found this document useful (0 votes)
3 views9 pages

Python 1

The document provides an overview of Python programming concepts, including syntax, functions, variables, and user input. It highlights common errors, the importance of comments, and how to properly format code. Additionally, it explains how to create a product name generator using user inputs for brand and model names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Python 1

The document provides an overview of Python programming concepts, including syntax, functions, variables, and user input. It highlights common errors, the importance of comments, and how to properly format code. Additionally, it explains how to create a product name generator using user inputs for brand and model names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Riot in Python

fune tion is
used to disploy teut
he prin
Coade min(tezt)

Output teyt

tsing: in lower cose


’ Type "print
’ follss ed by porentheses (O
specify the text you wont to disploy

Enelosed in either ing'e guotes()or double guots ( ")


’numbevs also used along with text i helessary

Syntar evvox Print


grommatical ero.
Python has coding sules If these are bsken,
the python pg am can' understand the
cade, resulting
syrt an evros
Cade
Output
print (" Helle, Code fobe! <Syntax Ex1os: EOF in multi-line
stotement dn line 2

pint('hello, Code tobe!') <Syntax Enos: bad input 6n line 1>


prin t(Hetlo, code Fobe!) <Syntax Evvos: bad tolen T_0P on line
1>
String s
Toining Text
Tnput Outpu t
Marrg"
Mavry Bsousn
"Byoun
’5tings in python is tex t oi thin.quote s

The len' functioo


’It tells you the mumber of charac ters in
Co de a
tet
Output
pxint(len ("vibhav'
Vsest The len function is very useful in Some
ife Case S tike Tuwi tter .
Since Tuottes originally allowed twee bs to be up to
lo characters In Such Scenavio S, developY s need
CYeate a progyom using thhe to
len ) tunc tio9 to
how long tweet is check
and it ts
ChaYac ter limit ithin tne

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:

Using Batksla Shes:


using a back sla Sh ) before the inner double 2uotes
in ashing help s ython un derstand that the inner
dou ble guotes pa t of the sing and not its
end.

Code :

Pairt("he Sece t to suceSs is \"keep Coding )


Ou tput :
The Secre t to Succes S
is, Coding"
Conca tenahpn
Soining sting s the
s'gn.
eo de :

paint ("'fonily" 4 "tsiends") Output:


(ode:
Familyf iends
6utput:
Familyand faiends
Vaviables
VaviobleG are used tb store informaton
Fos exaple
nameJame s Sni th phone number '23- 4s6- 90 ".
Code: Ouipu t:

Puint (name) James Smith


print (phene- number) 23-4s6 - 99O

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

Syntaxk1or.bad input on line 2


indentation erro resolved by cnsuing that
is he
Code ines
Popes ly aligned and lnden ted
Py thon Comments
Comments are special n otes in bue code.
"Comments in py ton are notes in the Code help'ng kb
elav iy its puspose and make main tenan(e easies

"In py thon cxeating comment 15 simple. Just start the


line jth the tt
# Symbol Anying after the on thot tine
is a Commend

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

Python oill eecute and print only


the lines withot
to mments Thus, Dtput oill be This wi thout
" Commen ts help des csibe the reason
behind the code
" Comments plevent future corfusion.
" Keep Comments simple and eosy to urders tand.
c.tommerce Prcduct Name Creator :

"De signing a proga m tha b Srn oo thly merge s


and t mode l name ba Sed on ubev
bran d name and
ilAput to 9et prodct name

Code'
This is a Product Name Creotor

2 bs ond. name =inpu t (°Enter the brand names)


3 model- name input ("Enter the model name:)
4 produetname - br and- n a me " + mode l- na me
5 print (Produet - name)
Output
Inter the brand name.' App le
Entes the mode I name: iPhone
Apple i Phone

You might also like