Introduction to Computing with Python - Tutorial 2 Basic Components of CodeFile

Today

•    Basic Components of Code
•    Types
•    Conversions and Casting
•    Exercises 

CONSTANTS, VARIABLES, IDENTIFIERS 

Reminder 

Constants 

•     Constants are any value: 
        -  Numerical: 15.6, 120 
        -  String: “Hello”, ‘bye’ 
        -  Boolean: True, False
•     Can appear on their own or assigned

into a variable. 

Variables 

•     A Variable is a cell in memory, with a given name (identifier) and value. 

•     Before we can use the variable, it must be initialized with a value.
•     It is possible to update a variable by assigning a different value: 

Assignment 

•     Assignment is made of two parts: 

•     How is Python performing this operation?
        1. Evaluating the expression on the right side.
        2. Writes the result to the variable. 

 •     During assignment, the computer does not “solve an equation”. This is not Algebra!

 

 • The righthand side is evaluated first. Therefore,
• the following statement in Python is legitimate:

Identifiers 

•     Variable (or function) names are called Identifiers.
•     Rules for legal identifiers in Python:
        1.  Only English letters (lowercase or UPPERCASE), digits or underscore (_)
        2.  Cannot start with a digit
        3.  Cannot be a reserved word
•     Spyder IDE will mark a reserved word in unique color. 

•     Legal identifiers:
temp counter 
the_one intro2cs


       

•     The computer doesn’t care what names you choose, but you should. It is important to choose meaningful names for your variables that will aid in understanding the function of your program.
•     Which example is clearer? 

TYPES 

Variable Types 

 •     Each variable has a Type.
•     Regardless of type, a variable is stored in memory as a sequence of bits.
•     The difference between types signifies how Python translates the contents of the variable to bits and back.
•       These contents can vary (numbers, characters, colors, images, music….)

•     In Python, a variable type is set automatically during assignment. 

•     In Spyder, we can see the different variables and their type under “Variable Explorer”. 

Types: String 

•     Python identifies strings by double quotes and single- 
quotes:
        •      Double Quotes:   my_string = “Hello”
        •      Single Quotes:     my_string = ‘hello’

•     Must be consistent: 

Type Conversion 

•     Occasionally, we want to perform calculation on values which are stored as different types. For example, adding int with float (100 + 2.4).
•     To perform such calculation, the type must be converted.
•     There are two types of type conversions:
        -  Automatic – happens without the user’s involvement: 

 

        -  Implicit – happens by implicit request (also called type casting): 

 Python Operators

 

•     Operators supported by string types:
•     Addition: +
        • The two operands must be strings.
        • Performs concatenation of strings.
•      Multiplication: *
        • The two operands must be a string and an integer (the order doesn’t matter).
        • Concatenates the string to itself.

 

Implicit Conversion 

 •     No automatic conversion. The computer doesn’t know which result you want!

 

 

EXERCISES 

 Exercise 1

 

 

Exercise 2 

 

 

Exercise 3 

 

•     Write a program that gets a number as input and returns the following number:
        num = input() 
        num = int(num)
•     Or:
        num = int(input())
•     Finally, we add: 
        num = num + 1 
        print(num) 

Comments and Documentation 

 

Exercise 4 

•     Write a program to calculate length of hypotenuse (use Pythagoras). 

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带带萌新JAMMY BEAR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值