Multiplication and Exponents
string * number |
stringsting...(number) |
string* string |
Fail!! |
number * number |
Multiply |
string ** string |
Fail!! |
number ** number |
Exponent |
string ** number |
Fail!! |
Random
import random intlist = [1,2,3,4,5,6,7,8,9,0] random_int = random.choice(intlist) print(intlist,random_int) |
fplist = [4.6,3.2,7.7,6.2] random_fp = random.choice(fplist) print(fplist,random_fp) |
strlist = ['uik','lok','pki','roo'] random_str = random.choice(strlist) print(strlist,random_str) |
mylist = [589,56.3,'suay'] random_mylist = random.choice(mylist) print(mylist,random_mylist) |
myvar1 = 1 myvar2 = 2 myvar3 = 3 varlist = [myvar1,myvar2,myvar3] random_var = random.choice(varlist) print(varlist,random_var) |
Example
Print (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 |
Capital letter
name = "tim GIRARD" |
print (name.upper()) → TIM GIRARD |
print (name.lower()) → tim girard |
print (name.capitalize()) → Tim girard |
print (name.title()) → Tim Girard |
Vocabulary
Variable |
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 |
Length |
the length of the string |
|
|
Countdown Number
user_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) |
Convert to Binary String
user_number = input ("Please enter 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) |
Reverse Number
word = input("enter the word") letter_num = 0 reverse = "" while letter_num < len(word): reverse = word[letter_num] + reverse letter_num = letter_num + 1 print ("Reverse: ",reverse) |
Convert to Binary String
user_number = input ("Please enter 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) |
Symbol
== |
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 |
|
|
Naming Conventions
Rule 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 |
Area of circle
user_radius = input("Enter the radius of the circle") radius = float(user_radius) pi = 3.1415 answer = pi / (radius * 2) print ("The area of the circle is",answer) |
Addition
string + string |
combine together |
string + number |
Fail |
number + number |
plus |
number - number |
minus |
Operation
x in s |
True if x is contained in s |
x not in s |
True if x is not contained in s |
s.index(item) |
position in s of item |
s[i] |
ith item in s (0-based) |
len(s) |
length of s |
Function
Print() |
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 |
Countdown Number
user_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) |
|