Cheatography
https://cheatography.com
Addition
string + string |
combine together |
string + number |
crash |
number + number |
math - addition |
Even, Odd number
even = 0
odd = 0
while True:
user_num = int(input("Enter the number :"))
if user_num >= 0:
if user_num % 2 == 0:
even = even + 1
else:
odd = odd + 1
else:
print ("Even number :", even)
print ("Odd number :", odd)
break |
Functions
print() |
displays information on the screen |
input() |
receives information from the user |
int() |
converts a value to an integer |
Math Operations
== |
equal to |
9 == 9 |
!= |
not equal to |
55 != 2 |
+ |
addition |
9 + 3 = 14 |
- |
minus |
5 - 1= 4 |
* |
mutiply |
5 * 5 = 25 |
/ |
divide |
4 / 2 = 2 |
% |
remainder |
3 % 2 = 1 |
** |
power |
2 ** 2 = 4 |
< |
less than |
1 < 2 |
> |
more than |
2 > 1 |
<= |
less than equal to |
2 =< 3 |
>= |
more than equal to |
3 => 2 |
// |
divide by (no decimal point) |
3/300 = 100 |
|
|
Vocabulary
variable |
holds a value & can be changed |
string |
a list of characters such as numbers, letters, symbols. |
Addition
string + string = Combine together
string + number = CRASH!
number + number = Addition (Math) |
code
print (name.upper()) |
print (name.lower()) |
print (name.capitalize()) |
print (name.title()) |
Convert integer to binary
integer = input("Enter a number:")
integer = int(integer)
remainder = integer
binary = ""
while integer != 0 :
remainder = integer % 2
integer = int(integer / 2)
remainderstr = str(remainder) |
Reverse Word
while True:
word = input("Please enter a word")
index = 0
reverse = ' '
while int(index) < len(word):
reverse = word[index] + (reverse)
index = int(index) + 1
print ("Reverse: ", reverse) |
|
|
Multiplication & Exponents
string * number |
combine that string multiple times |
string * string |
crash |
number * number |
math - multiply |
string ** string |
crash |
number ** number |
math- exponents |
string ** number |
crash |
naming convention
Rules for giving names
- letters
- numbers (Can't be the first letter)
- underscore _
Valid
- _mystr
- my3
- Hello_there
Invalid name
- 3my="hi" -- cannot start with number
-firstname ="hi"
= first-name |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment