Datatype & Variables


Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!


Variables are named locations which are used to store references to the object stored in memory. The names we choose for variables and functions are commonly known as Identifiers. In python Identifiers must obey the following rules.

  1. All identifiers must start with letter or underscore ( _ ) , you can’t use digits. For e.gmy_var  is valid identifier while 1digit  is not.
  2. Identifiers can contain letters, digits and underscores ( _  ).
  3. They can be of any length.
  4. Identifier can’t be a keyword (keywords are reserved words that Python uses for special purpose).Following are Keywords in python 3.

Assigning Values to Variables

Values are basic things that programs works with. For e.g 1 , 11 , 3.14 , "hello"  are all values. In programming terminology they are also commonly known as literals. Literals can be of different type for e.g 1 , 11  are of type int , 3.14  is float and "hello"  is string . Remember in python everything is object even basic data types like int, float, string, we will elaborate more on this in later chapters.

In python you don’t need to declare types of variable ahead of time. Interpreter automatically detects the type of the variable by the data it contains. To assign value to a variable equal sign (= ) is used. =  is also known as assignment operator.

Following are some examples of variable declaration:

Note: In the above code x  stores reference to the 100  ( which is an int object ) , x  don’t store 100 itself.

In Python comments are preceded by a pound sign ( # ). Comments are not programming statements that python interpreter executes while running the program. Comments are used by programmers to remind themselves how the program works. They are also used to write program documentation.

Simultaneous Assignments

Python allow simultaneous assignment syntax like this:

this statements tells the python to evaluate all the expression on the right and assign them to the corresponding variables on the left. Simultaneous Assignments is helpful to swap values of two variables. For e.g

Expected Output:

Python Data Types

Python has 5 standard data types namely.

a) Numbers
b) String
c)  List
d) Tuple
e) Dictionary
f)  Boolean – In Python True and False  are boolean literals.  But the following values are also considered as false.

  • 0 – zero , 0.0 ,
  • [] – empty list , () – empty tuple , {} – empty dictionary ,  ”
  • None

 

Receiving input from Console

input()  function is used to receive input from the console.

Syntax input([prompt]) -> string

input()  function accepts an optional string argument called prompt  and returns a string.

Note that input()  returns string even if you enter a number, to convert it to an integer you can use int() or eval() .

Importing modules

Python organizes codes using module. Python comes with many in built modules ready to use for e.g there is a math  module for mathematical related functions, re  module for regular expression and so on. But before you can use them you need to import them using the following syntax:

You can also import multiple module using the following syntax:

here is an example

First line import all functions, classes, variables, constant in the math  module. To access anything inside math module we first need to write module name followed by ( . ) and then name of class, function, constant or variable. In the above example we are accessing a constant called pi  in math  module

In next chapter we will cover numbers in python.


%Matlab程序读取sst数据: close all clear all oid='sst.mnmean.nc' sst=double(ncread(oid,'sst')); nlat=double(ncread(oid,'lat')); nlon=double(ncread(oid,'lon')); mv=ncreadatt(oid,'/sst','missing_value'); sst(find(sst==mv))=NaN; [Nlt,Nlg]=meshgrid(nlat,nlon); %Plot the SST data without using the MATLAB Mapping Toolbox figure pcolor(Nlg,Nlt,sst(:,:,1));shading interp; load coast;hold on;plot(long,lat);plot(long+360,lat);hold off colorbar %Plot the SST data using the MATLAB Mapping Toolbox figure axesm('eqdcylin','maplatlimit',[-80 80],'maplonlimit',[0 360]); % Create a cylindrical equidistant map pcolorm(Nlt,Nlg,sst(:,:,1)) % pseudocolor plot "stretched" to the grid load coast % add continental outlines plotm(lat,long) colorbar % sst数据格式 % Variables: % lat % Size: 89x1 % Dimensions: lat % Datatype: single % Attributes: % units = 'degrees_north' % long_name = 'Latitude' % actual_range = [88 -88] % standard_name = 'latitude_north' % axis = 'y' % coordinate_defines = 'center' % % lon % Size: 180x1 % Dimensions: lon % Datatype: single % Attributes: % units = 'degrees_east' % long_name = 'Longitude' % actual_range = [0 358] % standard_name = 'longitude_east' % axis = 'x' % coordinate_defines = 'center' % % time % Size: 1787x1 % Dimensions: time % Datatype: double % Attributes: % units = 'days since 1800-1-1 00:00:00' % long_name = 'Time' % actual_range = [19723 74083] % delta_t = '0000-01-00 00:00:00' % avg_period = '0000-01-00 00:00:00' % prev_avg_period = '0000-00-07 00:00:00' % standard_name = 'time' % axis = 't' % % time_bnds % Size: 2x1787 % Dimensions: nbnds,time % Datatype: double % Attributes: % long_name = 'Time Boundaries' % % sst % Size: 180x89x1787 % Dimensions: lon,lat,time % Datatype: int16 % Attributes: % long_name = 'Monthly Means of Sea Surface Temperature' % valid_range = [-5 40] % actual_range = [-1.8 36.08] % units = 'degC' % add_offset = 0 % scale_factor = 0.01 % missing_value = 32767 % precision = 2 % least_significant_digit = 1 % var_desc = 'Sea Surface Temperature' % dataset = 'NOAA Extended Reconstructed SST' % level_desc = 'Surface' % statistic = 'Mean' % parent_stat = 'Mean' 解释这个代码的意思,并将其转换为python代码
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值