Cheatography
https://cheatography.com
1
input( ) |
show |
print( ) |
show answer |
int( ) |
number |
str( ) |
float( ) |
change number to decimal |
while |
loop |
if: elif else: |
Boolean |
True/False |
len( ) |
length of |
in |
in |
import random |
import |
random.choice(mylist) |
random in mylist |
while True |
forever loop |
range() |
range(5)=0,1,2,3,4 |
5 Reverse
word = input("Please enter your name: ") |
index = 0 |
reverse = ' ' |
while index < len(word): |
reverse = word[index] + reverse |
index = index +1 |
print ("Reverse: ",reverse) |
area
def areaofellipse(radius1,radius2): |
-pi=3.1415 |
-area=piradius1radius2 |
-return area |
area1=areaofellipse(2,3) |
print(area1) |
evenodd count
evencount=0 |
oddcount=0 |
while True: |
-num=int(input("Enter a positive integer:")) |
-if num<0: |
--print("Even numbers:",evencount) |
--print("Odd numbers:",oddcount) |
--break |
-else: |
--if(num%2)==0: |
---evencount=evencount+1 |
--else: |
---oddcount=oddcount+1 |
|
|
2
+ |
add |
- |
subtract |
/ |
divide (ans in float/decimal) |
** |
power |
* |
multiply |
= |
assign a value |
== |
compare 2 values |
!= |
not equal to |
> |
more than |
< |
less than |
>= |
more than or equal to |
<= |
less than or equal to |
: |
for user to enter |
# |
comments (do not show) |
% |
remainder |
// |
divide(ans in integer) 2.5=2 |
3(3+1) |
CRASH because no *. Have to be |
|
3*(3+1) |
6 binary
user_number = ' ' |
while user_number != '0' : |
user_number = input("Enter a number to convert to binary") |
number = int(user_number) |
binary_string = ' ' |
while (number > 0): |
remainder = number%2 |
binary_string = str(remainder) + binary_string |
number = number//2 |
print ("Binary string is", binary_string) |
fibonacci
num1=0 |
num2=1 |
fibonacci=num1+num2 |
myoutput="0,1" |
while fibonacci<50: |
-myoutput=myoutput+", "+str(fibonacci) |
-num1=num2 |
-num2=fibonacci |
-fibonacci=num1+num2 |
print(myoutput) |
|
|
hex
while True: |
-user_number=input("Enter a number:") |
-num=int(user_number) |
-hex_string="" |
-while(number>0): |
--remainder=number%2 |
--if remainder==10: |
---remainder="A" |
--elif............................. |
--hex_string=str(remainder)+hex_string |
--num=num//16 |
-print("Hexadecimal string is 0x"+hex_string) |
3
naming consention |
letters |
a,A,_ |
|
numbers |
|
underscore_ |
4
string+string |
fusion |
string+number |
crash |
number+number |
normal math |
string*string |
crash |
string*number |
fusion |
number*number |
normal math |
string**string |
crash |
string**number |
crash |
number**number |
normal math |
while
0 |
mystring="" |
01 |
count=0 |
012 |
while count<5: |
0123 |
-mystring=mystring+str(count) |
01234 |
-print(mystring) |
|
-count=count+1 |
while
mylist=[1,2,3] |
num=0 |
while num<len(mylist): |
-print(mylist[num]) |
-num=num+1 |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment