Show Menu
Cheatography

User as float, print half of the that number

user_number = float(input('enter the number:'))
print (user_number/2)

list,print all element from the list using loop

# for loop solution
mylist = [1,2,3,4,5]
for number in mylist:
    print(number)

# while loop solution
mylist = [1,2,3,4,5]
num = 0
while number < len(mylist):
    print(mylist(number))
    number = number+1

print Fibonacci series between 0 to 50 using loop

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)

The areaOf­Ellipse using pir2r1 ( two parameter)

def areaOfEllipse(radius1,radius2):
    pi = 3.1415
    area = piradius1radius2
    return area
area1= areaOfEllipse(2,3)
print(area1)

User_input

 
user_input = input(­"­Enter a value: ")

Comparing Values:

 
When you compare two values, the result is a Boolean
(True or False) E.g. 2 == 3 is False
 == is equal to
 != is not equal to
 < less than
 <= less than or equal to
 > greater than
 >= greater than or equal to
 and
 or
 not
True or anything is always True
False and anything is always False

Converting between different data types:

 
word = str(3) #converts 3 to a string "­3"
num = int("3.5­") #converts "­3.5­" to an integer 3
num = float(­"­3") #converts "­3" to a float 3.0

Combining Strings (Conca­ten­ation)

 
"­hi" + "­the­re" == "­hit­her­e"
"­hi" * 5 == "­hih­ihi­hih­i"
 

The output of follwing code

Y = True
print (not y or 2 < 3) output = False

print alleven numbers from 1 100 using while loop

number = 0
while number < 100:
   number =number + 2
   print (number)

use a for loop to print the following

0
01
012
0123
01234
mystring = ""
for number in range(5):0
    mystring = mystring + str(number)
    print (mystring)
# range(2) = 01

receive input from user, convert intege­r,print , 5

number = int(input("Enter a number:"))
print(num*5)

output of the program

mystring = ""
count = 0
while count < 5:
    mystring = mystring + str(count)
    print(mystring)
    count = count + 1
0
01
012
0123
01234

Data types

 
String - a list of characters e.g. "­abc­123­$%^­", or
empty string "­"
Integer - whole numbers, and negative numbers e.g. -5,
0, 2
Floating Point - decimal numbers e.g. 1.5, 2.0, -2.99
Boolean - True or False

the loop doesn't go forever

gameover = 0
while (gameover ==0):
    
    print("hello")
    gameover = 1

Printing values:

 
print(­"­hel­lo", "­the­re") #displays hello there
print(­"­hel­lo" + "­the­re") #displays hellothere

Comments

 
# hashtag – everything after # is a comment not code
"­"­"
Double quote - Multi-line comment, everything in
between three double quotes is a comments
"­"­"
''' Single quote - Multi-line comment, everything in
between three single quotes is a comments '''
 

Receive number from user, number is determine by 3

user_number = int(input("Enter the number:"))
remainder = user_number%3
if remainder == 0:
    print (user_number, "is divisible by 3")
else:
    print (user_number, "is not visible by 3")

Multip­lic­ati­onTable

def multiplicationTable():
   user_number = input( "Enter the number:")
   number = int(user_number)
   count = 1

   While count <=10:
       print(number, "",count,"=",numcount)
       count = count + 1

Making number is negative, Zero or positive

user_number = int(input("Enter a number"))
if user_number > 0:
    print(user_number,"is positive")
elif user_number < 0:
    print(user_number,"is negative")
else:
    print(user_number,"is equal to zero")

print all even number from -100 to -1 (while loop)

user_number= -100
while user_number< -1:
    print(user_number)
    user_number= user_number + 2

positive intege­r,n­egative print HM odd, even

evencount = 0
oddcount = 0
while True:
    number = int(input("Enter a number"))
    if number < 0:
        print ("Even number",evencount)
        print ("odd number", oddcount)
        break
    else:
        if (number%2) == 0:
            evencount = evencount + 1
        else:
            oddcount = oddcount + 1

Basic Math Operat­ions:

 
+ addition, - subtra­ction
/ divide with answer as a float. E.g. 5/2 == 2.5
// divide with answer as an integer. E.g. 5//2 == 2
* multiply
exponent. E.g. 2 power 3 == 2 3
% modulo. Gives the remainder when dividing
e.g. 33 % 10 == 3
All math operations use the sa

print out fifth character from the variable

myword = "hellothere"
print(hellothere[4])

convert integer and multiply by 10 loop

while True:
    user_number = int(input("enter a number")
    number= int(user_number)*10
    print(number)

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by Chutima Rakyu