Sunday, August 11, 2019

Learn Python ,RUST and C - blog post 2

Data Types in Python,Rust and C - PART 1

Understanding Data Types in Python,Rust and C progamming languages.

I am not sure whether I can cover all data types comparison in one blog post. So I might be doing it in multiple blog posts.
Standard Data Types:*
  • Numbers.
  • String.
  • List.
  • Tuple.
  • Dictionary.
Numbers Datatypes
**Python programming **
Type format description
int a = 10 signed integer
long 356L Long integers
float a = 12.34 floating point values
complex a = 3.14J (J) Contains integer in the range 0 to 255
enter image description here
The variable types are dynamically typed in python.So when you assign a value to a variable, python compiler determines the type of the variable.
But once the type is defined , the defined operations can only be performed for that particular type.
but you can change the type of the variable by assigning a different value. So in python it is the developer responsibility to maintain and make sure type of the variable intact through out the program.
For example,
>a = 10;
>a = "naveen" //allowed
>a = a + 1 // not allowed
>a = 11  //allowed
>a = a +1 // now this is allowed
**Rust programming **:
Length signed unsigned
8bit i8 u8
16bit i16 u16
32bit i32 u32
64bit i64 u64
128 bit i128 u128
arch isize usize
signed and unsigned means whether a number can be -ve or +ve.
Each signed variant can store numbers from -(2n -1) to 2n-1
unsigned variants can vary from 0 to 2n-1
eg: u8 means 0 to 28-1 = 128
signed means -28-1 to 28-1
Rust’s floating-point types are f32 and f64, which are 32 bits and 64 bits in size, respectively.
In the below example we assigned a value 129 to varialble ‘b’ which is of ‘i8’ ,so the program won’t get compiled.
fn main(){
   let a:i8 = 127;
   let b:i8= 129;  //this will cause overflow.
   let c:f32=123.32;
   let d:i8 = -127;
   let e:u8 = 127;

  println!("{}.{},{},{},{}",a,d,c,e,b);
}

when you compile the program,

error: literal out of range for `i8`
 --> datatype.rs:3:13
  |
3 |   let b:i8= 129;
  |             ^^^
  |
  = note: #[deny(overflowing_literals)] on by default


String:
Strings in python can be denoted by single quotes(’), double quotes("), triple quotes(""").
name1    = 'naveen'
name2    = "davis"
fullname = """naveen davis
                       vallooran"""
python3
It possible to access the characters in a string using their index in python.
python-string2
nam1[0] prints the character 'n'

No comments: