0% found this document useful (0 votes)
12 views5 pages

Cs Practice

cs lesson practice

Uploaded by

ybp6t5tytf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Cs Practice

cs lesson practice

Uploaded by

ybp6t5tytf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

len(str): returns the number of characters in a string

s[x] -> returns the character at the xth position


slicing: s[start:end:step]

#single line comment


“””
multi line comment
“””
functions:
len(): takes a string and returns the integer lenghts of the string characters
round(): takes a oat value and returns the rounded value as a oat
abs (): takes a oat/int value and returns the absolute value of the argument
round ():
ceil (): returns the smallest integer greater than or equal to x
oor (): returns the largest integer less than or equal to x
sqrt (): returns the square root of x
pi: pi…
fl
fl
fl
fl
string operators:
in
not in

not a = true if a is false, false if a is true


a and b = true if both are true, false if either is false
a or b = true if either or both are true, false if neither are true

if/else: if the statement is true the rst statement will be executed (if), if the statement is false, the
second statement will be executed (else)

for *variable* in *sequence*:


range
start: starting number of sequence
stop: generate numbers up to but not including the stopping value
step: update/di erence between each number in the sequence

default values are:


start = 0 step = 1
loop until value is stop -1, inclusive

for loops
mysum = 0 (initialize)
for ? in range(x,y,z):
mysum += ?
print(mysum)

mysum=0 (initialize)
for i in range (7,10)
MYSUM += İ
print(mysum)
output=24

x=4
for i in range(x)
print(i)
output= 0,1,2,3

letters = “abcdef”
for i in range(0, len(letters)):
print(letters[i])

output=abcdef

break
immediaately exists whatever loop it is in
skips remianing expressions
break exits only containing loop

random
import random
num = random.randint(1,10)
generates a value between the two values, inclusive
ff
fi
lists
empty list: my_list = [ ]
list: my_list = [1,2,3,4,5]
display a list: print(my_list)
display the number of elements in a list: print(len(my_list))

indexing from 0, accessing an element: print(my_list[1]), print(my_list[3]+1]


indexing with variables: i=2, my_list[i-1]
updating an element: my_list[1] = 12
print(my_list)

assigning an element AT AN INDEX changes the value.

traversing a list
for i in my_list:

appending: my_list.append(x) -> adds the element x to the end of the list

insert: my_list.insert(y,x) -> inserts x at the index of y

nd: my_list.index(x) -> returns the index of the rst occurrence of x in my_list

removing: my_list.pop(x) -> removes and returns the item at index x in my_list
if no index is given, it removes the last element of the list

my_list.remove(x) -> deletes the rst occurrence of x from my_list

concatenation: ourfriends = myfriends + yourfriends

x=[1,16,9,4]
sum(x) will give 30
max(x) will give 16
min(x) will give 1
x.sort() will make x=[1,4,9,16]
x.sort(reverse=True) will make x=[16,9,4,1]
fi
fi
fi
tuple
empty tuple: t1 = ( )
tuple with 3 values: t2 = (1,”two”,3)
print(t1)
print(t2)
display an element in a tuple: print(t2[2])
display the type of an element: print( type(t2[1]))
TUPLES ARE IMMUTABLE. CANNOT ASSIGN.
concatenation: t1 = t1 + t2
indexing: print( t1 [2] )
silcing: print(t1 [1:3])

dictionary
keys must be unique in a dictionary

dictionary = { key1:value1, key2:value 2}


empty dictionary = dict = { }

dict [ key x] -> returns the value associated with a given key.

updating example:
name = input (“Enter person to update: “)
number = input (“Enter phone number: “)
if name in phone:
phone[name] = number
print (name, “updated! (“, phone [name],”)”)
else:
phone [name] = number
print (name, “added! (“, phone[name],”)”)

You might also like