VocabularyVariable | Hold a value and can be changed. It can also be set to a string of words | String | A list of character such as number,letter and symbols | Integer number | Whole number/counting number | Floating point | Numerical values in decimal | Syntax | Grammar/Structure of language | Modulo | Used to find a remainder | Boolean | True or False |
Commandsprint() | A command used to print something | int() | A command used to convert any kinds of number into integer | float() | A command used to convert any kinds of number into decimal numbers | input() | A command used to obtain information from the user | str() | A command used to convert letters or numbers into a string | len() | A command used to determine the length of a string | # | A command used to make statements a comment that does not have any effect on the coding |
Mathematical operators== | equal to | != | not equal to | < | less than | > | more than | <= | less than or equal to | >= | more than or equal to | % | Modulo, used to find a remainder |
| | Additionstring+string | Combine the two strings together | string+number | CRASH! | number+number | Add numbers together |
Multiplication and exponentsstring*number | Duplicate the strings x time = numerical r values | string*string | CRASH! | number*number | multiply numbers together | string**string | CRASH! | number**number | Exponent(math) | string**number | CRASH! |
Variable names conditionFollowing conditions must me followed:
-letters
-numbers
-underscore
Valid name
- _myStr
- my3
- Hello_there
Invalid name
- 3my=”hi” -- cannot start with number
- first name=”hi”
- first-name
- first+name |
Multiplication tablenumber=int(input("enter a number"))
def multiplicationTable (number):
count=1
while count<=10:
print(number,"",count,"=",numbercount)
count=count+1
multiplicationTable(number) |
Fibonaccinum1=0
num2=1
fibonacci=num1+num2
while fibonacci<50:
print(fibonacci)
num1=num2
num2=fibonacci
fibonacci=num1+num2 |
positive integer and negative integerevencount=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 |
| | ExamplePrint (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr,”Hi”,2,1.0) -- commas
mystr = “Hi”
mystr ← Variable
“Hi” ← value can be changed
print (int(1.5)) → 1
print (int(“2”)) → 2
print (float(1)) → 1.0
Modulo/Remainder %
print (4%2) → 0
print (30%7) → 2 |
Reverse Wordwhile 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) |
Binary number conversion#write a program that converts a number to binary
while True: #forever loop is while True:
#get a number from the user
user_number= input("Choose you number")
#convert to integer
number=int(user_number)
binary_string=''
while(number>0):#the number is greater than 0)
remainder= number%2#use modulo %
binary_string= str(remainder)+ binary_string
number= number//2#must use//when you divide
#after the loop print the binary string
print('binary string is',binary_string)
#expected output-5=101
#expected output-3=11
#expected output-2=10 |
Hexadecimal conversion#write a program that converts a number to hexadecimal
while True:
#get a number from the user
user_number= input("Choose your number: ")
#convert to integer
number=int(user_number)
hex_string=''
while(number>0):#the number is greater than 0
remainder= number%16#use modulo %
number= number//16#must use//when you divide
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
#after the loop print the hex string
print('hexadecimal string is 0x'+hex_string)
#expected output-5=101
#expected output-3=11
#expected output-2=1 |
Guess a word gameimport random
Chances=5
Score=0
while Chances>0:
mylist=['Moscow','Berlin','Vancouver','SaintPetersburg','Chicago']
random_item=random.choice(mylist)
print(mylist)
print (random_item)
user_guess=input("guess a word:")
if user_guess==random_item:
print("Chances remaining:",Chances)
print("Correct guess")
Score=Score+100
else:
Chances=Chances-1
print("Sorry, wrong choice")
print("Chances remaining:",Chances)
print("The correct answer was:",random_item)
print("Your score now is:",Score) |
selecting largest valuesdef max2 (num1,num2):
if num1>num2:
return num1
if num1<num2:
return num2
def max3 (num1,num2,num3):
if num1>num2 and num1>num3:
return num1
if num2>num1 and num2>num3:
return num2
if num3>num1 and num3>num2:
return num3
num1=input("Enter your num1:")
num2=input("Enter your num2:")
num3=input("Enter your num3:")
print("the largest number of max3 is:",max3(num1,num2,num3))
print("the largest number of max2 is:",max2(num1,num2)) |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment