Tsinghua University Undergraduate Programming Tutorial155


This tutorial is a comprehensive guide to programming for undergraduate students at Tsinghua University. It covers the fundamentals of programming, including data types, variables, operators, control structures, and functions. The tutorial also provides an overview of object-oriented programming and data structures. By the end of this tutorial, you will have a solid foundation in programming and be able to write your own programs.

Getting Started

Before you can start programming, you need to install a programming environment on your computer. A programming environment is a software program that allows you to write, compile, and run your programs. There are many different programming environments available, but we recommend using a free and open-source environment like Visual Studio Code or PyCharm. Once you have installed a programming environment, you can start writing your first program.

Data Types

The first step to writing a program is to declare the data types of your variables. A data type specifies the type of data that a variable can store, such as a number, a string, or a boolean. The most common data types are:* int: Integer
* float: Floating-point number
* string: String of characters
* bool: Boolean (true or false)
You can declare a variable by specifying its data type and name, like this:```python
my_int = 10
my_float = 3.14
my_string = "Hello, world!"
my_bool = True
```

Variables

A variable is a named location in memory that can store a value. You can use variables to store data that you want to use in your program. To create a variable, you need to declare its data type and name, like this:```python
my_name = "John Doe"
my_age = 20
```
You can then use the variable in your program by using its name, like this:```python
print("My name is", my_name)
print("My age is", my_age)
```

Operators

Operators are symbols that perform operations on variables. The most common operators are:* Arithmetic operators: +, -, *, /, % (modulus)
* Comparison operators: ==, !=, , =
* Logical operators: and, or, not
You can use operators to perform calculations and compare values in your program. For example, the following code prints the sum of two numbers:```python
x = 10
y = 20
print(x + y)
```

Control Structures

Control structures are used to control the flow of execution in your program. The most common control structures are:* if statements: Execute a block of code if a condition is true
* while loops: Repeat a block of code while a condition is true
* for loops: Repeat a block of code a specified number of times
You can use control structures to make your programs more efficient and readable. For example, the following code prints the numbers from 1 to 10:```python
for i in range(1, 11):
print(i)
```

Functions

Functions are reusable blocks of code that can be called from other parts of your program. Functions can take arguments and return values. You can use functions to organize your code and make it more modular. To create a function, you need to use the `def` keyword, like this:```python
def my_function(x, y):
return x + y
```
You can then call the function by using its name and passing in the arguments, like this:```python
result = my_function(10, 20)
print(result)
```

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that uses classes and objects to represent data and behaviour. Classes are blueprints for creating objects, and objects are instances of classes. OOP makes it easier to organize and manage your code, and it can also make your programs more flexible and reusable. To create a class, you need to use the `class` keyword, like this:```python
class MyClass:
def __init__(self, name, age):
= name
= age
def get_name(self):
return
def get_age(self):
return
```
You can then create an object of the class by using the `()` operator, like this:```python
my_object = MyClass("John Doe", 20)
```
You can then access the attributes and methods of the object by using the dot operator, like this:```python
print(my_object.get_name())
print(my_object.get_age())
```

Data Structures

Data structures are used to organize and store data in your program. The most common data structures are:* Arrays: A collection of elements of the same type
* Lists: A collection of elements of any type
* Dictionaries: A collection of key-value pairs
* Sets: A collection of unique elements
You can use data structures to make your programs more efficient and readable. For example, the following code creates a list of numbers:```python
my_list = [1, 2, 3, 4, 5]
```
You can then access the elements of the list by using the square brackets operator, like this:```python
print(my_list[0])
```

Conclusion

This tutorial has given you a basic overview of programming. You have learned about data types, variables, operators, control structures, functions, object-oriented programming, and data structures. This knowledge will give you a solid foundation for writing your own programs. However, there is much more to learn about programming. The best way to learn is to practice writing code. The more you code, the better you will become.

2024-12-02


Previous:LeEco Cloud Computing: A Comprehensive Overview

Next:How to Unlock Your Locked Sony Xperia Phone