DEV Community

jimenezfede
jimenezfede

Posted on

Python

Python is a very popular coding language. Its creator Guido van Rossum made it to be read easily and to be used for any task. Guido has a master’s degree in computer science and mathematics, which explains why python works great with mathematics. It’s open-source so anybody can help by contributing to the language. Today python’s latest update is a major version 3. As much as I wanna go over everything in python, I’m going to just go over the very basics of syntax and forming strings. Python uses a new line to finish an operation, whereas javascript uses a semicolon. For loops, functions, and classes are made through spaces with python, javascript uses curly braces. At least one space is needed, but four is the standard practice. The amount of spaces chosen has to be used on each line for the block of code to work, else python gives an error.

Variables

Python creates variables by assigning them to a value. Theres no declaring a variable in python as there is in javascript.

name = 'Bruce Wayne'
Enter fullscreen mode Exit fullscreen mode

Pseudo coding, writing comments, is done by writing after the # character. In python, a variable can be set to a particular datatype through a process called casting.

x = str('1') # prints '1'
x = int(1) # prints 1
Enter fullscreen mode Exit fullscreen mode

Type method is a python function that returns the datatype of a variable

print(type(name)) # prints <class 'str'>
Enter fullscreen mode Exit fullscreen mode

Python's string datatype can be formed with single or double quotes. For a string that will run more than one line, they can be formed with three single or double quotes.

achilles = '''Let no man forget how menacing we are, we are lions! Do you know
whats waiting beyond that beach? Immortality! Take it! It's yours!'''
Enter fullscreen mode Exit fullscreen mode

If looking for a word inside of a string, the keyword in can be used.

print('lions' in achilles) # prints true
Enter fullscreen mode Exit fullscreen mode

Can also use an if conditional to check for a word

sparta = 'SPARTANS! what is your profession?'

if 'profession' in sparta:
  print('HA-OOH! HA-OOH! HA-OOH!') # prints HA-OOH! HA-OOH! HA-OOH!
Enter fullscreen mode Exit fullscreen mode

Or can check if a word isn’t in a string with the not keyword

if 'cook' not in sparta :
  print('false') # prints false
Enter fullscreen mode Exit fullscreen mode

To turn the characters in a string from lower case to upper case, theres an upper method.

upperCaseBatman = name.upper()
print(upperCaseBatman) # prints BATMAN

print(upperCaseBatman.lower()) # prints batman
Enter fullscreen mode Exit fullscreen mode

Adding a string and a variable can be done with the addition operator.

def vengeance():
  print('I am ' + name)

vengeance() # prints I am Batman
Enter fullscreen mode Exit fullscreen mode

Python has many methods for strings. These are just a few. The syntax is pretty easy to catch on. And I must admit, it does make python very easy to read. Also makes it look really clean.

Sources

Latest comments (0)