VocabularyVariable | Holds a value and can be change | String | a list of characters such as numbers, letters, symbols | Integer number | Whole number/counting number | Float number | Number in decimal | Syntax | Grammar/Structure of language | Boolean | True/False | length | the length of the string |
FunctionPrint() | Show information that you want on screen | Int() | Change number to be number integer | input() | receives info from the user | str() | converts the value to a string | float() | converts the value to a floating point | len() | The length of the string | # | comment,no effect | ''' | Multi-line comment |
Multiplication and Exponentsstring * number | stringsting...(number) | string* string | Fail!! | number * number | Multiply | string ** string | Fail!! | number ** number | Exponent | string ** number | Fail!! |
Convert to Binary Stringuser_number = ' '
while user_name != ' '
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)
Simple Functiondef printdefinitions(word):
if word == ("variable"):
print ("""A variable is the value that can change""")
elif word == ("function"):
print ("""A function is the blog of code that can be reused""")
elif word == ("parameter"):
print ("""A parameter is something given to the function""")
elif word == ("agrument"):
print ("""An agrument is something given to the function""")
elif word == ("string"):
print ("""A string is a lsit of characters""")
elif word == ("function call"):
print ("""A function call makes your function run""")
else:
print ("Unknown word")
return
while True: #keep the loop go forever
user_input = input("Enter word: ")
printdefinitions(user_input)
|
Simple Functiondef printdefinitions(word):
if word == ("variable"):
print ("""A variable is the value that can change""")
elif word == ("function"):
print ("""A function is the blog of code that can be reused""")
elif word == ("parameter"):
print ("""A parameter is something given to the function""")
elif word == ("agrument"):
print ("""An agrument is something given to the function""")
elif word == ("string"):
print ("""A string is a lsit of characters""")
elif word == ("function call"):
print ("""A function call makes your function run""")
else:
print ("Unknown word")
return
while True: #keep the loop go forever
user_input = input("Enter word: ")
printdefinitions(user_input)
Enter word: hello
Unknown word
Enter word: function
A function is the blog of code that can be reused
Enter word: variable
A variable is the value that can change
Enter word:
area/volume of
|
| | Symbols== | equal to | != | not equal to | < | less than | <= | less than or equal to | > | greater than | >= | greater than or equal to | + | add | - | subtract | * | multiply | / | divide and quotient is float | // | divide and quotient is integer | ** | exponent | % | modulo: the remainder |
Additionstring + string | combine together | string + number | Fail | number + number | plus | number - number | minus |
Sample codemystr = "hellp THERE"
print (mystr.upper()) -all letters will become big HELP THREE
print (mystr.lower()) -all letters will become small help three
print (mystr.capitalize()) -First letter of first word will become big Help three
print (mystr.title())- first letter of each words will become big Help Three
|
ExamplePrint (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr,”Hi”,2,1.0) -- commas
mystr = “Hi”
mystr ← name
“Hi” ← value can change
print (int(1.5)) → 1
print (int(“2”)) → 2
print (float(1)) → 1.0 anything to a float
Modulo/Remainder %
print (4%2) → 0
print (30%7) → 2
Area of the circledef areaOfCircle(r):
pi = 3.1415
area = pir*2
return area
user_radius = float (input("Enter the radius: "))
print ('The area of the circle is',areaOfCircle(user_radius))
MaxValuedef max2(num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print (max2(4,5))
print (max2(33,5))
def max3(num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print (max3(1,2,3))
5
33
3
|
Maxlistdef maxlist(list):
maxvalue = list[0]
for item in list:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
777
|
| | Naming ConventionsRule for giving name
-letter
-numbers
-underscore_
Valid name
- _myStr
- my3
- Hello_there
Invalid name
- 3my="hi" -- cannot start with number
- first name = "hi"
- first-name
- first+name
|
Capital lettername = "tim GIRARD"
print (name.upper()) → TIM GIRARD
print (name.lower()) → tim girard
print (name.capitalize()) → Tim girard
print (name.title()) → Tim Girard
circle areadef areaofcircle(radius):
if radius <= 0:
return "Error: invalid raadius"
pi = 3.1415
area = pi (radius*2)
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaofcircle(user_radius))
Enter the radius: 2
The area of the circle is 12.566
Enter the radius: 0
The area of the circle is Error: invalid raadius
|
Countdown Numberuser_number = input("Please enter the number")
number = int(user_number)
countdown_string = ""
while number>0:
countdown_string = countdown_string + str(number)
number = number - 1
print (countdown_string)
Palindromedef isPalindrome(word):
reverse = ""
letter_num=0
while letter_num<len(user_input):
reverse = user_input[letter_num]+reverse
letter_num = letter_num+1
if reverse==word:
return True
else:
return False
while True :
user_input = input("Enter a word")
if user_input == "quit":
break
isPal = isPalindrome(user_input)
if isPal == True:
print (user_input,'is parindorm')
else:
print (user_input,'is not parindorm')
break
Enter a word113311
113311 is parindorm
Enter a word123
123 is not parindorm
Enter a wordquit
|
Short word per linemystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
H
e
l
l
o
|
Basic Functiondef myprint(text):
print ("" + str(text) + "")
return
myprint("opal")
hello it's bacon
opal
def myprintnew(text, decoration):
print (decoration + str(text) + decoration)
return
myprintnew("opal", "m")
hello it's bacon
opal
mopalm
def doubleit(number):
return number * 2
print (doubleit(3))
print (doubleit(doubleit(4)))
hello it's bacon
opal
mopalm
6
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment