Loop Positive Integer
even = 0
odd = 0
while True:
user_input = int(input("Enter a number :"))
user = user_input % 2
if user_input > 0:
if user == 0:
even = even + 1
elif user != 0:
odd = odd + 1
print(user_input)
else:
print ("Even number = ", even)
print ("Odd number = ", odd)
break |
Fibonacci
num1 = 0
num2 = 1
fibonacci = num1 + num2
output = "0,1"
while fibonacci < 50:
output = output + "," + str(fibonacci)
num1 = num2
num2 = fibonacci
fibonacci = num1 + num2
print (output) |
Fibonacci
num1 = 0
num2 = 1
fibonacci = num1 + num2
output = "0,1"
while fibonacci < 50:
output = output + "," + str(fibonacci)
num1 = num2
num2 = fibonacci
fibonacci = num1 + num2
print (output) |
Variable Name
Good Variable Name
my_string = "123"
_hello = "1"
mystring = 1
value1 = 1
#can have integers, lowercase/uppercase, underscores
#the first character must be a lowercase/uppercase or an underscore
Bad Variable Name
email@ = 2
1value = 2 |
Upper Lower List
mystr = "hello THERE"
print (mystr.upper()) #Upper case all the letter in a word
print (mystr.lower()) #Lower case all the letter in a word
print (mystr.capitalize()) #Capital only first letter of first word
print (mystr.title()) #Capital first letter of every word.
#List in python
shoppinglist = ['Dogs', 'Cats', 'Mouses', 'Giraffe']
print(shoppinglist[2]) #Will print 'Mouses'
#while loop
item_number = 0
while item_number < len(shoppinglist):
print ("list item:", shoppinglist[item_number])
item_number = item_number + 1
#for loop
other = 0
for cat in shoppinglist:
other = other + 1
# print ("List item:", cat)
print (other) |
Calculator
def calc(num1, num2, operation):
if operation == "sum":
return sum(num1, num2)
elif operation == "product":
return product(num1, num2)
elif operation == "diff":
return diff(num1, num2)
elif operation == "div":
return div(num1, num2)
else:
print ("Unknown Operation")
def sum(a, b):
return a + b
def product(a, b):
return a * b
def diff(a, b):
return a - b
def div(a, b):
if b == 0:
return ("Error: Undefined value")
else:
return a // b
print(calc(12, 12, "sum"))
print(calc(9, 18, "diff"))
print(calc(20, 10, "product"))
print(calc(12, 4, "div")) |
Guessing Game
#PWTK 1002
scores = 0
chances = 3
while chances > 0:
print ("-=-=-=-=-=-=-=-=-=-=-Guessing Game-=-=-=-=-=-=-=-=--=-=-=-")
import random
mylist = ['apple', 'banana', 'papaya', 'melon', 'orange', 'grape', 'mango']
print (mylist)
random_item = random.choice(mylist)
user_guess = input("Guess a word: ")
if user_guess == random_item:
print ("That's correct")
scores = scores + 100
print ("Scores =", scores)
else:
chances = chances - 1
print ("Chances left: ", chances)
if user_guess in mylist:
print ("That's incorrect")
else:
print ("Sorry, that is not even in the list!")
if chances == 0:
print ("The word was: ", random_item)
print ("Final score =", scores)
print ("GAME OVER!!!!!!!!") |
Vocabulary
String |
A list of characters such as numbers, letters, symbols |
Variable |
Holds a value and can be changed |
Syntax |
The set of rules that defines the combinations of symbols |
Boolean |
Identified True or False (true is not the same as True, false is not the same as False) |
Modulo |
Finds the remainder after division of one number by another |
Radius of a Circle
while True:
#Ask the user for a radius of a circle
user_radius = input("What is the radius of the circle ")
#Convert the given radius to a floating point
radius = (float(user_radius))
#Make a variable called pi
pi = 3.1415
#Calculate the area of the circle using exponents
area = (pi (radius * 2))
#Display the area of the circle to the user
print("The area of the circle is", area) |
|
|
Basic Info
Basic Python Programming Language
:(colon) = syntax
Syntax (=) Grammar
Variable = Something that cahnges(numbers, words)
Number vs Strings: my var = 1 + 2
print my var = 3
my var 2 = "1" + "2"
print my var "12"
hello= "hello" + "It's me"
print hello = "helloIt'sme"
If 1==2:
When you do division in programming the program will add decimal even if it doesn't have the decimal EX: 10.0
my var = "yourname"[0] (the first letter in programming is 0 not 1)
== equal to
!= not equal to
> Greater than
>= Greater than or equal to
<= Less than or equal to
< Less than
print (len(fullname))
if 1 == 2:
print ("true")
else:
print ("false")
if 2 == 2:
print ("true")
else:
print ("flase")
print ("false2") |
Palindrome
def isPalindrome(word):
reverse = ""
for item in user_word:
reverse = item + reverse
reverse_item = reverse
if reverse_item == user_word:
return True#(reverse_item, ("is a palindrome"))
else:
return False#(reverse_item, ("is not a palindrome"))
while True:
user_word = input("Enter a word: ")
length = len(user_word)
if user_word == 'quit':
break
else:
print (length)
numlen = 0
while numlen < length / 2 + 1:
if user_word[numlen] != user_word[-numlen-1]:
print (user_word,"is not a palindrom")
break
numlen += 1
else:
print (user_word,"is a palindrome") |
Return Max Number
def max2(num1, num2):
maxvalue = num1
if num2 > maxvalue:
masvalue = num2
return maxvalue
def max3(num1, num2, num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
def maxlist(list):
maxvalue = list[0]
for num in list:
if num > maxvalue:
maxvalue = num
return maxvalue
print (maxlist([1,2,3,4,5])) |
Binary
while True:
user_number = input("Put the number: ")
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) |
Loop Range
#Creating List
mylist = [1,2,3,4,5,6]
mylist2 = ['hi', 'hello', 'anything']
mylist3 = [1, 'hello', 2.5]
print (mylist)
print (mylist2)
print (mylist3)
#How to make a list with all numbers from 0-10
mynumbers = range(11) #0-10 (Number starts with 0)
for num in mynumbers:
print (num)
mylist2.append('another item') #Adding item in a list
print (mylist2) |
Reverse
word = input("Input a word: ")
reverse = ""
letter_num = 0
'''
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
'''
for item in word:
reverse = item + reverse
print ("Reverse: ",reverse) |
Command
# Hashtag |
Add comment |
# CAN WRITE ANYTHING HEREEE |
''' (3 Apostrophe) |
Long comment |
''' ALSO HEREEEEE ''' |
print |
To display something |
print (var) |
'' '' |
Assign something in a variable |
mystr = ("George") |
int() |
Set the number to interger |
integer = int(20) #with no decimal |
str() |
Convert a variable to string |
String = str(integer) |
input() |
Gain information from the user |
Name = input(" Put your name here: " |
float() |
Convert the number with decimal |
Num = float(2) #the answer will be 2.0 |
len() |
Find the length of the string |
num1 = ("George"),, num2 = len(num1) #Answer will be 6 |
|
|
List RandomChoice
import random
intlist = [1, 2, 3, 4, 5]
random_int = random.choice(intlist)
print (intlist, random_int)
fplist = [2.2, 3.5, 4.8, 6.2, 7.9]
random_fp = random.choice(fplist)
print (fplist, random_fp)
strlist = ['burger', 'cheese', 'ham', 'bacon', 'sandwich', 'pizza']
random_str = random.choice(strlist)
print (strlist, random_str)
mylist = [4, 6, 8, 11.4, 12.8, 17.6,'coco', 'latte', 'mocha']
random_item = random.choice(mylist)
print (mylist, random_item)
myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print (varlist, random_var) |
Countdown
user_number = input("Please enter a number: ")
number = int(user_number)
countdown_string = ""
while number > 0:
countdown_string = countdown_string + str(number)
number = number - 1
#add the number to the string
#subtract 1 from the number
print (countdown_string) |
Area of Triangle
# Pom Wintakorn 1002
def areaofTriangle(base, height):
return 0.5 base height
user_base = float(input("Enter the base of the triangle: "))
user_height = float(input("Enter the height of the triangle: "))
print ("The area of the triangle is", areaofTriangle(user_base, user_height))
area = areaofTriangle(user_base, user_height)
def volumeofPrism(area, height):
return area * height
user_height2 = float(input("Enter the second height of the triangle: "))
print ("The volume of the triangular prism is", volumeofPrism(area, user_height2)) |
Hexadecimal
while True:
user_number = input("Put the number: ")
number = int(user_number)
#Loop the command
hex_string = ''
while (number > 0):
remainder = number % 16
if remainder == 10:
remainder = 'A'
elif remainder == 11:
remainder = 'B'
elif remainder == 12:
remainder = 'C'
elif remainder == 13:
remainder = 'D'
elif remainder == 14:
remainder = 'E'
elif remainder == 15:
remainder = 'F'
hex_string = str(remainder) + hex_string
number = number // 16
print ("hexadecimal string is 0x"+ hex_string) |
Loop Review
#While loop
mylist = [1,2,3]
index = 0 #set to 0 because that is the first item in the list
while index < len(mylist):
print (mylist[index])
index = index + 1
#For loop
for item in mylist:
print(item) |
List Practice
import random
intlist = [1, 2, 3, 4, 5]
random_int = random.choice(intlist)
print (intlist, random_int)
fplist = [2.2, 3.5, 4.8, 6.2, 7.9]
random_fp = random.choice(fplist)
print (fplist, random_fp)
strlist = ['burger', 'cheese', 'ham', 'bacon', 'sandwich', 'pizza']
random_str = random.choice(strlist)
print (strlist, random_str)
mylist = [4, 6, 8, 11.4, 12.8, 17.6,'coco', 'latte', 'mocha']
random_item = random.choice(mylist)
print (mylist, random_item)
myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print (varlist, random_var) |
Number and String
"String" + "String" |
Put both string together |
Number + "String" |
CRASH! |
Number + Number |
Addition(Math) |
"String" * "String" |
CRASH! |
"String" * Number |
Print that string that number times |
Number * Number |
Multiplication(Math) |
String ** String |
CRASH! |
String ** Number |
CRASH! |
Number ** Number |
Exponent(Math) |
Mathematics
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division (Result with floating point) |
// |
Division |
** |
Exponent |
% |
Modulo (Find remainder) |
== |
Equal to |
>= |
Greater than or equal to |
<= |
Less than or equal to |
!= |
Not equal to |
< |
Less than |
> |
More than |
Multiplication Table
def multiplicationTable():
innum = int(input("Enter a number: "))
for i in range(1,11):
output = innum*i
print (str(innum) + "*" + str(i) + "=" + str(output))
multiplicationTable() |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment