Before diving deep into the nitty-gritty details of a programming language or perhaps even a human language, we need
to understand its terminologies and basic principles and start building a basic mental model which we can come back and
refer whenever needed.
The building block of any programming language can be divided mainly into the following:
- Terminologies
- Data Types
- Actions (functions)
- Best Practices
Today I spent understanding some basic Python terms, syntax, its data types and some of its actions or better known
as functions in programming terms.
Data Types
Data Types in simple words are a way to represent values. In our physical world, we have letters, numbers, symbols as
different type of commonly used values. Similarly, Python comprises of these fundamental data types:
- int (to represent numbers)
- float (to represent decimal numbers)
- str (to represent strings)
- bool (to represent boolean)
- list
- tuple
- set
- dict
- complex (not used very often)
- None (to represent an absence of value)
These are the standard data types available in Python. To create our own custom type, classes are used. Specialized data
types can also be used via importing external libraries or modules.
In contrast, in JavaScript, these are the following primitive types available:
- number (for both whole and decimal numbers)
- string
- boolean
- symbol
- bigInt
- null
- undefined Also object as a non-primitive type.
Today I just spent time in understanding the number and string types of Python.
Numbers
There are 3 types of numeric data types:
- int (stores whole numbers of unlimited size)
- float (stores floating-point real number values)
- complex (I just skipped it as of now as I learnt it is not used commonly, similarly to bigInt in JavaScript).
In contrast, JavaScript has two kinds of numeric data types, Number and BigInt.
The type
function is used to determine the type of a value or an expression. (Similar to the typeof
operator in
JavaScript)
num = 100 # variable assignement
print(type(num)) # <class 'int'>
num2 = 99.99
print(type(num2)) # <class 'float>
expression1 = num * 10
print(type(expression1)) # <class 'int'>
expression2 = num + num2
print(type(expression2)) # <class 'float'>
In Python, variable assignment happens by just writing a name and assigning a value using the
=
operator.
In JavaScript, a variable name needs to be preceded withvar
,const
orlet
keyword.
Math functions
There are some built-in mathematical functions that allow us to calculate various mathematical operations with ease.
Math Functions and Constants - this document contains all the built-in
math functions and constants
print(round(2.1)) # 2
print(round(5.9)) # 6
print(abs(-34)) # 34
Will explore the math module in detail some other day.
Variables
Variables store values. In python, these are the variable naming conventions:
- Variables should start with a letter(preferrably lowercase) or underscore and can be followed by numbers or underscore
- Snake case is the conventional way of writing variable with multiple words such as
user_name
(Javascript recommends cameCasing likeuserName
) - They are case sensitive
- Keywords should not overwrite keywords (Python keywords)
Strings
Strings in Python are an ordered sequence of characters (similar to Javascript).
name = 'Python' # string assignment within single quotes
name2 = "Python" # string assingment within double quotes
name3 = '''This is a a very long string and supports
multiline statements as well''' # string assingment within 3 single quotes
name4 = 'Hello! \"Rockstar Programmer\"' # string with escaped character sequence
print(type(name)) # <class 'str'>
print(type(name2)) # <class 'str'>
print(type(name3)) # <class 'str'>
print(type(name4)) # <class 'str'>
String Concatenation
Similar to Javascript, strings can be concatenated using the +
operator. It simply joins or 'concatenates' strings.
first_name = 'Mike'
last_name = 'Tyson'
print(first_name + ' ' + last_name) # Mike Tyson
Type Conversion
Unlike Javascript, where there is implicit type conversion (a.k.a Type Coercion), Python will throw an error if
operations are performed with different types
user_name = 'John'
age = 40
print(user_name + age) # TypeError: can only concatenate str (not "int") to str
# This would work in Javscript where it would convert the result to string type
In Python, types need to be converted explicitly to perform operations with different types
user_name = 'John'
age = 40
print(user_name + str(age)) # John40
print(type str(age)) # <class 'str'>
Similarly, strings can be converted into numbers
lucky_number = 7
lucky_number_stringified = str(7)
lucky_number_extracted = int(lucky_number_stringified)
print(lucky_number_extracted) # 7
That's all for today! Still taking it simple and easy. Will continue understanding the other string operations and built-in methods and functions along
with Boolean and List types. Pretty excited for Day 3!
Have a great one!
Top comments (17)
One thing I found strange about strings in Python is that there doesnβt seem to be much of a material difference between using single and double quotes. In other languages, string interpolation only works inside doubles and singles are treated as exact literals but strong formatting in python works differently (as Iβm sure youβll find soon). The only time I can see a need to choose one over the other is when you have quotes inside your strings. Maybe someone can correct me on this or point out the relevant idiom. Still, keep up the good work man. I waited to long to invest time in learning Python and I have to say itβs been a real joy since I started a few months ago.
After reading some articles and blog posts I found out that:
I am still figuring out the best practices but I personally like the
f
syntax.And yes learning python is a real joy. Hope I keep getting better at it :) Thanks for reading the blog.
Yeah me too. f'{}...' usually meets my needs and I only use format when I need to use printf style formatting or the expression I want to print is complex enough to impact readability inside a simple interpolation.
I believe the %-format approach is from python2 and str.format() is preferred in python3. You still see it a lot though.
"Variables must start with a lowercase letter" can you edit this line because Python variables can start with uppercase letters as well. the variable in python must start with a letter or underscore (letter can be a capital uppercase or lowercase), it can't start with a number or special character.
Thanks for your observation. I have now updated the line.
You're welcome.
Nice man, keep it going with this I'm enjoying this and learning with you.
I am glad you liked it π
So glad to follow you and learning from you
I am glad π
Too good man....
Thank you very much for this, enjoying and learning this with you.
So glad to hear that :)
"Keywords should not overwrite keywords" I'm assuming you meant "Variables should not overwrite keywords"
Some comments may only be visible to logged-in visitors. Sign in to view all comments.