DEV Community

Cover image for Introduction to Modern Python

Posted on

Introduction to Modern Python


You most probably know why and are now scouring the internet for Zen advice on how to go about the language. Well, Same here.
We will discuss the absolute basics and I will link some resources(and cheatsheets) that will be be helpful in learning the Language. I will discuss Primitive Data Types, Math Operands and inbuilt data Structures.

Data Types

1. Strings

  • Strings are usually alphanumeric characters enclosed in "" or ' '. They can be manipulated in a variety of ways through: Concatenation, Replication, Slicing and a variety of other methods(convert to upper, lower and replacing or iterating through the characters)
x = 'Python'
y = 'Developer'
#string Concatenation
x + y # returns PythonDeveloper
#string Replication
x * 3 # returns PythonPythonPython

len(x) # length of string -- in this case 6
x[0] #returns first letter - p
x[-1]#returns the last letter - n
x[0:3]#returns characters from within the specified range -- pyt

name = f 'I am a {x }{y}' # returns I am a python developer 
x.upper()    #converts to uppercase PYTHON
x.lower()    #converts to uppercase python
x.title()    #converts to titlecase Python
x.strip()    #removes first and last character and returns the string
x.find(p)  #finds index of passed character within the string
x.replace(a, b) #replaces the first passed character with the second one
a in x      #find character within the string
Enter fullscreen mode Exit fullscreen mode

2. Numeric - These values can wither be integers, floats or complex numbers
3. Boolean - These is either True or False. It can be used mainly for comparison operations and conditional statements.
4. Special- None

a = 1       # integer
b = 1.1     # float
c = 1 + 2j  # complex number (a + bi)
d = a     # string
e = True    # boolean (True / False)
b = ()      #an empty list evaluates to None
Enter fullscreen mode Exit fullscreen mode

Math Operands

  • Mathematical Operands enable the performance of mathematical operations within our program.
  • Just like ordinary mathematics, the operands appear in order of precedence as listed below: (*) ()(/)(//)(%) (+)(-) in that order.
2 ** 3     #returns the exponent = 8
2 * 3      # returns 6
12 / 2     #returns 4
24 // 5    #returns 4
24 % 5     #returns the remainder that is 4
28 + 3     #returns 31
28 - 3     #returns 25
Enter fullscreen mode Exit fullscreen mode

Data Structures

1. Lists

  • Lists contain a collection of items. They are very similar to arrays, however the only difference is that list can store different data type elements while arrays only store elements of a similar data type
  • They are defined using [] or list() constructor
list1 = ['potatoes',59, 8.5, 'sugar']
list2 = list(('potatoes',59, 8.5, 'sugar'))
Enter fullscreen mode Exit fullscreen mode

2. Tuples

  • They are an immutable(hence they cannot be changed or modified once declared) and unordered data structures that store a collection of items. They are defined using ()
tuple1 = ('potatoes',59, 8.5, 'sugar')
len(tuple1) # returns 4 that is the number of items in the tuple
del tuple1 # to delete the entire tuple
Enter fullscreen mode Exit fullscreen mode

Difference Between Tuples and Lists

  1. Lists are defined using [] while tuples are defined using ()
  2. Lists are mutable(can be changed and modified) while tuples are immutable
  3. Tuples are more memory efficient and execute faster than lists.

3. Dictionaries

  • Dictionaries are data structures that store items as key:value pairs. They are defined using {}
  • Using the keys and values different items within the dictionary can be accessed
my_dict = {3: 'd', 2: 'c', 1: 'b', 0: 'a'}
my_dict[0] # returns the first item in the dictionary
Enter fullscreen mode Exit fullscreen mode

4. Sets

  • Sets are unordered data structures that store unique elements_(No duplicates Allowed!!)_. They are defined using {}
my_set = {5, 4, 6, 7}
Enter fullscreen mode Exit fullscreen mode

Variables are place-holders that serve to reference items. CONSTANTS are values that once declared remain the same and cannot be changed within the scope of the program.
Rules that Governs naming a variable.

  1. It can be only one word(No spaces between the words please)
  2. It can use only letters, numbers, and the underscore (_) character.
  3. It can’t start with a number. CONSTANTS are usually declared in uppercase letters.

Python comments are preceded with a #. For multiple lines, you can learn a shorthand command for commenting, eg. Ctrl + K + C in VSCode.

#this is a CONSTANT
#this are variables
myName = 'Developer X'
my_age = 15
Enter fullscreen mode Exit fullscreen mode

Beginner Friendly CheatSheets

  1. Programming with Mosh :
  2. Official Python Cheatsheet :

Discussion (0)