0% found this document useful (0 votes)
15 views7 pages

Noeyarp Python

This Python Cheat Sheet provides a comprehensive overview of essential functions, operators, data types, and coding examples in Python. It covers topics such as functions for input/output, conditional statements, loops, data type conversions, and assignment operators. Additionally, it includes sample code snippets for practical applications like a shopping program and a guessing game.

Uploaded by

Saurabh Pathak
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)
15 views7 pages

Noeyarp Python

This Python Cheat Sheet provides a comprehensive overview of essential functions, operators, data types, and coding examples in Python. It covers topics such as functions for input/output, conditional statements, loops, data type conversions, and assignment operators. Additionally, it includes sample code snippets for practical applications like a shopping program and a guessing game.

Uploaded by

Saurabh Pathak
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

python Cheat Sheet

by noeyarp via [Link]/25806/cs/6980/

Functions Functions (cont) Python Identifier

print() displays inform​ation on the screen. complex are of the form a + bJ, where a Defi​ is a name used to identify a variable,

input() receives inform​ation from the user. (complex and b are floats and J (or j) nit​i function, class, module or other object.
numbers) represents the square root of -1 on An identifier starts with a letter A to Z or
int() converts a value to an integer.
(which is an imaginary number). a to z or an underscore (_) followed by
float() change number to be decimal The real part of the number is a, zero or more letters, unders​cores and
number. and the imaginary part is b. digits (0 to 9).
str() a list of number​,letter and symbols. Complex numbers are not used
much in Python progra​mming. Python Assignment Operators
len() the length of the string.

# Comment or no effect Oper​ator Desc​rip​tion **E


Code
long Also called longs, they are integers
(long of unlimited size, written like integers Python Assignment Operators
name = "noey RAWIDA​"
integers and followed by an uppercase or
print (name.u​pp​er())
) lowercase L. Oper​ator Desc​rip​tion Exam​ple
print (name.l​ow​er())
print (name.c​ap​ita​lize()) = Assigns values from c=a+b
print (name.t​it​le()) right side operands to assigns
left side operand value of a
Condit​ional + b into c

+=Add It adds right operand c += a is


if A statement that the writer given a
AND to the left operand equivalent
condition
and assign the result to c = c +
else A statement that can be combined with
to left operand a
an if statement.
- It subtracts right c -= a is
elif A statement that allows you to check
=Subtract operand from the left equivalent
multiple expres​sions for TRUE and AND operand and assign to c = c -
execute a block of code as soon as the result to left a
one of the conditions evaluates to
operand
TRUE.

while A statement that acting resembles like


a loop.

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 1 of 7. [Link]
python Cheat Sheet
by noeyarp via [Link]/25806/cs/6980/

Python Assignment Operators (cont) Random Code Number to Binary Code

*=Multiply It multiplies right c = a is import random mystring = "hello"


AND operand with the equivalent mylist = ['Dog'​,'F​ish', 'Cat', print (mystring)
left operand and to c = c a
'Bear'] firstname = input( "what is your
assign the result
counter = 0 first name?")
to left operand
while counter < 10: lastname = input( "what is your
/=Divide It divides left c /= a is
​ ​ ​ ​ ​ ​ran​dom​_item = [Link] last name?")
AND operand with the equivalent
(mylist) fullname = firstname + " " +
right operand and to c = c / ac
assign the result /= a is ​ ​ ​ ​ ​ ​print (rando​m_item) lastname

to left operand equivalent ​ ​ ​ ​ ​ ​counter = counter + 1 print (fullname)


to c = c / a letter​number = int(input( " what

%=Modulus It takes modulus c %= a is Number to Binary Code is letter number? " ))


AND using two equivalent if letter​number >le​n(f​ull​name):
mystring = "hello"
operands and to c = c % ​ ​ ​ ​print ( " invalid letter
print (mystring)
assign the result a
number, try again! " )
to left operand firstname = input( "what is your
else:
first name?")
**=Exp​onent Performs c = a is
​ ​ ​ ​letter = (
AND expone​ntial lastname = input( "what is your
equivalent
fullna​me[​let​ter​number] )
(power) to c = c a last name?")
​ ​ ​ ​print (letter)
calcul​ation on fullname = firstname + " " +
operators and ​ ​ ​ ​num​ber​letter = int(input( "how
lastname
assign value to the many times to print letter " ))
print (fullname)
left operand ​ ​ ​ if number​letter >100:
letter​number = int(input( " what
//=Floor It performs floor c //= a is ​ ​ ​ ​ ​ ​ ​print ( " too many letters
is letter number? " ))
Division division on equivalent to print! " )
if letter​number >le​n(f​ull​name):
operators and to c = c // a ​ ​ ​ ​else:
assign value to the ​ ​ ​ ​print ( " invalid letter
​ ​ ​ ​ ​ ​ ​ ​print (letter *
left operand number, try again! " )
number​letter )
else:
​ ​ ​ ​letter = (
Addition
fullna​me[​let​ter​number] )
​ ​ ​ ​print (letter) string + string combine together
​ ​ ​ ​num​ber​letter = int(input( "how string + number crash
many times to print letter " ))
number + number math - addition
​ ​ ​ if number​letter >100:
​ ​ ​ ​ ​ ​ ​print ( " too many letters
to print! " )
​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​print (letter *
number​letter )

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 2 of 7. [Link]
python Cheat Sheet
by noeyarp via [Link]/25806/cs/6980/

Multip​​li​c​ation and Exponents Python Variables Types Data Type Conversion (cont)

string * number combine that string multiple Number dict(d) Creates a dictio​nary, d must be a
times. String sequence of (key,v​alue) tuples.

string * string crash List frozen​set Converts to a frozen set.

number * math - multiply (s)


Tuple
number chr(x) Converts an integer to a character.
Dictionary
string ** string crash unichr(x) Converts an integer to a Unicode
character.
number ** math - exponents Data Type Conversion
number ord(x) Converts a single character to its
Func​tion Desc​rip​tion
string ** crash interger value.

number int(x Converts x to an integer. base hex(x) Converts an integer to a


[,base]) specifies the base if x is a string. hexade​cimal string.
Loop float(x) Converts x to a floati​ng-​point oct(x) Converts an interger to an octal
number. string.
While Repeats a statement or group of
Loop statements while a given condition is long(x Converts x to a long integer. base

TRUE. It tests the condition before [,base] ) specifies the base if x is a string. Data Types
executing the loop body. str(x) Converts object x to a string Integer -256, 15
For Executes a sequence of statements repres​ent​ation.
Float -253.23, 1.253e-10
Loop multiple times and abbrev​iates the repr(x) Converts object x to an
String " Hel lo", 'Goodbye', " " " Mul til ine
code that manages the loop variable. expression string.
"""
Nested You can use one or more loop inside comple​x(r Create a complex number.
Boolean True,False
Loop any another while, for or do..while eal
loop. [,imag]) List [ value, ... ]

eval(str) Evaluates a string and returns an Tuple ( value, ... )1


object. Dictionary { key: value, ... }
tuple(s) Converts s to a tuple. Set { value, value, ... }2
list(s) Converts s to a list

set(s) Converts s to a set.

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 3 of 7. [Link]
python Cheat Sheet
by noeyarp via [Link]/25806/cs/6980/

Python Shop Code Python Shop Code (cont) Random Choice Code

print ("welcone to our shop") ​ ​ ​ ​ ​ ​ ​ ​price = price+80 import random


price=0 ​ ​ ​ ​ ​ ​ ​ ​print( "the price now mylist =
size=(​'s'​,'m​','​l',​'xl') is",​price) ['beag​le'​,'p​ome​ran​ian​','​pug​','​gol​de
colour​=('​red​','​bla​ck'​,'w​hite') elif shirtc​olour ==('wh​ite'): n​','​chi​hua​hua']
sock=(​'wa​nt'​,'not want') ​ ​ ​ ​ ​ ​ ​ ​price = price+90 score = 0
print (size) ​ ​ ​ ​ ​ ​ ​ ​print( "the price now chances = 3
shirt = (input​('what shirt size do is",​price) start_over = 0
you want?')) else: random​_item =
if shirt == ('s'): ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"our shop don't have [Link]​oic​e(m​ylist)
​ ​ ​ ​ ​ ​ ​ ​price = price+70 this colour​") while chances > 0:
​ ​ ​ ​ ​ ​ ​ ​print( "the price now print (size) ​ ​ ​ ​sta​rt_over = 0
is",​price) pant = (input​('what pant size do ​ ​ ​ ​ran​dom​_item =
elif shirt ==('m'): you want?')) [Link]​oic​e(m​ylist)
​ ​ ​ ​ ​ ​ ​ ​price = price+80 if pant == ('s'): ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​print( "the price now ​ ​ ​ ​ ​ ​ ​ ​price = price+70 ​ ​ ​ ​while start_over < 1:
is",​price) ​ ​ ​ ​ ​ ​ ​ ​print( "the price now ​ ​ ​ ​ ​ ​ ​ ​print ("-=​-=-​=-=​-=-​=-=​-
elif shirt ==('l'): is",​price) =-​=-=​-=-​=-=​-=-​=-=​-")
​ ​ ​ ​ ​ ​ ​ ​price = price+90 elif pant ==('m'): ​ ​ ​ ​ ​ ​ ​ ​print ("Gu​essing Game")
​ ​ ​ ​ ​ ​ ​ ​print( "the price now ​ ​ ​ ​ ​ ​ ​ ​price = price+80 ​ ​ ​ ​ ​ ​ ​ ​print ("-=​-=-​=-=​-=-​=-=​-
is",​price) ​ ​ ​ ​ ​ ​ ​ ​print( "the price now =-​=-=​-=-​=-=​-=-​=-=​-")
elif shirt ==('xl'): is",​price) ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​wor​ds:​", mylist)
​ ​ ​ ​ ​ ​ ​ ​price = price+100 elif pant ==('l'): ​ ​ ​ ​ ​ ​ ​ ​guess = input(​"​Guess a
​ ​ ​ ​ ​ ​ ​ ​print( "the price now ​ ​ ​ ​ ​ ​ ​ ​price = price+90 word: ")
is",​price) ​ ​ ​ ​ ​ ​ ​ ​print( "the price now ​ ​ ​ ​ ​ ​ ​ if (guess in mylist):
else: is",​price) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"our shop doesn't elif pant ==('xl'): ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​if(​guess ==
have this size.") ​ ​ ​ ​ ​ ​ ​ ​price = price+100 random​_item ):
print (colour) ​ ​ ​ ​ ​ ​ ​ ​print( "the price now ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​That's
shirtc​olour= (input​('what colour of is",​price) correc​t!")
shirt do you want?')) else: ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​score = score +
if shirtc​olour == ('red'): ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"our shop doesn't 100
​ ​ ​ ​ ​ ​ ​ ​price = price+70 have this size.c​hoose again") ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sco​re:​",
​ ​ ​ ​ ​ ​ ​ ​print( "the price now score)
is",​price) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​sta​rt_over = 2
elif shirtc​olour ==('bl​ack'): ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, wrong
choice! ")
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​chances =
int(ch​ances) -1
​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, that is
not even in the list")
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​chances = int(ch​ances)
-1

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 4 of 7. [Link]
python Cheat Sheet
by noeyarp via [Link]/25806/cs/6980/

Random Choice Code (cont) Loop list Code (cont) Vocabulary (cont)

​ ​ ​ ​ ​ ​ ​ ​if(​chances > 0): print(​use​rlist) integer whole number or counting number.


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Chances ​ ​ ​ ​ ​ ​ ​ number

remain​ing​:",c​hances) ​ ​ ​ ​ ​ ​ ​ float the number in decimal.


​ ​ ​ ​ ​ ​ ​ ​else: number

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​sta​rt_over = 2 Math syntax grammar structure of language.


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"Game Over! The value the number or the string can be
!= unequal or not equal to
word was ", random​_item) store in valuable.
== equal to example;(a == b)
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Chance loop
is not true.
remain​ing​:", chances) module the text for storing for python
< less than example;(a < b)
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Final score:​", code.
is true.
score)
> more than example;(a > b) blank

is not true. comment


Loop list Code
<= less than or equal example;(a <= b) input receives inform​ation from the user.
def creatlist(quitword) : is true.
code
​ ​ ​ ​print ('Keep entering words to >= more than or equal example;(a >= b)
print to show inform​ation.
add to the list') is not true.
syntax make possible to the parse
​ ​ ​ ​print ('Quit when word =', % modulo or find the remainder
error
quitword)
<> If values of two example;(a <> b)
boolean true/false
​ ​ ​ ​mylist = [] operands are not is true. This is
​ ​ ​ ​while True: equal, then condition similar to !=
Python Arithmetic Operators
​ ​ ​ ​ ​ ​ ​ ​use​r_word = input(​'Enter a becomes true. operator.

word to add to the list:') Oper​at Desc​rip​tion Exam​

​ ​ ​ ​ ​ ​ ​ if user_word == quitword Vocabulary or ple

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return mylist variable holds a value and can be changed. + Adds values on either side a+b

​ ​ ​ ​ ​ ​ ​ ​dup​lic​ateword = False Addition of the operator. = 30


string a list of characters such as numbers,
​ ​ ​ ​ ​ ​ ​ for item in mylist: letters, symbols.
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ if item == user_word:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​dup​lic​ateword =
True
​ ​ ​ ​ ​ ​ ​ for item == user_word:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​dup​lic​ateword =
True
​ ​ ​ ​ ​ ​ ​ if duplic​ateword == True:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​print ('Dupl​icate
Word')
​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​myl​[Link]​pen​d(u​ser​_word
)
userlist = create​Lis​t("s​top​")

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 5 of 7. [Link]
python Cheat Sheet
by noeyarp via [Link]/25806/cs/6980/

Python Arithmetic Operators (cont) Statements Code

- Subtracts right hand a-b= If Statem​ent mystring = "hello"


Subtra​ctio operand from left hand 30 if expres​sion: print (mystring)
n operand. statements firstname = input( "what is your
* Multiplies values on either a*b= elif expres​sion:
first name?")
Multip​lic​a side of the operator. 200 statements
lastname = input( "what is your
tion else:
last name?")
statements
/ Division Divides left hand operand b/a=
While Loop fullname = firstname + " " +
by right hand operand. 2
while expres​sion: lastname
% Divides left hand operand b%a statements print (fullname)
Modulus by right hand operand =0 For Loop letter​number = int(input( " what
and returns remainder.
for var in collec​tion:
is letter number? " ))
** Performs expone​ntial a ** b statements
if letter​number >le​n(f​ull​name):
Exponent (power) calcul​ation on = 10 Counting For Loop
​ ​ ​ ​print ( " invalid letter
operators. to the for i in range(s t art, end [, step]):
power number, try again! " )
statements
20 (start is included; end is not) else:

// Floor Division - The 9//2 = ​ ​ ​ ​letter = (


division of operands 4 and Area of circle Code fullna​me[​let​ter​number] )
where the result is the 9.0/2.0 ​ ​ ​ ​print (letter)
while True:
quotient in which the = 4.0 ​ ​ ​ ​num​ber​letter = int(input( "how
​ ​ ​
digits after the decimal
many times to print letter " ))
point are removed. ​ ​ ​ ​use​r_r​adius = input(​"What is
​ ​ ​ if number​letter >100:
the radius​?")
​ ​ ​ ​ ​ ​ ​print ( " too many letters
​ ​ ​ ​radius = float(​use​r_r​adius)
to print! " )
​ ​ ​ pi = 3.1415
​ ​ ​ ​else:
​ ​ ​ ​area= pi radius * 2
​ ​ ​ ​ ​ ​ ​ ​print (letter *
​ ​ ​ ​print ("The area of the circle
number​letter )
is", area)

Print Code

name = "noey RAWIDA"


print (name.u​pp​er())
print (name.l​ow​er())
print (name.c​ap​ita​lize())
print (name.t​it​le())

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 6 of 7. [Link]
python Cheat Sheet
by noeyarp via [Link]/25806/cs/6980/

List Code Random Code 2

shoppinglist = ['tshirt' , 'pants' , import random


'socks'] intlist = [1,2,3,4]
for myvariable in shoppi​nlist: random_int =
​ ​ ​ ​print (myvar​iable) [Link]​oic​e(i​ntlist)
print (shopp​ing​lis​t[1]) print(​int​lis​t,r​and​om_int)
for number in range(5): fplist = [1.0, 2.0, 3.0, 4.0]
​ ​ ​ ​print (number) random_fp = [Link]​oic​e(f​plist)
print(​fpl​ist​,ra​ndo​m_fp)
Count Down Code strlist =
['boo​k',​'pe​n',​'ba​g',​'pe​ncil']
#create a program that receives a
random_str =
number from the user and count down
[Link]​oic​e(s​trlist)
from that number on the same line
print (strli​st,​ran​dom​_str)
#recive the number from the user as a
mylist = [1, 1.0, 'beagle' ]
string
random​_item =
user_n​umber= input(​"​enter number​")
[Link]​oic​e(m​ylist)
#convert the user number to an integer
print(​myl​ist​,ra​ndo​m_item)
number = int(us​er_​number)
myvar1 = 1
#setup the countdown string
myvar2 = 2
countd​own​_string = "​"
myvar3 = 3
while number > 0:
varlist =[myvar1, myvar2, myvar3]
​ ​ ​ #add the number to the string
random_var =
​ ​ ​ ​#su​btract 1 from the number
[Link]​oic​e(v​arlist)
​ ​ ​ ​cou​ntd​own​_string =
print(​var​lis​t,r​and​om_var)
countd​own​_string + str(nu​mber) + "​"
​ ​ ​ ​number = number-1
print (count​dow​n_s​tring)
#output should look like this
# if the user enter 5:
#5 4 3 2 1
#print (count​dow​n_s​tring)

By noeyarp Published 15th February, 2016. Sponsored by [Link]


[Link]/noeyarp/ Last updated 8th March, 2016. Learn to solve cryptic crosswords!
Page 7 of 7. [Link]

You might also like