Datatype & Variables

本文介绍了Python编程的基础知识,包括变量和标识符的定义、赋值操作、数据类型、接收控制台输入及模块导入等内容。


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.


Dimensions: time = 1 depth = 102 lat = 21 lon = 31 Variables: s_an Size: 31x21x102x1 Dimensions: lon,lat,depth,time Datatype: single Attributes: standard_name = 'sea_water_salinity' long_name = 'Objectively analyzed mean fields for sea_water_salinity at standard depth levels.' coordinates = 'time depth lat lon ' cell_methods = 'area: mean depth: mean time: mean within years time: mean over years' grid_mapping = 'crs' units = '1e-3' _FillValue = 9.969209968386869e+36 time Size: 1x1 Dimensions: time Datatype: single Attributes: standard_name = 'time' long_name = 'time' units = 'months since 2005-01-01 00:00:00' axis = 'T' climatology = 'climatology_bounds' calendar = 'gregorian' _CoordinateAxisType = 'Time' depth Size: 102x1 Dimensions: depth Datatype: single Attributes: standard_name = 'depth' bounds = 'depth_bnds' positive = 'down' units = 'meters' axis = 'Z' _CoordinateAxisType = 'Height' _CoordinateZisPositive = 'down' lat Size: 21x1 Dimensions: lat Datatype: single Attributes: standard_name = 'latitude' long_name = 'latitude' units = 'degrees_north' axis = 'Y' bounds = 'lat_bnds' _CoordinateAxisType = 'Lat' lon Size: 31x1 Dimensions: lon Datatype: single Attributes: standard_name = 'longitude' long_name = 'longitude' units = 'degrees_east' axis = 'X' bounds = 'lon_bnds' _CoordinateAxisType = 'Lon' crs Size: 1x1 Dimensions: Datatype: int32 Attributes: grid_mapping_name = 'latitude_longitude' epsg_code = 'EPSG:4326' longitude_of_prime_meridian = 0 semi_major_axis = 6378137 inverse_flattening = 298.2572 _CoordinateTransformType = 'Projection' _CoordinateAxisTypes = 'GeoX GeoY' >>这是被读取的盐度数据,温度数据格式与之相同;现在请你根据该s.nc文件和t.nc文件。matla.确认K值为4,请对水团聚类,并画出(温盐,水团)经,纬向断面图
06-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值