# 01%20-%20Python%20Basics%20Cheat%20Sheet

course: Academy — 65-GENAI-for-Engineers
module: Academy/65-GENAI-for-Engineers
type: pdf
source_url: https://personal-learn.armco.dev/files/Academy/65-GENAI-for-Engineers/folders/Python_Cheat_Sheets_Folder/01%20-%20Python%20Basics%20Cheat%20Sheet.pdf
pages: 3

---
[page 1]
Python Basics Cheat Sheet
Data Types Operations
OOPS
Operators
NUMBERS: LIST OPERATIONS:
INHERITANCE:
CLASSES AND OBJECTS:
POLYMORPHISM:
ENCAPSULATION:
STRING OPERATIONS:
DICTIONARY OPERATIONS:
NUMERIC OPERATOR:
COMPARISON OPERATOR:
BOOLEAN OPERATOR:
Immutable
Say, a holds 5, b holds 10
Integer: a=2
a b=15+
(a b)==
List=[]
String[i]
String.upper()
String.lower()
String.title()
dict={}
dict[i]=a
key
values
dict[i]
String[i:j]
List[i]=a
List[i]
List[i:j]
append(val)
pop([i])
:Equal
:Defines an  list empty
:Retrieves the character at  position i th
: C onvert strin g  to uppercase
: C onvert strin g  to lo w ercase
: C onvert strin g  to title case
:Defines an  dictionary empty
:Stores  to the k ey a i
:Retrieves the item w ith the k ey i
: G ives all the  items k ey
: G ives all the v a lu es
A  process o f  usin g  details f rom a 
A   is a  f or c l ass blu eprint
A  concept o f  usin g  common
Hi d ing pri v ate d etai l s the  o f  a
ne w  class  the e x istin g  class .w itho u t mo d i f ying
creating o bj ects o bj ect instance, w hile an  is an  o f  a 
c l ass .
operations in  f or .d i ff erent w ays d i ff erent d ata inp u t
class f rom other ob j ects .
:Retrieves characters in the ran g e 
i j to 
:Stores  at  position a i th
:Retrieves the element at  position i th
:Retrieves elements in the ran g e  to i j
:  an item at the A dd s en d
:  and returns item at Remo v es in d e x  i
a   band
a   bor
not  b
(a b)< : Less th a n
(a b)! = : Not Equal
(a b)> = : Gre a ter th a n or e qual  to
(a b)> : Gre a ter th a n
(a b)< = :l ess th a n or e qual  to
b a= 2/ 0 2.0 = 0.0//
- 11 3 = -4//b a= 0%
a b=5 10**
a b= - 5-
a b=5 0*
Comp l e x :c =1 +2jF l oat: b=2. 0
LIST: M utable
a=[ 1, 2 ,3,’W ord ’ ]
TUPLE: Immutable
a=( 1, 2 ,4 )
STRING: Immutable
a=“N ew  String ”
SETS: M utable
a={2 ,3,4, 5 }
DICTIONARY: M utable
a={ 1 : ’ h ello ’, 2: ’ world ’,4 :[ 7 , 8 ]}

[page 2]
Try & Except Block
Function
Conditional Statements
Loop Control Statements
Looping Statements
try :
def new_function() :
def my_function(fname) :
lambda arguments:expression 
x= a,b:a*blambda 
print(x( , ) 5 6
if condition:
for  variable  sequence: in
for  variable  sequence: in
while  condition:
for  variable  sequence: in
while  condition:
while  condition:
if condition:
if condition1:
elif  condition2:
else :
else :
except ExceptionType:
new_function()
my_function( ) "Emil"
my_function( ) "Tobias"
my_function( ) "Linus"
#code that may raise an exception
print(“Hello world”)
print( f name +  "  R e f snes " )
#exec u te code b loc k
#statements
#statements
#statements
#statements
#statements
#statements
#exec u te code i f  condition is tr u e
#exec u te this statement
#exec u te this statement
#exec u te code i f  condition is f alse
#i f  non o f  the a b o v e conditions e v al u ate to tr u e
#exec u te this statement
#code to handle the exception
# fu nction declaration
# g ettin g  parameter
# fu nction callin g
#passin g  callin g
if  ST A TE M E N T :
B R E AK  ST A TE M E N T :
C ON T INU E ST A TE M E N T :
PA SS ST A TE M E N T :
F OR  L OOP:
WHI LE L OOP:
N ESTE D  L OOP:
F or loop statement
W hile loop statement
if -else  ST A TE M E N T :
if -el if -else  ST A TE M E N T :
T erminates the loop containin g
I t s k ips the exec u tion o f  the
Example:
the statement and b rin g s the control o u t o f  the loop .
c u rrent iteration when the condition is met and lets the
wants to do nothin g  when the condition is satis fi ed .  
U sed when the pro g rammer 
loop contin u e to mo v e to the next iteration .
T his control statement in P ython does not terminate or 
s k ip the exec u tion ,  it simply passes to the next
iteration .

[page 3]
Python Basics Using ChatGPT
Installing and Importing the Library
Sending a Prompt to ChatGPT
Handling Multiple Responses:
Handling Multiple Responses:
Sending Additional Parameters
Handling Errors
Retrieving the Generated Text
pip install openai
import  openai
generated_code = 
response.choices[0].text.strip()
response= ( openai.Completion.create
engine = ,”davinci”
prompt = ,”WAP to print ‘Hello, World’”
max_token =10
)
for response.choice  choice in :
generated_te x t = choice.text.strip()
print (generated_te x t)
You  can  use  the  OpenAI  Playground  to  test-out  
prompts  and  see  the  responses  generated  by  
ChatGPT .  The  Playground  is  available  at
Clic k  here to V isit Playground
response= ( openai.Completion.create
engine = ,”davinci”
prompt = ,”WAP to find s u m of 2  n u m b ers”
max_token =1 0,
temperat u re=0. 5,
n=1,
stop =N one
)
tr y:
except  as Ex ception e :
print( ) “E rror :{} ”.format(e)
response= ( openai.Completion.create
engine = ,”davinci”
prompt = ,”WAP to find s u m of 2  no.s”
max_token =1 0
)
open . api_ k e y =” YOUR _AP I _ KEY ” print (generated_te x t)