r/learningpython • • Sep 13 '25

Want to learn Python, try out EasyDev: Learn to Code!

Post image
2 Upvotes

Link to the App Store page: https://apps.apple.com/app/6749594445

Hello everyone, I wanted to share my new Learn to Code app, EasyDev. I built this app using Swift UI in around 4 months, and it is actually my first ever Swift project. I am coming here to gain some eyes on my app, and give me suggestions on what I can do to make my app better and grow as a developer.

The app itself was made exclusively by me, including all the programming, UI, assets, logos, etc. The actual learning content was also handcrafted by using structures similar to popular websites such as Edube and Learncpp, and there is a lot of interactive and descriptive content that takes inspiration from these websites, which are very popular for their effectiveness in teaching people how to code.

If you are interested in learning programming or just want to check the app out, please consider downloading the app using the link above. Also, if you experience any bugs or errors of any kind, please go to the Discord (in the app store page or directly in the app (Settings -> Join the Discord)) and let me know. Thanks in advance!


r/learningpython • • May 20 '25

How do I install Curses for python? (On windows)

2 Upvotes

I saw some files for this, but idk how to use them, please give me a step-by-step tutorial in the comments, I just want to code snake (I already made something like snake, but I need Curses to make it actually snake)


r/learningpython • • May 17 '25

Want to learn more

2 Upvotes

Hi, im a biological engineering undergrad. I had taken an python course in one of my semesters and as a result I have some basic understanding of the concepts. but however I know that I've just scratched the surface and haven't learnt/applied anything in depth.

I want to learn python little bit more application oriented (in the data science and ML side of things) and I genuinely don't know where to start or how to start.

Any help is greatly appreciated, as to how to move forward with projects or roadmaps. I also would like to have good learning materials with which I can strengthen my fundamentals for the same.

Thanks in Advance!!!


r/learningpython • • Apr 25 '25

I finally figured out what I want to do with my life—but I need your help to see if this plan holds up.

2 Upvotes

Hey everyone. I’m finally at the point where I know what I want to do: I want to become a full-stack developer, and I’m going all in. No more second-guessing, no more endless “should I/shouldn’t I”—this is it. I'm fully committed.

That said, I need a sanity check on my approach, especially from those of you who’ve walked this path or are currently deep in it.

Context:

I work full-time (8–5, Monday to Friday), and every 4th day is a 24-hour shift that can bleed over weekends.

I’m making this shift not just for income—it’s a deliberate move because I’m not being valued where I currently work.

There’s some financial pressure from past debt, but it’s not the main driver.

I’d been working through CS50P and making real progress daily—until I hit file I/O and the concepts beyond. That’s when it hit me: I didn’t build enough fundamentals before diving into something so deep.

I’ve decided to start with JavaScript tutorials—not to switch languages, but to better understand core programming logic in a different way.

My main focus is Python, and I want to be job-ready for at least a junior developer role in the next 3–6 months. I’m aiming to hit above-average junior pay—not from entitlement, but by proving my value with strong projects and deep learning.

My current process (recent breakthrough):

Split each tutorial into two sessions to reduce cognitive overload after work.

Follow the JavaScript tutorial step-by-step (e.g. building a calculator).

After each half of the JS tutorial, rebuild that exact part in Python from memory and logic.

If I hit any walls, I save that version into a “struggled-with-this” folder for review.

Between sessions, I reflect on what worked, what didn’t, and how I can improve it next time.

Everything is tracked and organized in Notion to keep momentum and clarity.

Why I’m posting: I think this could be a really strong system—but I don’t know what I don’t know. I’d love your feedback on:

Does this sound like a good way to approach it?

Am I setting myself up for burnout or does the pacing make sense?

Is the JavaScript-to-Python method helping or just a creative detour?

What would you tweak if this were your plan?

Thanks in advance for any thoughts, warnings, or tweaks! I’d really appreciate it.


r/learningpython • • Jan 22 '25

Looking to practice loops

2 Upvotes

Hello,

Do any of you guys know of any platforms where you can practice writing loops with the help an AI instructor? Thank you for your time.


r/learningpython • • Nov 15 '24

Step by step guide to becoming a backend developer in 2024

Thumbnail
roadmap.sh
2 Upvotes

r/learningpython • • Nov 15 '24

The importance of understanding the code

2 Upvotes

One of the first things I learned when programming is that, when you are creating something, most of your time will be used researching. This can include looking for answers on how to implement something, asking people or AI how to help solve an error, and trying to find that specific functionality in some sort of documentation.

In a world where code snippets are getting flung your way, it's important to understand the code you are sticking in your projects to test it out. Why? Well, if you don't know why the code works, then you will struggle to solve that error, or add functionality.

"Hey, the only thing that matters is that it works!"

Okay, sure. You can either learn Python or fiddle around with...Idk, uh ChatGPT until the code you're using is messier than a child's play room...when they are playing.

The fact that it works is good, but we need to understand how it works in order to learn and progress effectively.

Well, that's enough chatter.

I wrote an article dissecting small, beginner-friendly, and incredibly simple code scripts. I hope you like it.


r/learningpython • • Oct 23 '24

Is there a way to find a package name from an imported module's name?

2 Upvotes

Is there some way of intuiting a package's name from the module's name? For instance, in my source file I see import pywt. How do I learn that the package I need to install is PyWavelets? I have pip_search, and PyWavelets is listed when I search for pywt, but so are a dozen other packages.


r/learningpython • • Oct 22 '24

help with python assignment

Post image
2 Upvotes

I’m new to programming and i’ve messaged my teacher (no response) but all assignments require a submission to go to the next. i’m literally so confused and not sure if it’s an issue with my code or the editor (vs code). if someone could help me out i’d greatly appreciate it!

  • The terminal is only showing one line and not the rest. i’ve tried to many different things and asked chatgpt and youtube and nothing has helped..


r/learningpython • • Jul 01 '24

Learning python guides but for intermediate people

2 Upvotes

So as the title says, I'm trying to learn python and I understand what I've been learning so far. However, it's all so simplistic and things that I already know for the most part. Can any of you suggest a course through youtube or just a great study guide. I want to get into network auditing and hardening, so its imperative I learn at least one or two languages. Also, what should I learn beyond that code wise? Java seems so daunting


r/learningpython • • Jun 06 '24

Need help on assignment

Post image
2 Upvotes

r/learningpython • • May 25 '24

Question about C and python multithreading. Rejected by Stack Overflow.

2 Upvotes

My code is simple and I use gcc offered by MinGW-w64 under win 10 and win 11 to run this. (Both work similar that more threads cost less time.)

```

include <stdio.h>

include <stdlib.h>

include <string.h>

/* 多线程库 */

include <pthread.h>

/* 计时 */

include <time.h>

unsigned char MaxThreadNumber = 8;

ifdef _WIN32

include <windows.h>

void setMaxThreadNumber()

{

SYSTEM_INFO systemInformation;

GetSystemInfo(&systemInformation);

MaxThreadNumber = systemInformation.dwNumberOfProcessors;

}

elif __linux__

include <unistd.h>

define setMaxThreadNumber() MaxThreadNumber=_SC_NPROCESSORS_CONF

else

define setMaxThreadNumber() {}

endif

define pause() puts("\t");system("pause");puts("\t")

define LN 1234567890

unsigned long long int add_from_m_to_n(unsigned long long int m, unsigned long long int n)

{

if (m>n)

{

printf("Error in %s : m = %llu > n = %llu\n",__func__,m,n);

pause();

exit(1);

}

unsigned long long int sum = 0;

for(unsigned long long int counting = m; counting <=n; counting++)

{

sum += counting;

}

return sum;

}

/* args = {m,n,sum}*/

void* threadMission(void* args)

{

unsigned long long int m,n;

m = *((unsigned long long int*)args);

n = *(((unsigned long long int*)args) + 1);

//printf("m = %llu, n = %llu\n",m,n);

*(((unsigned long long int*)args) + 2) = add_from_m_to_n(m,n);

return (void*)(((unsigned long long int*)args) + 2);

}

void test_oneThread()

{

clock_t start ,end;

start = clock();

unsigned long long int sum = add_from_m_to_n(1,LN);

end = clock();

printf("Calculation done. 1 + 2 + 3 + ... + %llu = %llu. Costing %.05g s\n",LN,sum,(double)(end-start)/CLOCKS_PER_SEC);

}

void test_twoThreads()

{

clock_t start ,end;

start = clock();

unsigned long long int mid = LN/2;

pthread_t myThread_1 = NULL;

pthread_t myThread_2 = NULL;

unsigned long long int args_myThread_1[3] = {0,mid,0};

unsigned long long int args_myThread_2[3] = {mid+1,LN,0};

pthread_create(&myThread_1, NULL, threadMission, (void*)args_myThread_1);

pthread_create(&myThread_2, NULL, threadMission, (void*)args_myThread_2);

pthread_join(myThread_1,NULL);

pthread_join(myThread_2,NULL);

unsigned long long int sum = args_myThread_1[2] + args_myThread_2[2];

end = clock();

printf("Calculation done. 1 + 2 + 3 + ... + %llu = %llu. Costing %.05g s\n",LN,sum,(double)(end-start)/CLOCKS_PER_SEC);

}

void test_multiThreads(unsigned char threadnumber)

{

clock_t start ,end;

start = clock();

unsigned long long int mid = LN/2;

pthread_t* myThreads = (pthread_t*)malloc(threadnumber * sizeof(pthread_t));

// memset(myThreads,0,threadnumber * sizeof(pthread_t));

unsigned long long int* args_myThreads_data = (unsigned long long int*)malloc(3 * threadnumber * sizeof(unsigned long long int));

for(size_t counting = 0;counting<threadnumber;counting++)

{

unsigned long long int n = (LN * (counting + 1))/threadnumber;

unsigned long long int m = (LN * counting)/threadnumber + 1;

args_myThreads_data[3 * counting + 0] = m;

args_myThreads_data[3 * counting + 1] = n;

args_myThreads_data[3 * counting + 2] = 0;

//printf("m = %llu, n = %llu, sum = %llu\n",m,n,0);

pthread_create(&myThreads[counting], NULL, threadMission, (void*)(args_myThreads_data + 3 * counting));

}

for(size_t counting = 0;counting<threadnumber;counting++)

{

pthread_join(myThreads[counting],NULL);

}

unsigned long long int sum = 0;

for(size_t counting = 0;counting<threadnumber;counting++)

{

sum+=args_myThreads_data[3 * counting + 2];

}

end = clock();

printf("Calculation done. 1 + 2 + 3 + ... + %llu = %llu. Costing %.05g s\n",LN,sum,(double)(end-start)/CLOCKS_PER_SEC);

}

int main()

{

printf("Multithread Test\n"

"\n"

"Mission: calculate 1 + 2 + 3 + ... + %llu = ?\n",LN);

puts("---------------------------------------------");

puts("One thread");

test_oneThread();

puts("---------------------------------------------");

puts("Two threads");

test_twoThreads();

puts("---------------------------------------------");

setMaxThreadNumber();

printf("Multi threads(%u)\n",(unsigned int)MaxThreadNumber);

test_multiThreads(MaxThreadNumber);

puts("---------------------------------------------");

pause();

return 0;

}

```

And I got this.

```

Multithread Test

Mission: calculate 1 + 2 + 3 + ... + 1234567890 = ?


One thread

Calculation done. 1 + 2 + 3 + ... + 1234567890 = 762078938126809995. Costing 1.158 s


Two threads

Calculation done. 1 + 2 + 3 + ... + 1234567890 = 762078938126809995. Costing 0.581 s


Multi threads(48)

Calculation done. 1 + 2 + 3 + ... + 1234567890 = 762078938126809995. Costing 0.065 s


请按任意键继续. . .

```

and this

```

Multithread Test

Mission: calculate 1 + 2 + 3 + ... + 1234567890 = ?


One thread

Calculation done. 1 + 2 + 3 + ... + 1234567890 = 762078938126809995. Costing 2.304 s


Two threads

Calculation done. 1 + 2 + 3 + ... + 1234567890 = 762078938126809995. Costing 1.156 s


Multi threads(16)

Calculation done. 1 + 2 + 3 + ... + 1234567890 = 762078938126809995. Costing 0.16 s


请按任意键继续. . .

```

on different computers.

When I was learning multithreading and multiprocessing, I learned that when I run multithreading programs computer only create a mission that will called by the same core by switching between one and the other, whilst multiprocessing copies the running environment and give the mission to another core. So if I want a calculation be faster I need to create processes, whilst multiple-file IO or internet stuffs (I haven't tried it so I don't know) are not CPU consuming so threads are enough.

But my C program doesn't seem to obey this.

Before I tried multithreading and multiprocessing in C, I tried both on Python.

```

import time

import threading

import queue

def pause():

from os import system

system("pause")

def 从m加到n(m,n=None):

结果 = 0

if (n is None):

n = m

m = 0

范围 = range(m,n+1)

for i in 范围:

结果+=i

print("\t\t从 {} 加到 {} 结果为 {}".format(m,n,结果))

return 结果

def 从1加到n_单线程(n):

开始时刻 = time.time()

结果 = 从m加到n(n)

结束时刻 = time.time()

print("\t单线程结果:{},用时 {:.2g}s。".format(结果,结束时刻-开始时刻))

def 从1加到n_两个线程(n):

def 线程1任务(返回结果,开始时刻,中点):

结果 = 从m加到n(中点)

结束时刻=time.time()

print("\t线程1结果:1+2+3+...+{}={},用时 {:.2g}s。".format(中点,结果,结束时刻-开始时刻))

返回结果.put(结果)

def 线程2任务(返回结果,开始时刻,中点,终点):

结果 = 从m加到n(中点+1,终点)

结束时刻 = time.time()

print("\t线程2结果:{}+{}+...+{}={},用时 {:.2g}s。".format(中点+1,中点+2,终点,结果,结束时刻-开始时刻))

返回结果.put(结果)

开始时刻 = time.time()

中点 = int(n/2)

终点 = n

结果收集 = queue.Queue()

线程1 = threading.Thread(target=线程1任务,args=(结果收集,开始时刻,中点))

线程2 = threading.Thread(target=线程2任务,args=(结果收集,开始时刻,中点,终点))

线程1.start()

线程2.start()

线程1.join()

线程2.join()

最终结果=0

while (not(结果收集.empty())):

最终结果 += 结果收集.get()

结束时刻 = time.time()

print("\t单线程结果:{},用时 {:.2g}s。".format(最终结果,结束时刻-开始时刻))

def 从1加到n_多个线程(n):

def 生成线程任务(线程序号):

def 线程任务(返回结果,开始时刻,起点,终点):

结果 = 从m加到n(起点,终点)

结束时刻 = time.time()

print("\t线程{}计算从{}加到{}".format(线程序号,起点,终点))

print("\t线程{}结果:{},用时 {:.2g}s。\n".format(线程序号,结果,结束时刻-开始时刻))

返回结果.put(结果)

return (线程序号,线程任务)

from multiprocessing import cpu_count

线程数量 = int(cpu_count())

import numpy

节点 = numpy.linspace(0,n,线程数量+1,dtype="int")

起点 = list(节点[:-1]+1)

起点[0]-=1

终点 = list(节点[1:])

import pandas

print(pandas.DataFrame({"起点":起点,"终点":终点}))

线程信息 = {"线程函数":[],"参数列表":[],"返回值收集":[]}

所有线程 = []

开始时刻 = time.time()

for i in range(线程数量):

线程信息["线程函数"]+=[生成线程任务(i)[1]]

线程信息["返回值收集"]+=[queue.Queue()]

线程信息["参数列表"]+=[(线程信息["返回值收集"][i],开始时刻,起点[i],终点[i])]

print("\t已生成线程{},线程函数是{},参数列表是{}".format(i,线程信息["线程函数"][i],线程信息["参数列表"][i]))

for i in range(线程数量):

所有线程+=[threading.Thread(target=线程信息["线程函数"][i],args=线程信息["参数列表"][i])]

for i in range(线程数量):

print("线程{}开始运行".format(i))

所有线程[i].start()

for i in range(线程数量):

所有线程[i].join()

print("所有线程运行完成")

最终结果=0

for i in range(线程数量):

最终结果+=线程信息["返回值收集"][i].get()

结束时刻 = time.time()

print("\t多线程结果:{},用时 {:.2g}s。".format(最终结果,结束时刻-开始时刻))

if __name__=="__main__":

n=int(123456789)

print("本程序利用计算 1+2+3+...{} 来进行计算".format(n))

print("单线程运算开始执行\n")

从1加到n_单线程(n)

print("结束执行\n\n")

print("---------------------------------")

print("两个线程运算开始执行\n")

从1加到n_两个线程(n)

print("结束执行\n\n")

print("---------------------------------")

print("多个线程运算开始执行\n")

从1加到n_多个线程(n)

print("结束执行\n\n")

pause()

```

More threads runs a little bit slowlier, spending time on creating threads.

```

本程序利用计算 1+2+3+...123456789 来进行计算

单线程运算开始执行

单线程结果:7620789436823655,用时 6.8s。

结束执行


两个线程运算开始执行

线程1结果:1+2+3+...+61728394=1905197343773815,用时 7s。

线程2结果:61728395+61728396+...+123456789=5715592093049840,用时 7s。

单线程结果:7620789436823655,用时 7s。

结束执行


多个线程运算开始执行

线程0开始运行

线程1开始运行

线程2开始运行

线程3开始运行

线程0计算从0加到2572016

线程1计算从2572017加到5144032

线程0结果:3307634438136,用时 0.3s。

线程2计算从5144033加到7716049

线程1结果:9922900742392,用时 0.4s。

线程3计算从7716050加到10288065

线程2结果:16538174762697,用时 0.49s。

线程3结果:23153435922920,用时 0.58s。

线程4开始运行

线程5开始运行

线程6开始运行

线程7开始运行

线程4计算从10288066加到12860082

线程8开始运行

线程5计算从12860083加到15432098

线程6计算从15432099加到18004115

线程4结果:29768715087258,用时 0.85s。

线程9开始运行

线程7计算从18004116加到20576131

线程5结果:36383971103448,用时 1.1s。

线程6结果:42999255411819,用时 1.1s。

线程7结果:49614506283976,用时 1.2s。

线程9计算从23148148加到25720164

线程10开始运行

线程8计算从20576132加到23148147

线程9结果:62845064612652,用时 1.4s。

线程11开始运行

线程8结果:56229772588232,用时 1.3s。

线程10计算从25720165加到28292180

线程10结果:69460307768760,用时 1.6s。

线程11计算从28292181加到30864197

线程11结果:76075604937213,用时 1.7s。

线程12开始运行

线程13开始运行

线程14开始运行

线程15开始运行

线程12计算从30864198加到33436213

线程16开始运行

线程12结果:82690842949288,用时 2s。

线程17开始运行

线程14计算从36008231加到38580246

线程13计算从33436214加到36008230

线程15计算从38580247加到41152263

线程18开始运行

线程14结果:95921378129816,用时 2.3s。

线程17计算从43724280加到46296295

线程19开始运行

线程13结果:89306145261774,用时 2.2s。

线程17结果:115767179614600,用时 2.6s。

线程15结果:102536685586335,用时 2.4s。

线程18计算从46296296加到48868312

线程16计算从41152264加到43724279

线程20开始运行

线程18结果:122382494787168,用时 2.7s。

线程16结果:109151913310344,用时 2.5s。

线程19计算从48868313加到51440328

线程20计算从51440329加到54012345

线程19结果:128997714795128,用时 2.9s。

线程21开始运行

线程20结果:135613035111729,用时 3s。

线程22开始运行

线程23开始运行

线程24开始运行

线程25开始运行

线程21计算从54012346加到56584361

线程26开始运行

线程21结果:142228249975656,用时 3.3s。

线程22计算从56584362加到59156378

线程27开始运行

线程24计算从61728395加到64300410

线程23计算从59156379加到61728394

线程25计算从64300411加到66872427

线程24结果:162074051460440,用时 3.6s。

线程23结果:155458785156184,用时 3.7s。

线程27计算从69444444加到72016460

线程25结果:168689384637123,用时 3.7s。

线程26计算从66872428加到69444443

线程22结果:148843575436290,用时 3.4s。

线程27结果:181919924961684,用时 4s。

线程26结果:175304586640968,用时 3.9s。

线程28开始运行

线程29开始运行

线程30开始运行

线程31开始运行

线程28计算从72016461加到74588476

线程28结果:188535121821496,用时 4.3s。

线程32开始运行

线程29计算从74588477加到77160493

线程29结果:195150465286245,用时 4.4s。

线程33开始运行

线程34开始运行

线程30计算从77160494加到79732509

线程32计算从82304527加到84876542

线程31计算从79732510加到82304526

线程35开始运行

线程33计算从84876543加到87448558

线程31结果:208381005610806,用时 4.8s。

线程33结果:221611458486808,用时 4.9s。

线程36开始运行

线程34计算从87448559加到90020575

线程30结果:201765657002024,用时 4.5s。

线程34结果:228226814811639,用时 5s。

线程35计算从90020576加到92592591

线程36计算从92592592加到95164608

线程35结果:234841993667336,用时 5.2s。

线程32结果:214996192182552,用时 4.7s。

线程37开始运行

线程36结果:241457355136200,用时 5.3s。

线程38开始运行

线程39开始运行

线程40开始运行

线程41开始运行

线程42开始运行

线程37计算从95164609加到97736624

线程37结果:248072528847864,用时 5.7s。

线程43开始运行

线程38计算从97736625加到100308641

线程40计算从102880658加到105452673

线程44开始运行

线程39计算从100308642加到102880657

线程40结果:267918330332648,用时 6s。

线程45开始运行

线程42计算从108024691加到110596706

线程38结果:254687895460761,用时 5.8s。

线程43计算从110596707加到113168723

线程42结果:281148865513176,用时 6.2s。

线程46开始运行

线程44计算从113168724加到115740739

线程43结果:287764244986155,用时 6.3s。

线程41计算从105452674加到108024690

线程44结果:294379400693704,用时 6.4s。

线程39结果:261303064028392,用时 6s。

线程47开始运行

线程41结果:274533704661594,用时 6.1s。

线程45计算从115740740加到118312756

线程46计算从118312757加到120884772

线程47计算从120884773加到123456789

线程46结果:307609935874232,用时 6.7s。

线程47结果:314225325635277,用时 6.9s。

线程45结果:300994785310716,用时 6.6s。

所有线程运行完成

多线程结果:7620789436823655,用时 6.9s。

结束执行

请按任意键继续. . .

```

while multiprocessing does perform an acceleration as expected.

```

from email.mime.multipart import MIMEMultipart

import time

import multiprocessing

def pause():

from os import system

system("pause")

def 从m加到n(m,n=None):

结果=0

if (n is None):

n=m

m=0

范围=range(m,n+1)

for i in 范围:

结果+=i

print("\t\t从 {} 加到 {} 结果为 {}".format(m,n,结果))

return 结果

def 从1加到n_单进程(n):

开始时刻=time.time()

结果=从m加到n(n)

结束时刻=time.time()

print("\t单线程结果:{},用时 {:.2g}s。".format(结果,结束时刻-开始时刻))

创建进程不能用函数内函数

def 进程1任务(进程结果收集,开始时刻,中点):

结果=从m加到n(中点)

结束时刻=time.time()

print("\t进程1结果:{},用时 {:.2g}s。".format(结果,结束时刻-开始时刻))

进程结果收集.put(结果)

def 进程2任务(进程结果收集,开始时刻,中点,终点):

结果=从m加到n(中点+1,终点)

结束时刻=time.time()

print("\t进程2结果:{},用时 {:.2g}s。".format(结果,结束时刻-开始时刻))

进程结果收集.put(结果)

def 从1加到n_两个进程(n):

开始时刻=time.time()

中点=int(n/2)

终点=n

进程结果收集 = multiprocessing.Queue()

进程1=multiprocessing.Process(target=进程1任务,args=(进程结果收集,开始时刻,中点))

进程2=multiprocessing.Process(target=进程2任务,args=(进程结果收集,开始时刻,中点,终点))

进程1.start()

进程2.start()

进程1.join()

进程2.join()

线程1结果 = 进程结果收集.get()

线程2结果 = 进程结果收集.get()

最终结果 = 线程1结果 + 线程2结果

结束时刻=time.time()

print("执行完成,最终结果为 {},用时 {:.2g}s".format(最终结果,结束时刻-开始时刻))

多个进程

def 进程任务(返回结果,开始时刻,起点,终点):

进程名称 = multiprocessing.current_process().name

结果 = 从m加到n(起点,终点)

结束时刻 = time.time()

print("\t进程“{}”计算从{}加到{}".format(进程名称,起点,终点))

print("\t进程“{}”结果:{},用时 {:.2g}s。\n".format(进程名称,结果,结束时刻-开始时刻))

返回结果.put(结果)

def 从1加到n_多个进程(n):

进程数量 = int(0.5*multiprocessing.cpu_count())

import numpy

节点 = numpy.linspace(0,n,进程数量+1,dtype="int64")

起点 = list(节点[:-1]+1)

起点[0]-=1

终点 = list(节点[1:])

进程信息 = {"进程函数":[],"参数列表":[],"返回值收集":[]}

所有进程 = []

开始时刻 = time.time()

返回值收集 = multiprocessing.Queue()

for i in range(进程数量):

所有进程+=[multiprocessing.Process(target=进程任务,args=(返回值收集,开始时刻,起点[i],终点[i]),name="进程{}".format(i))]

for i in range(进程数量):

print("进程{}开始运行".format(i))

所有进程[i].start()

for i in range(进程数量):

所有进程[i].join()

print("所有进程运行完成")

最终结果=0

for i in range(进程数量):

最终结果 += 返回值收集.get()

结束时刻 = time.time()

print("执行完成,最终结果为 {},用时 {:.2g}s".format(最终结果,结束时刻-开始时刻))

if __name__=="__main__":

n = int(123456789)

print("本程序利用计算 1+2+3+...{} 来进行计算".format(n))

print("单进程运算开始执行\n")

从1加到n_单进程(n)

print("结束执行\n\n")

print("---------------------------------")

print("两个进程运算开始执行\n")

从1加到n_两个进程(n)

print("结束执行\n\n")

print("---------------------------------")

print("多个进程运算开始执行\n")

从1加到n_多个进程(n)

print("结束执行\n\n")

pause()

```

result

```

本程序利用计算 1+2+3+...123456789 来进行计算

单进程运算开始执行

单线程结果:7620789436823655,用时 6.6s。

结束执行


两个进程运算开始执行

进程2结果:5715592093049840,用时 3.5s。

进程1结果:1905197343773815,用时 3.5s。

执行完成,最终结果为 7620789436823655,用时 3.5s

结束执行


多个进程运算开始执行

进程0开始运行

进程1开始运行

进程2开始运行

进程3开始运行

进程4开始运行

进程5开始运行

进程6开始运行

进程7开始运行

进程8开始运行

进程9开始运行

进程10开始运行

进程11开始运行

进程12开始运行

进程13开始运行

进程14开始运行

进程15开始运行

进程16开始运行

进程17开始运行

进程18开始运行

进程19开始运行

进程20开始运行

进程21开始运行

进程22开始运行

进程23开始运行

进程“进程3”计算从15432099加到20576131

进程“进程3”结果:92613761695795,用时 0.58s。

进程“进程0”计算从0加到5144032

进程“进程0”结果:13230535180528,用时 0.58s。

进程“进程4”计算从20576132加到25720164

进程“进程4”结果:119074837200884,用时 0.59s。

进程“进程2”计算从10288066加到15432098

进程“进程2”结果:66152686190706,用时 0.59s。

进程“进程1”计算从5144033加到10288065

进程“进程1”结果:39691610685617,用时 0.59s。

进程“进程6”计算从30864198加到36008230

进程“进程6”结果:171996988211062,用时 0.6s。

进程“进程7”计算从36008231加到41152263

进程“进程7”结果:198458063716151,用时 0.6s。

进程“进程8”计算从41152264加到46296295

进程“进程8”结果:224919092924944,用时 0.61s。

进程“进程9”计算从46296296加到51440328

进程“进程9”结果:251380209582296,用时 0.61s。

进程“进程10”计算从51440329加到56584361

进程“进程10”结果:277841285087385,用时 0.62s。

进程“进程13”计算从66872428加到72016460

进程“进程13”结果:357224511602652,用时 0.62s。

进程“进程5”计算从25720165加到30864197

进程“进程5”结果:145535912705973,用时 0.63s。

进程“进程12”计算从61728395加到66872427

进程“进程12”结果:330763436097563,用时 0.63s。

进程“进程15”计算从77160494加到82304526

进程“进程15”结果:410146662612830,用时 0.64s。

进程“进程11”计算从56584362加到61728394

进程“进程11”结果:304302360592474,用时 0.64s。

进程“进程16”计算从82304527加到87448558

进程“进程16”结果:436607650669360,用时 0.64s。

进程“进程20”计算从102880658加到108024690

进程“进程20”结果:542452034994242,用时 0.65s。

进程“进程14”计算从72016461加到77160493

进程“进程14”结果:383685587107741,用时 0.65s。

进程“进程18”计算从92592592加到97736624

进程“进程18”结果:489529883984064,用时 0.65s。

进程“进程17”计算从87448559加到92592591

进程“进程17”结果:463068808478975,用时 0.66s。

进程“进程23”计算从118312757加到123456789

进程“进程23”结果:621835261509509,用时 0.66s。

进程“进程19”计算从97736625加到102880657

进程“进程19”结果:515990959489153,用时 0.66s。

进程“进程21”计算从108024691加到113168723

进程“进程21”结果:568913110499331,用时 0.69s。

进程“进程22”计算从113168724加到118312756

进程“进程22”结果:595374186004420,用时 0.69s。

所有进程运行完成

执行完成,最终结果为 7620789436823655,用时 0.71s

结束执行

请按任意键继续. . .

```

So I got very surprised when I got accelerated even if I just use multithreading in C, but in Python I need multiprocessing.

Can someone explain this to me?


r/learningpython • • May 22 '24

Why is my "if" statement, the only code being executed, and not my "elif"?

2 Upvotes

(I'm on my pc so hopefully my code translates ok for folks on mobile.)

This is for the CS50 Python class (extensions.py if any one is familiar with this exercise). The object is to input something like: "cat.gif", and it should return, "image/gif". Or, "document.pdf" should return, "application/pdf.

The "if" and "elif" are suppose check the "image" array, and "app" array respectfully. The "if" seems to check the image array just fine. However, no matter what file extension I put, whether an app extension, or something made up, it always just runs the "if" part of the if statement and prints "image/whatEverBs_I_typed". Does any one have any insight as to why the elif statement isn't being executed?

image = ["gif", "jpg", "jpeg", "png"]
app = ["pdf", "txt", "zip"]

fileName = input("File name: ").split(".")

if fileName[1] == image[0] or image[1] or image[2] or image[3]:
    print("image/" + fileName.pop(1))
elif fileName[1] == app[0] or app[1] or app[2]:
    print("application/" + fileName.pop(1))

r/learningpython • • May 01 '24

Where does jupyter lab install modules when using pip install

2 Upvotes

I do not want installed modules to be installed on my solid state disk, but on my external hard drive. instead. Is there a way to do this?


r/learningpython • • Mar 20 '24

Getting started with web scraping in Python

2 Upvotes

I've done some basic Python programming, but I'm a bit intimidated by the idea of scraping websites. I've heard it can be a bit tricky to navigate and that there are various considerations to keep in mind. I stumbled upon something called antidetect browsers like GoLogin. Can anyone shed some light on whether they're actually useful for web scraping? I know that they can automate tasks like logging in, navigating through websites, and scraping data.


r/learningpython • • Feb 13 '24

Please help me.

2 Upvotes

I'm coding in Python as a hobby for some years now and I want to code without comments. I'm currently writing the base structure for an AI library and I would like to get feedback how readable/good/bad my code is. The code for controlling the network is here:

from nodes import *


class WrongLayerChosen(Exception):
    pass


class Network:
    def __init__(self, input_nodes: int | list[InputNode], hidden_layers: list[int] | list[list[Node]],
                 output_nodes: int | list[OutputNode]):
        if isinstance(input_nodes, int):
            self._input_layer: list[InputNode] = list(InputNode() for i in range(input_nodes))
        else:
            self._input_layer: list[InputNode] = input_nodes

        if isinstance(hidden_layers[0], int):
            self._hidden_layers: list[list[Node]] = [list(Node(hidden_layers[i - 1]) for j in range(hidden_layers[i]))
                                                     for i in range(1, len(hidden_layers))]
            self._hidden_layers.insert(0, list(Node(input_nodes) for i in range(hidden_layers[0])))
        else:
            self._hidden_layers: list[list[Node]] = hidden_layers

        if isinstance(output_nodes, int):
            self._output_layer: list[OutputNode] = [OutputNode(hidden_layers[-1]) for i in range(output_nodes)]
        else:
            self._output_layer: list[OutputNode] = output_nodes

        self.layer_count = 2 + len(hidden_layers)

    def get_layers(self):
        output_list: list[list[InputNode | Node | OutputNode]] = [self._input_layer]
        for layer in self._hidden_layers:
            output_list.append(layer)
        output_list.append(self._output_layer)
        return output_list

    def get_layer(self, index: int):
        return self.get_layers()[index]

    def get_node(self, layer: int, index: int):
        return self.get_layer(layer)[index]

    def get_weights(self, layer: int, index: int):
        if layer == 0:
            raise WrongLayerChosen
        return self.get_layer(layer)[index].get_weights()

    def set_weights(self, layer: int, index: int, weights: list[float]):
        if layer == 0:
            raise WrongLayerChosen
        elif layer == self.layer_count - 1 or layer == -1:
            self._output_layer[index].set_weights(weights)
        elif layer < 0:
            layer += 1
        self._hidden_layers[layer][index].set_weights(weights)

    def get_weight(self, layer: int, index: int, weight_index: int):
        if layer == 0:
            raise WrongLayerChosen
        return self.get_layer(layer)[index].get_weight(weight_index)

    def set_weight(self, layer: int, index: int, weight_index: int, new_weight: float):
        if layer == 0:
            raise WrongLayerChosen
        elif layer == self.layer_count - 1 or layer == -1:
            self._output_layer[index].set_weight(weight_index, new_weight)
        elif layer < 0:
            layer += 1
        self._hidden_layers[layer][index].set_weight(weight_index, new_weight)

    def get_bias(self, layer: int, index: int):
        if layer == 0:
            raise WrongLayerChosen
        return self.get_layer(layer)[index].get_bias()

    def set_bias(self, layer: int, index: int, new_bias: float):
        if layer == 0:
            raise WrongLayerChosen
        self.get_layer(layer)[index].set_bias(new_bias)

    def get_value(self, layer: int, index: int):
        return self.get_layer(layer)[index].get_value()

    def set_value(self, layer: int, index: int, new_value: float):
        self.get_layer(layer)[index].set_value(new_value)


if __name__ == "__main__":
    network = Network(10, [9, 8, 7], 6)

and the code for the nodes is here:

class InputNode:
    def __init__(self):
        self._value: float = 0

    def set_value(self, value: float):
        self._value = value

    def get_value(self):
        return self._value


class Node(InputNode):
    def __init__(self, node_count_of_layer_before):
        super().__init__()
        self._bias: float = 0
        self._weights: list[float] = [0 for i in range(node_count_of_layer_before)]

    def set_weights(self, weights: list[float]):
        self._weights = weights

    def get_weights(self):
        return self._weights

    def set_weight(self, index: int, value: float):
        self._weights[index] = value

    def get_weight(self, index: int):
        return self._weights[index]

    def set_bias(self, bias: float):
        self._bias = bias

    def get_bias(self):
        return self._bias


class OutputNode(Node):
    def __init__(self, node_count_of_last_hidden_layer):
        super().__init__(node_count_of_last_hidden_layer)

r/learningpython • • Oct 27 '23

Loosing my mind - Advice needed (college)

2 Upvotes

I'm currently enrolled in an introductory Python programming course at university, and I'm struggling so bad right now. While most people I know find this course easy, I'm finding it extremely difficult and often feel lost. I work through the zyBooks assignments each week, taking my time to ensure I grasp the material, yet my classmates who speed through them seem to have a much better understanding of the homework and lab work.

Speaking of the lab, I frequently rely on my lab partners, and since we rotate partners often, it can be somewhat embarrassing to sit quietly, pretending to contemplate the code while I daydream, and they complete the entire assignment. It's not because I want to do this, but I just get confused and give up a couple times before fully checking out. Almost everyone I talk to in this class either took AP Computer Science in high school or just quickly grasps new concepts. The pace of this course is crazy. We're introduced to concepts like nested loops, arrays, etc., and suddenly we're expected to be experts in these functions, knowing how and when to implement them and understanding their impacts on other python functions. I currently have a B but that's only because I've resorted to cheating on homework and relying on others in the lab just to get by.

The most frustrating aspect of all of this is not just how challenging it is for me, but how effortless it appears to be for others. I slacked off throughout high school, and I feel like I haven't developed the skills needed to handle these demanding college courses. It's disheartening, especially because this is just the introductory level. I'm determined not to change my major; I want to graduate with a STEM degree, and the idea of becoming a software engineer and the potential salary are motivating factors. But I genuinely just might be too dumb.

Despite the difficulties, I want to mention that I am learning a lot. It's just that I'm learning at a slower pace and definitely not learning as much compared to others, and my primary concern is how I'll fare in the next course, data structures. I know it's a crucial class, and if I'm struggling now, I fear my chances of success might be slim. I'm seeking your honest opinion, but please, some words of encouragement. I truly need that support right now.


r/learningpython • • Oct 04 '23

Youtube function disappearing when opening document again.

Post image
2 Upvotes

I’m two weeks into learningand making a YouTube downloader. After installing Pytube when I typed YouTube in highlight in Visual Studio code and works.

When I open the code again a day late r YouTube will not be highlighted in the code. When I type YouTube it will not change and highlight an add a function.

When I run the code it says the YouTube function is not defined.

I got stuck for hours and did a fresh Iinstall of windows. Everything worked and then a day later the same issue happened.

I’m completely stuck. I have no idea why Visual code works and then Stops. I’ve deleted all my plugins and extensions and downloaded again. I still can not get YouTube to work in my code.

Any help is appreciated


r/learningpython • • Sep 28 '23

imbalanced dataset

2 Upvotes

Hi,
For a project, I need to create a machine learning program that predicts whether a person is within a certain income bracket. The dataset is pretty large with 159 variables and n = 220000. So now within the dataset, more than 60% consist of zeros which makes the randomForest overfit, and the cross-validation accuracy stays stranded at 80 %. Does anyone have any tips on how to balance the dataset and get a higher cross-validation accuracy?


r/learningpython • • Sep 26 '23

Is there a glossary for this?

2 Upvotes

I was wondering if there is a compiled list of python commands with explanations?


r/learningpython • • Sep 23 '23

Excel to Python Exercises, Calculating Percentages

2 Upvotes

I hope this will be useful to this category. I'm doing some lessons showing people how to do analysis in Python by showing how something in Excel can be done in Python.

One of the ways I learned Python was by taking things I was doing in Excel and trying to do them in Python. In that spirit this lesson we will look at how Excel calculates a percent, and we'll show you how to do that in Python.

https://youtu.be/aw_kEgSWXZE?si=tbpE9L5ASTgmD1AX


r/learningpython • • Sep 21 '23

Help

2 Upvotes

So I'm brand new at this coding thing. I'm doing little things to learn but it's hard to find answers of why you use certain symbols. I have 2 that I'm curious about if anyone can help? For the hello world, I see a code that says helloworld = hello + " " + world. Why are the plus signs and quotation marks there? Also another is if my string == "hello": print("String: %s" % mystring) why is the %s and just % sign there and why is a colon in the middle of a string?


r/learningpython • • Jun 27 '23

requests-oauth vs. MSAL+requests?

2 Upvotes

I'm starting work on a script that will access a SharePoint drive and looking for guidance on how best to access it. The msgraph-python libraries are not yet production ready. That leaves requests, I think. Does anybody have strong opinions on one or the other?


r/learningpython • • Jun 11 '23

Python Community: Let's Dive Into the Exciting World of Web Scraping!

Thumbnail self.coder_corner
2 Upvotes

r/learningpython • • May 21 '23

Demystifying Python Decorators: A Simple Guide

Thumbnail self.coder_corner
2 Upvotes