__itertools__
Badges: 11
Rep:
?
#1
Report Thread starter 2 years ago
#1
Hi,
I am trying to learn Python and I am having hard time trying to improve. I know the basic data types, conditionals, while and for loops, functions and a little bit of OOP and was looking for online resources to help me practice the language.

I have tried Codewars, CodingBat and Project Euler, each with little success.
In Codewars, the problems are completely random (and difficult) and you can't practice specific topics like recurtions or generators, for instance.

Project Euler is all about maths. Except for first 25 or so problems, the problems are inaccessible.

CodingBat is a great with a lot of themes but only for Java. They have some exercises for Python but only trivial ones. I was looking for something like CodingBat but for Python. In particular, I would like to practice lambda, map and reduce functions.

Any tips on learning Object Oriented Concepts will also be appreciated.

I want to be able to understands the codes like this:
Spoiler:
Show


import numpy as np ### Interfaceclass Environment(object): def reset(self): raise NotImplementedError('Inheriting classes must override reset.') def actions(self): raise NotImplementedError('Inheriting classes must override actions.') def step(self): raise NotImplementedError('Inheriting classes must override step') class ActionSpace(object): def __init__(self, actions): self.actions = actions self.n = len(actions) ### SimpleRoomsEnv Environment class SimpleRoomsEnv(Environment): """Define a simple 4-room environment""" """actions: 0 - north, 1 - east, 2 - west, 3 - south""" def __init__(self): super(SimpleRoomsEnv, self).__init__() # define state and action space self.S = range(16) self.action_space = ActionSpace(range(4)) # define reward structure self.R = [0] * len(self.S) self.R[15] = 1 # define transitions self.P = {} self.P[0] = [1, 4] self.P[1] = [0, 2, 5] self.P[2] = [1, 3, 6] self.P[3] = [2, 7] self.P[4] = [0, 5, 8] self.P[5] = [1, 4] self.P[6] = [2, 7] self.P[7] = [3, 6, 11] self.P[8] = [4, 9, 12] self.P[9] = [8, 13] self.P[10] = [11, 14] self.P[11] = [7, 10, 15] self.P[12] = [8, 13] self.P[13] = [9, 12, 14] self.P[14] = [10, 13, 15] self.P[15] = [11, 14] self.max_trajectory_length = 50 self.tolerance = 0.1 self._rendered_maze = self._render_maze() def step(self, action): s_prev = self.s self.s = self.single_step(self.s, action) reward = self.single_reward(self.s, s_prev, self.R) self.nstep += 1 self.is_reset = False if (reward < -1. * (self.tolerance) or reward > self.tolerance) or self.nstep == self.max_trajectory_length: self.reset() return (self._convert_state(self.s), reward, self.is_reset, '') def single_step(self, s, a): if a < 0 or a > 3: raise ValueError('Unknown action', a) if a == 0 and (s-4 in self.P[s]): s -= 4 elif a == 1 and (s+1 in self.P[s]): s += 1 elif a == 2 and (s-1 in self.P[s]): s -= 1 elif a == 3 and (s+4 in self.P[s]): s += 4 return s def single_reward(self, s, s_prev, rewards): if s == s_prev: return 0 return rewards[s] def reset(self): self.nstep = 0 self.s = 0 self.is_reset = True return self._convert_state(self.s) def _convert_state(self, s): converted = np.zeros(len(self.S), dtype=np.float32) converted[s] = 1 return converted def _get_render_coords(self, s): return (int(s / 4) * 4, (s % 4) * 4) def _render_maze(self): # draw background and grid lines maze = np.zeros((17, 17)) for x in range(0, 17, 4): maze[x, :] = 0.5 for y in range(0, 17, 4): maze[:, y] = 0.5 # draw reward and transitions for s in range(16): if self.R[s] != 0: x, y = self._get_render_coords(s) maze[x+1:x+4, y+1:y+4] = self.R[s] if self.single_step(s, 0) == s: x, y = self._get_render_coords(s) maze[x, y:y+5] = -1 if self.single_step(s, 1) == s: x, y = self._get_render_coords(s) maze[x:x+5, y+4] = -1 if self.single_step(s, 2) == s: x, y = self._get_render_coords(s) maze[x:x+5, y] = -1 if self.single_step(s, 3) == s: x, y = self._get_render_coords(s) maze[x+4, y:y+4] = -1 return maze def render(self, mode = 'rgb_array'): assert mode == 'rgb_array', 'Unknown mode: %s' % mode img = np.array(self._rendered_maze, copy=True) # draw current agent location x, y = self._get_render_coords(self.s) img[x+1:x+4, y+1:y+4] = 2.0 return img



Source: Microsoft's 'Reinforcement Learning Explained' Course.
0
reply
I'm God
Badges: 19
Rep:
?
#2
Report 2 years ago
#2
Have you tried Codecademy?
There are also courses on edX and Coursera which are free unless you want a certificate.
Also, if you type in "learn python" into Google, a website called learnpython.org will show up (first one in the results I believe) which is pretty good

winterscoming may also be able to help?
Last edited by I'm God; 2 years ago
1
reply
CraigBackner
Badges: 20
Rep:
?
#3
Report 2 years ago
#3
the best website to learn python: https://cscircles.cemc.uwaterloo.ca/
0
reply
__itertools__
Badges: 11
Rep:
?
#4
Report Thread starter 2 years ago
#4
(Original post by I'm God)
Have you tried Codecademy?
There are also courses on edX and Coursera which are free unless you want a certificate.
Also, if you type in "learn python" into Google, a website called learnpython.org will show up (first one in the results I believe) which is pretty good
Thanks for the reply.
I have already completed the codecademy course (the free one on Python). edX and Coursera courses fall into the tutorial categories, you can't do the assignment problems without buying the course for most of them. I was looking for resourses to practice Python and learn along the way.
0
reply
__itertools__
Badges: 11
Rep:
?
#5
Report Thread starter 2 years ago
#5
(Original post by ltsmith)
for learning frameworks and libraries:
udemy

for improving problem solving ability:
firecode.io
leetcode
hackerrank
Thank You!
0
reply
winterscoming
Badges: 19
Rep:
?
#6
Report 2 years ago
#6
(Original post by I'm God)
Have you tried Codecademy?
There are also courses on edX and Coursera which are free unless you want a certificate.
Also, if you type in "learn python" into Google, a website called learnpython.org will show up (first one in the results I believe) which is pretty good

winterscoming may also be able to help?
Thanks for the tag, agree with you on edx/coursera, both are really good

(Original post by __itertools__)
Thanks for the reply.
I have already completed the codecademy course (the free one on Python). edX and Coursera courses fall into the tutorial categories, you can't do the assignment problems without buying the course for most of them. I was looking for resourses to practice Python and learn along the way.
You shouldn't need to pay for anything on EdX or Coursera in order to unlock all of the course material, including the assignments/problem sets - when you enrol on the courses, there's an option to click 'Audit' which bypasses the paid-for certificate and gives you the same access as someone who has paid for it.

Sometimes the EdX and Coursera courses are split into a 'series' of courses - You can audit those too, but you need to click the individual courses before you'll find the Audit link. 'Audit' isn't always in a very easy-to-find place, so it's more about getting used to navigating those sites.

In any case, the MIT Python course is available separately on MIT OpenCourseware (instead of EdX):
https://ocw.mit.edu/courses/electric...hon-fall-2016/

And the Coursera (University of Michigan / "Dr Chuck") material is all available on Dr. Chuck's website, including the assignments and auto-grader: https://www.py4e.com/

There's a pretty good free e-book here which covers a lot of the language, but also covers a lot of more advanced concepts about how to structure and design your code in Python too: http://greenteapress.com/wp/think-python-2e/

The official Python docs are the authoritative reference for the whole language - not the best place to learn from, but it works OK as a tutorial too: https://docs.python.org/3/tutorial/index.html

You can get into these individual Python courses from Georgia Tech for free too - again, no need to enrol on the series or pay for the certificates to unlock the problem sets: https://www.edx.org/professional-cer...ting-in-python


The last thing to say is that books, MOOCs, tutorials, and other beginner/learning resources can get you so far but eventually you'll reach a point where the best thing you can do is focus on something much bigger by trying a project such as a Game, GUI app, Web app, etc. When you feel like you're confident with all the most important parts of the language (e.g. you're near the end of the 'Thing Python' book and it all makes sense), then have a look at some of these:

Web apps in Python using the 'Flask' framework: https://blog.miguelgrinberg.com/post...-i-hello-world

Or maybe use these to practice by writing games:
https://inventwithpython.com/pygame/
https://realpython.com/pygame-a-primer/
https://www.youtube.com/playlist?list=PL6gx4Cwl9DGAjkwJocj7vlc_mFU-4wXJq

One reason for doing this is that once you're past the core Python language and moving into things like 'OO' design, you really need a bigger project in order to be able make use of those concepts (and it's good practice).

A lot of the more advanced concepts in programming exist to help programmers manage big, complex problems, so if you're writing lots of short programs which are less than a couple of hundred lines of code and only 2-3 source files, then 'OO' programming doesn't really change very much; but it makes a huge difference if you've got a big program built from dozens of source files, thousands of lines of code, etc. "OO" is really a technique to help you think and organise your code, so it's a way of preventing yourself from getting lost and drowning in complexity -- the problem which "OO" solves is to make really big, complex things seem more managable and easier to reason over.

Also, don't get too hung up on 'OO' - it's only one paradigm (way of thinking), and there are other paradigms in Python too (paradigms aren't mutually exclusive from each other - so you can mix them up, especially in a language like Python). The Functional Programming paradigm is really important, and so are topics like Python's "Decorators", as well as itertools, functools, generators, (All that stuff is covered in the Think Python book and the official docs)
2
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

What support do you need with your UCAS application?

I need help researching unis (25)
13.74%
I need help researching courses (12)
6.59%
I need help with filling out the application form (9)
4.95%
I need help with my personal statement (76)
41.76%
I need help with understanding how to make my application stand out (43)
23.63%
I need help with something else (let us know in the thread!) (3)
1.65%
I'm feeling confident about my application and don't need any help at the moment (14)
7.69%

Watched Threads

View All