I'm going to be converting my notes as I learn Python into blog posts by comparing it to a language I already know - JavaScript. I'm starting with freeCodeCamp’s Scientific Computing with Python course, diving straight into string manipulation and building a cipher.
Variables: No More let
or const
In JavaScript, we explicitly declare variables using let
, const
, or (though not recommended) var
:
// JavaScript Example
let number = 5;
const greeting = "Hello!";
Python, on the other hand, skips the keyword entirely—just assign a value and you’re good to go:
# Python Example
number = 5
greeting = "Hello!"
Strings: Mostly the Same
Strings in Python and JavaScript are similar in how they’re created:
// JavaScript Example
let str1 = "I'm a string in JavaScript";
let str2 = 'So am I';
# Python Example
str1 = "I'm a string in python"
str2 = 'So am I'
However, I did read that Python doesn't use template literals like in JavaScript but this isn't in the lesson so we will come back to it.
Printing: print()
vs console.log()
Printing is straightforward in both languages:
// JavaScript Example
console.log("Hello, JavaScript world!");
# Python Example
print("Hello, python world!")
String Indexing: Familiar, But With a Bonus
String indexing is essentially the same as in JavaScript:
// JavaScript Example
let text = "Hello World";
console.log(text[8]); // "r"
# Python Example
text = "Hello World"
print(text[8]) # "r"
However, Python allows negative indices, which makes grabbing the last character much simpler:
# Python Example
print(text[-1]) # "d"
In JavaScript, you’d have to do:
// JavaScript Example
console.log(text[text.length - 1]); // "d"
This is a nice Python feature that makes things a little cleaner.
Finding String Length & Type
Python’s len()
function is similar to JavaScript’s .length
property:
// JavaScript Example
console.log("Hello".length); // 5
# Python Example
print(len("Hello")) # 5
However, Python requires a function call (len())
, whereas .length
is a property in JavaScript.
Checking a variable’s type is also slightly different:
// JavaScript Example
console.log(typeof "Hello"); // "string"
# Python Example
print(type("Hello")) # <class 'str'>
Methods vs Functions: The .find()
Method
Like JavaScript, Python has built-in methods for string manipulation. I used .find()
to locate characters in a string, which works similarly to JavaScript’s .indexOf()
:
// JavaScript Example
console.log("My brain hurts!".indexOf("r")); // 4
# Python Example
sentence = "My brain hurts!"
print(sentence.find("r")) # 4
Both return the index of the first occurrence of a character.
Caesar Cipher & The Power of Python’s Simplicity
The first project was implementing a Caesar cipher, where each letter in a message is shifted by a fixed amount in the alphabet.
This involved:
- Finding a letter’s position using
.find()
- Shifting the position by a set value
- Grabbing the new letter using indexing
One key thing I learned was that Python’s string methods are case-sensitive. Running .find()
on an uppercase letter failed since my alphabet string only contained lowercase letters. The fix? Using .lower()
to convert the text before searching:
# Python Example
text = "Hello World"
alphabet = "abcdefghijklmnopqrstuvwxyz"
index = alphabet.find(text[0].lower())
print(index) # 7
This is where we paused - We will continue with this lesson on Day 2.
Top comments (0)