loading...
Cover image for Intro to Python Pt. #1

Intro to Python Pt. #1

chase42 profile image Chase Collins Updated on ・3 min read

A little bit about Python:

  • First released in 1991
  • Emphasizes on readability
  • Supports multiple programming paradigms
  • Named after Monty Python

A good goal to have through learning programming and problem-solving is to think like a computer scientists. Thinking like a computer scientist means incorporating some of the best parts of math, engineering, and natural science.

Math: Uses formal language to denote ideas.

Engineering: Design things, build systems, and test tradeoffs among alternatives.

Natural Science: Observe the behavior of complex systems, form hypotheses, and test predictions.

Computer scientists think by using and combining these things.

But, above all else, the most significant skill for a computer scientist is the ability to solve complex problems.

Problem solving means:

  1. Defining problems
  2. Reasoning about solutions
  3. Expressing a solution

By learning to program and understanding the fundamentals of computer science, you are receiving the preeminent "transferable skill": how to solve complex problems.

What is a program?

A program is a sequence of instructions that specifies how to perform a computation.

The computation could be anything. It could require you to get the length of one side of a triangle , to look for every instance of a certain word and to replace it, or transform a color image into a black-and-white image.

Regardless of the programming language, every language is made up of a few basic instructions.

  • Input - Receiving data from a keyboard, a file, the network, or some other device.
  • Output - Presenting data on a screen, saving it to a file, sending it over a network.
  • Arithmetic and Logic - Adding, subtracting, multiplying and dividing.
  • Conditional execution - Running the code given certain conditions.
  • Repetition - Executing repeatedly but with a slight modification.

Why is it important to understand this? Because, regardless of the complexity of the problem you are dealing with, if you can break it into small parts that fit into these categories, you can write a program to solve the problem.

A primary reason that Python is a well-loved programming language is its high readability. Programmers often point out that humans read code much more frequently than they write it.

Comments

 # Single line comment 
"""docstring that is available at runtime by accessing _*doc*_ variable"""

Operators

JavaScript

i++
i--

!
&&
||

Python

i += 1 
i -= 1

not 
and 
or

Strings

JavaScript

let s = "Bob";
let x = 5; 

Python

s = "Bob"
x = 5

Conditionals

JavaScript

if(grade > 85) {
    return "E"
} else if (grade >= 70) {
    return "S"
} else {
    return "F"
}

Python

if grade > 85:
    return "E"
elif grade >= 70:
    return "S"
else: 
    return "F"

Arrays / Lists

JavaScript

nums = [1,2,3]

nums[99] = 100

nums.length

Python

nums = [1,2,3,4]

nums.append(5)
nums.insert(2,2.5)

len(nums)

Loops

JavaScript

for(let i = 0; i < 10; i++) {
    console.log(i)
}

Python

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

nums = [1,2,3,4] 
for n in num: 
    print(i)

To sum it all up...

Some minor syntactical differences between Python & other programming languages.

BUT

Major constructs like lists, control structures, arithmetic and logical operators, and control flow all still possible.

(You just might have to google an example)

Collections

  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries

Lists:

Very similar to JavaScript arrays. They are a group of items we reference by index.

✅ Mutable

✅ Duplicate Items

Tuples:

They are another group of items we reference by index. Uses parentheses instead of square brackets.

❌ Mutable

✅ Duplicate Items

Sets:

They are unordered groups of items. Uses curly brackets instead of square brackets or parentheses.

✅ Mutable

❌ Duplicate Items

Dictionaries:

They are groups of values we reference through their keys. Very similar to JavaScript objects.

✅ Mutable

✅ Duplicate Items

Creating new functions

A function definition specifies the name of a new function and the sequence of statements that run when we call that function.

def print_lyrics():
      print("I want to dream")

def is a keyword in Python that shows that this is a function definition. THe function name is print

To sum it all up...

There are several data structures available to us in Python.

  • Each have their own unique pros and cons.
  • Many similarities to other languages like JS.

Posted on by:

chase42 profile

Chase Collins

@chase42

Full Stack Web Developer [JavaScript, HTML/CSS, React & Node/Express]. Currently a Junior Web Developer Intern @ SetPatrol

Discussion

markdown guide
 

Hello! I noticed some typos in the python code:

  • The docstring uses tripple quotation marks, you only had 2 in the example
  • The if statement has a semi-colon instead of colon
  • The elif statement is missing the number to compare to (70) and a colon
  • The array example nums[99] = 100, which I suppose should have been stroke using the markdown syntax isn't as it is in a code block. It makes it a bit unclear what you mean.

Also, while you can use semi-colon when declaring the variables in the string example, they have no effect and is never used in python (unless you want to put multiple statements on the same line)

 

Thank you! Appreciate the help!