Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 2.85 KB

05. Variables.md

File metadata and controls

78 lines (53 loc) · 2.85 KB

1. Variables in C

In C, variables must be a specified data type and they need format specifiers to be printed.


1.1. Basic Data Types

Data Type Size Description
int 2 or 4 bytes stores whole numbers
float 4 bytes stores fractional numbers up to 7 decimal digits
double 8 bytes stores fractional numbers up to 15 decimal digits
char 1 byte a single character / letter / number / ASCII value
long 8 bytes stores whole numbers, can go higher than int
bool 2 bytes stores 'true' or 'false' values

1.2. Basic Format Specifiers

Format Specifier Data Type
%d or %i int
%f float
%1f double
%c char
%s strings

1.3. Operators

  • '+' for addition
  • '-' for substraction
  • '*' for multiplication
  • '/' for division
  • '%' for remainder

1.4. Syntactic Sugar

C also supports syntactic sugar, or shorthand expressions for the same functionality. We could equivalently say counter += 1; to add one to counter before storing it again. We could also just write counter++ or counter--.


2. Variables in Python

In Python, variables don't need a specified data type or a format specifier to pe printed. A variable is created when first assigning a value to it.


2.1. Basic Data Types

Data Type Size Description
int 28 bytes stores whole numbers
float 24 bytes stores fractional numbers
bool 28 bytes stores 'True' or 'False' values
string 49 bytes +1 byte for each character

2.2. Other Data Types

Besides the previously mentioned data types, Python also has as built-in types:

  • complex - numeric type that returns a complex number by specifing a real number and an imaginary number.
  • list - sequence type that stores ordered, changeable, and duplicate items.
  • tuple - sequence type that stores multiple items that are ordered, unchangeable and duplicate.
  • set - set type that stores unordered, unchangeable and don't allow duplicates.
  • dict - mapping type that stores ordered, changeable, and don't allow duplicates. Stores data in key - value pairs.
  • range - sequence type returns a sequence of numbers.

2.3. Syntactic Sugar

Similar to C, Python also supports syntactic sugar, although only the expression counter += 1.