Python Basics and Practice Problems on Functions
Python Basics and Practice Problems on Functions
Python Basics
1 Topics covered
Python Syntax
Variables and data types
Storing lots of data in memory: Lists and Tuples
Functions
Comments in code
In [1]: x = 10
print(x)
y = 'galg'
print(y)
10
galg
149100.0
Hello world
In [7]: x = 50
print(x)
y = "noida"
print(y)
50
noida
In [3]: simple_sum = 1 + 5
print(simple_sum)
In [14]: x_y = 10
print(x_y)
10
In [4]: x = 30
y = 5
print(x*y)
150
Example:
In [5]: x = 25
y = 3
print(x + y) # addition
print(x - y) # subtraction
print(x * y) # multiplication
print(x / y) # true division
print(x // y) # floor divisipon
print(x % y) # modulus
print(x ** y) # exponentiation
print(-x) # negation
28
22
75
8.333333333333334
8
1
15625
-25
Example:
In [12]: bar = 10
foo = -bar
print(foo)
-10
Tips:
tom
<class 'int'>
<class 'str'>
<class 'int'>
<class 'str'>
<class 'float'>
In [25]: x = True
y = False
spam = 3.142
eggs = 10000000
print(type(x))
print(type(y))
print(type(spam))
print(type(eggs))
<class 'bool'>
<class 'bool'>
<class 'float'>
<class 'int'>
Using Lists is a simple and flexible way to store variables and data
Using [] and , to define a list
[1, 2, 9, 0, 5]
In [1]: bar = ['spam', [2, 'eggs'], 82.96, True, 22,True, [2, 'true']]
length_of_bar = len(bar)
print(length_of_bar)
2
20
[1, 5, 10, 2, 8, 9, 10, 20, 23, 1, 5]
In [16]: foo = []
del foo[2] # Remove a specific index (and the value on this index)
print(foo)
['ttk']
['ttk', 'swan']
['ttk', 'bar', 'swan']
['ttk', 'bar']
['bar']
print(foo[1])
print(bar[2])
2
7
In [18]: foo[1] = 4
print(foo)
bar[2] = 7
print(bar)
[4, 4, 7, 8, 3]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-73bfa8f6bd90> in <module>
2 print(foo)
3
----> 4 bar[2] = 7
5 print(bar)
4 Functions
We have already seen some Python built-in functions: print() , type() and
len()
Python has lots of them
If you are not sure how they work you can use the help() function!
In [3]: help(print)
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
In [18]: help(len)
len(obj, /)
Return the number of items in a container.
178
My random integer is 78
u1 = randint(1, 100)
u2 = gauss(0, 1)
4.2.1 Motivation
You have been asked to convert degrees celsius to fahrenheit conversions
In [1]: deg_celsius = 40
deg_fahrenheit = 9.0/5.0 * deg_celsius + 32
print(deg_fahrenheit)
deg_celsius = 10
deg_fahrenheit = 9.0/5.0 * deg_celsius + 32
print(deg_fahrenheit)
104.0
50.0
In [6]: deg_celsius = 0
deg_fahrenheit = celsius_to_fahrenheit(deg_celsius)
print(deg_fahrenheit)
# print(deg_celsius,'C =',deg_fahrenheit,'F')
32.0
4.166666666666667
function_name only contains letters, numbers, and _, always starts with a letter
Lines follow : must be indented by 4 spaces (use tab key or 4 spaces)
return sum3nums
def square(num):
num2 = num ** 2
return num2
81
data_0 = [1, 2, 3, 4, 5]
results = list_info(data_0)
print(results)
#print("results = {0} has type {1}".format(results, type(results)))
(15, 5, 3.0)
In [29]: help(celsius_to_fahrenheit)
celsius_to_fahrenheit(deg_celsius)
Converts degrees celcius to degrees fahrenheit
Keyword arguments:
deg_celsius -- a float temperature in degree celsius, e.g., 18.5
In [1]: from math import pi # import the constant pi from math module
def area_of_circle(radius):
# area of a circle with the radius
area = pi * radius ** 2
return area
radius = 4
area = area_of_circle(radius)
print(area)
50.26548245743669
In [31]: help(area_of_circle)
area_of_circle(radius)
In [3]: help(print)
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Number >= 75
11
1 + 1
In [3]: y = '1' + 1
print(y)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-4b1149ab9b70> in <module>
----> 1 y = '1' + 1
2 print(y)
In [4]: p = 1 + 1
print(p)
# Driver code
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}
None
{'d': 6, 'c': 4, 'a': 10, 'b': 8}
True False
In [4]: # Python program to find the largest number among the three input numbers
In [ ]: