Skip to content

Instantly share code, notes, and snippets.

View techstackmedia's full-sized avatar
🏠
Working from home

Bello Osagie techstackmedia

🏠
Working from home
View GitHub Profile
# assign 4 to the variable x
x = 4
// C code
// C is a statically-typed language
int x = 4;
# Python code
# Python is a dynamically-typed language
x = 4
x = 1 # x is an integer
x = 'hello' # now x is a string
x = [1, 2, 3] # now x is a list
x = 4.5
x.is_integer()
# False
x = 4.0
x.is_integer()
# True
type(x.is_integer)
# <class 'builtin_function_or_method'>
# Numbers
integer_number = 23
floating_point_number = 23.5
complex_number = 2 + 3.8j
# Boolean
boolean_value = True
# String Literal
string_literal = 'This is a text.'
isinstance('Hello', (float, int, str, list, dict, tuple))
# True
# Primitive Data Structures (Types)
# All Primitive Data Types are Immutable Objects
int_ = 5 # int - Immutable Object
float_ = 5.25 # float - Immutable Object
complex_ = 2 + 7.25j # complex - Immutable Object
str_ = 'This is a text' # str - Immutable Object
bool_ = False # bool - Immutable Object
type(int_) # int - Integer Numbers
# <class 'int'>
# Number - Integer, Float, Complex
# Integer
Integer in Python are numbers without decimal e.g 20
# A different data type can be converted to an integer
# Integer conversion is done with the int() function
book_price = 20
print(book_price)
# 20
book_price = '30'
print(int(book_price))
z = 2 + 17.6j
print(z)
# (2 + 17.6j)
print(z.real)
# 2.0
print(z.imag)
# 17.6
print(z.conjugate())
# (2-17.6j)