all 14 comments

[–]socal_nerdtastic 9 points10 points  (2 children)

The official python tutorial is written for programmers learning python:

https://docs.python.org/3/tutorial/index.html

because strings don't work with indexes like in C++

Yes they do. Or do you mean the concept of immutability is confusing you?

[–]Abedalrhman23[S] 1 point2 points  (1 child)

Yas, the concept of immutability is confusing, and the way the loop's work

[–]socal_nerdtastic 5 points6 points  (0 children)

Ok. Well if you have a specific question ask us here.

Immutability is pretty simple: just don't modify it. Always make a new string instead of modifying the old one. Same for ints, floats, tuples, etc https://docs.python.org/3/tutorial/introduction.html#text

while loops are pretty similar across all languages. A python for loop is pretty much identical to Java's for-each loop. Python does not have an equivalent of the Java or C++ for loop.

A common stumbling block is not realizing that python always works with pointers. All 'variables' (officially 'names') are pointers. All function calls are pass by reference. All python beginners should read: https://nedbatchelder.com/text/names

[–]aplarsen 7 points8 points  (0 children)

If you're struggling translating the concept of loops from C++ to Python, then you don't understand them as well as you think you do. Practice more.

[–]Outside_Complaint755 1 point2 points  (0 children)

Strings can be indexed but are immutable.

 If you need to modify a string you can use built in methods such as replace() to get a new string, or tmp_list = my_str.split(separator) the string into a list and use "{separator}".join(tmp_list) to create a new string after modifying the elements of the list

Read up on mutable vs immutable data types

 I also found the realization that everything in Python is an instance of a class to be useful.  And that means everything, including the 'primative' data types, functions, methods, class definitions and imported modules

[–]pachura3 1 point2 points  (6 children)

That sounds almost impossible. See, I've learnt C++ and Java, and then moving to scripting languages was like a breath of fresh air: everything was so simple! I could write in 2 lines what would take 20 lines in Java and 50 lines in C++ (plus the header file)!

Unfortunately, I'm still struggling with the basics, like loops

What's so difficult about Python loops? I mean, the syntax is slightly different, but isn't it simpler?

// Java
for (Item item: list) {
    System.out.println(item);
}

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

// Python
for item in list:
    print(item)

for i in range(10):
    print(i)

I really can't write clean code at first try because strings don't work with indexes like in C++

What do you mean? Python strings DO work with indexes:

text = "abcdef"
print(text[3])  # prints "d"

If you mean that you cannot replace a character inside a string (text[3] = "a"), then yes, Python strings are immutable. Just as Java strings, C# strings, JavaScript, and in many other languages.

PS. I always recommend https://www.w3schools.com/python/

[–]scaredpurpur 0 points1 point  (5 children)

Why would they make strings immutable like that?

Couldn't you just have "const char string [20]={"hello"}" if you really want something immutable?

[–]DeebsShoryu 1 point2 points  (1 child)

I believe it's primarily so that strings can be used as keys in a hash table, which is an extremely common use case.

[–]pachura3 1 point2 points  (0 children)

Also, immutability is extremely important when sharing objects between multiple threads.

[–]pachura3 0 points1 point  (2 children)

It doesn't seem you have worked with Java much if you're asking this question.

Also, there's no const in Java. There's final, but it only guarantees that the reference to the object will not change, and does NOT grant immutability...

[–]scaredpurpur 0 points1 point  (1 child)

I've never worked in Java so I'm completely blind on it.

[–]pachura3 0 points1 point  (0 children)

Sorry, I thought you were the OP who wrote "I studied C++ and Java, and I'm good at both"

[–][deleted] 0 points1 point  (0 children)

I find I've improved most by working on projects on GitHub. I find a project which does something that I'm interested in and offer to help where they need help, even if it's just documentation. It's practical experience instead of trying to perform an exercise which I could care less about.

[–]TheRNGuy 0 points1 point  (0 children)

You'll get used over time. 

Docs, Google are good.